我正在使用我已预先创建的嵌入式sqlite数据库在android上开发应用程序。 在我的设备(HTC Wildfire)上部署应用程序时,我从assets文件夹中检索现有数据库并读取数据库中的数据。在这一点上一切都很好。 我面临的问题是关于我在数据库上进行的所有操作(如添加,编辑或删除)不起作用的事实。这似乎是数据库处于只读模式,但选择操作正常,我得到我的列表显示。 请,我需要你的建议!无法读取部署在设备上的SQLite数据库
请参见下面的代码,我使用创建数据库:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mydb.sqlite";
private static final int DATABASE_VERSION = 1;
private static String DATABASE_PATH = "";
public DatabaseHelper(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
try {
copyDataBase(context);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DATABASE_PATH = context.getDatabasePath(DATABASE_NAME).getAbsolutePath();
}
private void copyDataBase(Context context) throws IOException{
//Open your local db as the input stream
InputStream input = context.getAssets().open(DATABASE_NAME);
//Open the empty db as the output stream
OutputStream output = new FileOutputStream(DATABASE_PATH);
//transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer))>0){
output.write(buffer, 0, length);
}
//Close the streams
output.flush();
output.close();
input.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
db = SQLiteDatabase.openDatabase(DATABASE_PATH, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(DatabaseHelper.class.getName(), "New version : " + newVersion);
db.execSQL("DROP TABLE IF EXISTS person");
db.execSQL("DROP TABLE IF EXISTS destiny");
db.execSQL("DROP TABLE IF EXISTS intention");
db.execSQL("DROP TABLE IF EXISTS innerself");
onCreate(db);
}
}
问候。
有什么错误?你能证明你如何检索数据库实例吗?你使用getWritableDatabase()或getReadableDatabase()? – hovanessyan
我正在使用getWritableDatabase()函数。 –