如何使用 Asset 文件夹中的 SQLite 连接

发布于 2024-11-05 22:19:45 字数 34 浏览 0 评论 0原文

如何使用 Asset 文件夹中的 SQLite 连接。

How to use SQLite connection from Asset folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里人 2024-11-12 22:19:45

这称为 DataBaseHelper.java 文件

public class DataBaseHelper extends SQLiteOpenHelper
{
private static String TAG = "TAG";
private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
private static String DB_NAME = "ServiceInfo.sqlite";
private SQLiteDatabase mDataBase; 
private final Context mContext;

public DataBaseHelper(Context context) 
{
    super(context, DB_NAME, null, 1);
    DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    this.mContext = context;
}   

public void createDataBase() throws IOException
{
    boolean mDataBaseExist = checkDataBase();
    if(!mDataBaseExist)
    {
        this.getReadableDatabase();
        try 
        {
            copyDataBase();
        } 
        catch (IOException mIOException) 
        {
            throw new Error("ErrorCopyingDataBase");
        }
    }
}

private boolean checkDataBase()
{
    SQLiteDatabase mCheckDataBase = null;
    try
    {
        String myPath = DB_PATH + DB_NAME;
        mCheckDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    }
    catch(SQLiteException mSQLiteException)
    {
        Log.e(TAG, "DatabaseNotFound " + mSQLiteException.toString());
    }

    if(mCheckDataBase != null)
    {
        mCheckDataBase.close();
    }
    return mCheckDataBase != null;
}

private void copyDataBase() throws IOException
{
    InputStream mInput = mContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream mOutput = new FileOutputStream(outFileName);
    byte[] mBuffer = new byte[1024];
    int mLength;
    while ((mLength = mInput.read(mBuffer))>0)
    {
        mOutput.write(mBuffer, 0, mLength);
    }
    mOutput.flush();
    mOutput.close();
    mInput.close();
}

public boolean openDataBase() throws SQLException
{
    String mPath = DB_PATH + DB_NAME;
    mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    return mDataBase != null;
}

@Override
public synchronized void close() 
{
    if(mDataBase != null)
        mDataBase.close();
    super.close();
}

@Override
public void onCreate(SQLiteDatabase db) 
{ }

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
    Log.v(TAG, "UpgradingDatabase, This will drop current database and will recreate it");
}
}

现在 AdapetrClass.java

public class KamaDBAdapter
{
protected static final String TAG = "TAG";

private final Context mContext;
private SQLiteDatabase mDb;
private DataBaseHelper mDbHelper;

private static String ACCOUNT_TABLE = "account";
public static String ACCOUNT_EXTRADATA = "extraData";
public static String ACCOUNT_ID = "ID";
public static String ACCOUNT_ADDITIONALDATA = "additionalData";
public static String ACCOUNT_DATA = "data";

public KamaDBAdapter(Context context) 
{
    this.mContext = context;
    mDbHelper = new DataBaseHelper(mContext);
}

public KamaDBAdapter createDatabase() throws SQLException 
{
    try 
    {
        mDbHelper.createDataBase();
    } 
    catch (IOException mIOException) 
    {
        Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
        throw new Error("UnableToCreateDatabase");
    }
    return this;
}

public KamaDBAdapter open() throws SQLException 
{
    try 
    {
        mDbHelper.openDataBase();
        mDbHelper.close();
        mDb = mDbHelper.getReadableDatabase();
    } 
    catch (SQLException mSQLException) 
    {
        Log.e(TAG, mSQLException.toString());
        throw mSQLException;
    }
    return this;
}

public void close() 
{
    mDbHelper.close();
}

public int countAccountData() 
{
    Cursor mCoursor = mDb.query(ACCOUNT_TABLE, new String[] {}, null, null, null, null, null);
    int mReturnedCount = mCoursor.getCount();
    mCoursor.close();
    return mReturnedCount;
}

public long insertData(String mExtra, String mAdditionalData, String mData) 
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(ACCOUNT_EXTRADATA, mExtra);
    initialValues.put(ACCOUNT_ADDITIONALDATA, mAdditionalData);
    initialValues.put(ACCOUNT_DATA, mData);
    return mDb.insert(ACCOUNT_TABLE, null, initialValues);
}

public boolean updateData(int mPosition, String mExtra, String mAdditionalData, String mData) 
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(ACCOUNT_EXTRADATA, mExtra);
    initialValues.put(ACCOUNT_ADDITIONALDATA, mAdditionalData);
    initialValues.put(ACCOUNT_DATA, mData);
    return mDb.update(ACCOUNT_TABLE, initialValues, "ID=" + mPosition, null) > 0;
}

public String retriveData(int mPosition)
{
    Cursor mCursor = mDb.query(ACCOUNT_TABLE, new String[] {ACCOUNT_DATA}, "ID=" + mPosition, null, null, null, null);
    mCursor.moveToFirst();
    String mReturn = mCursor.getString(mCursor.getColumnIndex(ACCOUNT_DATA));
    mCursor.close();
    return mReturn;
}

public String retriveAdditionalData(int mPosition)
{
    Cursor mCursor = mDb.query(ACCOUNT_TABLE, new String[] {ACCOUNT_ADDITIONALDATA}, "ID=" + mPosition, null, null, null, null);
    mCursor.moveToFirst();
    String mReturn = mCursor.getString(mCursor.getColumnIndex(ACCOUNT_ADDITIONALDATA));
    mCursor.close();
    return mReturn;
}

public boolean deleteAccount(int mPosition) 
{
    return mDb.delete(ACCOUNT_TABLE, ACCOUNT_ID + "=" + mPosition, null) > 0;
}
}

现在在您的主类中调用此类: 如下所示:

private static KamaDBAdapter mDbHelper;
mDbHelper = new KamaDBAdapter(Usage.this);
mDbHelper.createDatabase();

现在您的数据库已复制到设备。您可以从本地设备访问放入资产文件夹中的相同数据库。

希望它可以帮助你。

This is called DataBaseHelper.java file

public class DataBaseHelper extends SQLiteOpenHelper
{
private static String TAG = "TAG";
private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
private static String DB_NAME = "ServiceInfo.sqlite";
private SQLiteDatabase mDataBase; 
private final Context mContext;

public DataBaseHelper(Context context) 
{
    super(context, DB_NAME, null, 1);
    DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    this.mContext = context;
}   

public void createDataBase() throws IOException
{
    boolean mDataBaseExist = checkDataBase();
    if(!mDataBaseExist)
    {
        this.getReadableDatabase();
        try 
        {
            copyDataBase();
        } 
        catch (IOException mIOException) 
        {
            throw new Error("ErrorCopyingDataBase");
        }
    }
}

private boolean checkDataBase()
{
    SQLiteDatabase mCheckDataBase = null;
    try
    {
        String myPath = DB_PATH + DB_NAME;
        mCheckDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    }
    catch(SQLiteException mSQLiteException)
    {
        Log.e(TAG, "DatabaseNotFound " + mSQLiteException.toString());
    }

    if(mCheckDataBase != null)
    {
        mCheckDataBase.close();
    }
    return mCheckDataBase != null;
}

private void copyDataBase() throws IOException
{
    InputStream mInput = mContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream mOutput = new FileOutputStream(outFileName);
    byte[] mBuffer = new byte[1024];
    int mLength;
    while ((mLength = mInput.read(mBuffer))>0)
    {
        mOutput.write(mBuffer, 0, mLength);
    }
    mOutput.flush();
    mOutput.close();
    mInput.close();
}

public boolean openDataBase() throws SQLException
{
    String mPath = DB_PATH + DB_NAME;
    mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    return mDataBase != null;
}

@Override
public synchronized void close() 
{
    if(mDataBase != null)
        mDataBase.close();
    super.close();
}

@Override
public void onCreate(SQLiteDatabase db) 
{ }

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
    Log.v(TAG, "UpgradingDatabase, This will drop current database and will recreate it");
}
}

Now AdapetrClass.java

public class KamaDBAdapter
{
protected static final String TAG = "TAG";

private final Context mContext;
private SQLiteDatabase mDb;
private DataBaseHelper mDbHelper;

private static String ACCOUNT_TABLE = "account";
public static String ACCOUNT_EXTRADATA = "extraData";
public static String ACCOUNT_ID = "ID";
public static String ACCOUNT_ADDITIONALDATA = "additionalData";
public static String ACCOUNT_DATA = "data";

public KamaDBAdapter(Context context) 
{
    this.mContext = context;
    mDbHelper = new DataBaseHelper(mContext);
}

public KamaDBAdapter createDatabase() throws SQLException 
{
    try 
    {
        mDbHelper.createDataBase();
    } 
    catch (IOException mIOException) 
    {
        Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
        throw new Error("UnableToCreateDatabase");
    }
    return this;
}

public KamaDBAdapter open() throws SQLException 
{
    try 
    {
        mDbHelper.openDataBase();
        mDbHelper.close();
        mDb = mDbHelper.getReadableDatabase();
    } 
    catch (SQLException mSQLException) 
    {
        Log.e(TAG, mSQLException.toString());
        throw mSQLException;
    }
    return this;
}

public void close() 
{
    mDbHelper.close();
}

public int countAccountData() 
{
    Cursor mCoursor = mDb.query(ACCOUNT_TABLE, new String[] {}, null, null, null, null, null);
    int mReturnedCount = mCoursor.getCount();
    mCoursor.close();
    return mReturnedCount;
}

public long insertData(String mExtra, String mAdditionalData, String mData) 
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(ACCOUNT_EXTRADATA, mExtra);
    initialValues.put(ACCOUNT_ADDITIONALDATA, mAdditionalData);
    initialValues.put(ACCOUNT_DATA, mData);
    return mDb.insert(ACCOUNT_TABLE, null, initialValues);
}

public boolean updateData(int mPosition, String mExtra, String mAdditionalData, String mData) 
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(ACCOUNT_EXTRADATA, mExtra);
    initialValues.put(ACCOUNT_ADDITIONALDATA, mAdditionalData);
    initialValues.put(ACCOUNT_DATA, mData);
    return mDb.update(ACCOUNT_TABLE, initialValues, "ID=" + mPosition, null) > 0;
}

public String retriveData(int mPosition)
{
    Cursor mCursor = mDb.query(ACCOUNT_TABLE, new String[] {ACCOUNT_DATA}, "ID=" + mPosition, null, null, null, null);
    mCursor.moveToFirst();
    String mReturn = mCursor.getString(mCursor.getColumnIndex(ACCOUNT_DATA));
    mCursor.close();
    return mReturn;
}

public String retriveAdditionalData(int mPosition)
{
    Cursor mCursor = mDb.query(ACCOUNT_TABLE, new String[] {ACCOUNT_ADDITIONALDATA}, "ID=" + mPosition, null, null, null, null);
    mCursor.moveToFirst();
    String mReturn = mCursor.getString(mCursor.getColumnIndex(ACCOUNT_ADDITIONALDATA));
    mCursor.close();
    return mReturn;
}

public boolean deleteAccount(int mPosition) 
{
    return mDb.delete(ACCOUNT_TABLE, ACCOUNT_ID + "=" + mPosition, null) > 0;
}
}

Now in your main class call this class: Something like this:

private static KamaDBAdapter mDbHelper;
mDbHelper = new KamaDBAdapter(Usage.this);
mDbHelper.createDatabase();

Now your database copied to device. You can access the same database what you put in Asset folder from your device locally.

Hope it can help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文