我什至遇到了同样的问题,并且对此感到非常沮丧,即使我使用了本教程,但它不起作用。我搜索了很多关于此的线程并找到了一个......甚至这也不起作用......但我做了一个简单的改变。首先,创建两个类DatabaseHelper和CopyAdapter:
数据库助手
public class DatabaseHelper extends SQLiteOpenHelper{
private static String TAG = "TAG";
private static String DB_PATH = "/data/data/PLACE_HERE_YOUR_INTERNAL_PATH/databases/";
private static String DB_NAME = "PLACE_HERE_YOUR_DATABASE_NAME_WITH_EXTENSION";
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();
Log.d(TAG,"create DB in Helper. Data exists?"+mDataBaseExist);
if(!mDataBaseExist)
{
Log.d(TAG,"get Writable in DatabaseHelper");
this.getWritableDatabase();
try
{
Log.d(TAG,"copy Database");
copyDataBase();
}
catch (IOException mIOException)
{Log.d(TAG,"copy not succeed");
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
{
Log.d(TAG,"copy");
InputStream mInput = mContext.getResources().getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
Log.d(TAG,"Output:"+outFileName);
File createOutFile = new File(outFileName);
if(!createOutFile.exists()){
createOutFile.mkdir();
}
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.vTAG, "UpgradingDatabase, This will drop current database and will recreate it");
}
}
复制适配器
public class CopyAdapter {
private final Context mContext;
private SQLiteDatabase mDb;
private DatabaseHelper mDbHelper;
private static String TAG = "TAG";
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 CopyAdapter(Context context)
{
this.mContext = context;
mDbHelper = new DatabaseHelper(mContext);
Log.d(TAG,"done");
}
public CopyAdapter createDatabase() throws SQLException
{
try
{
Log.d(TAG,"create database");
mDbHelper.createDataBase();
}
catch (IOException mIOException)
{
Log.e(TAG, mIOException.toString() + " UnableToCreateDatabase");
throw new Error("UnableToCreateDatabase");
}
return this;
}
public CopyAdapter open() throws SQLException
{
try
{
Log.d(TAG,"Open");
mDbHelper.openDataBase();
mDbHelper.close();
mDb = mDbHelper.getWritableDatabase();
}
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;
}
}
然后只要在需要的地方调用即可:
CopyAdapter mDbHelper = new CopyAdapter(YourActivity.this);
mDbHelper.createDatabase();
并确保您设置
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在你的manifest.xml中
我读过很多变体,例如,将您的数据库文件放入资产文件夹中的另一个文件夹中,或者将您的数据库文件的结尾重命名为 jpg,或者在将数据库文件添加到资产之前将其压缩...忘记这一点。我能够使我的应用程序工作的唯一方法是将数据库文件放在我的资产文件夹中,而不使用文件夹或重命名或压缩。只是纯文件。从那时起,它就没有问题了。
我不知道从哪里得到上面的例子,我再也找不到这个网站了(但它在 stackoverflow 上),但是,通过一些解决方法并将纯数据库文件放入资产文件夹中,这就是解决方案。希望能帮助到你..