如何使用Asset文件夹中的SQLite连接。
答案 0 :(得分:6)
这称为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();
现在您的数据库已复制到设备。您可以从本地设备访问属于Asset文件夹的相同数据库。
希望它可以帮到你。