无法读取设备上部署的 SQLite 数据库
我正在 Android 上开发一个应用程序,使用我预先创建的嵌入式 sqlite 数据库。 在我的设备(HTC 野火)上部署应用程序时,我从资产文件夹中检索现有数据库并读取数据库中的数据。此时一切都正常了。 我面临的问题是我在数据库上执行的所有操作(例如添加、编辑或删除)都不起作用。数据库似乎处于只读模式,但选择操作工作正常,并且我显示了我的列表。 拜托,我需要你的建议!
请参阅下面我用来创建数据库的代码:
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);
}
}
问候。
I am developing an application on android, using an embedded sqlite database that I have prealably created.
While deploying the application on my device (an HTC wildfire), I retrieve the existing database from the assets folder and read the data in the database. At this point everything is alright.
The issue I am facing is about the fact that all the opération I am doing on the database like Add, Edit or Remove doesn't work. It seems like the database is in readonly mode but the select operation is working fine and I get my list displayed.
Please, I need your advise!
See below the code I am using to create the database:
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);
}
}
Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将数据库复制到 /data/data/packagename 文件夹,请参阅
you need to copy the database to the /data/data/packagename folder, see
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
也许您忘记提交数据库操作?
示例: http://www.higherpass.com/ Android/教程/使用 Android 光标访问数据/3/
Maybe you are forgetting to commit your Database Operations?
Example: http://www.higherpass.com/Android/Tutorials/Accessing-Data-With-Android-Cursors/3/