无法读取设备上部署的 SQLite 数据库

发布于 2024-12-22 10:33:15 字数 1949 浏览 0 评论 0原文

我正在 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 技术交流群。

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

发布评论

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

评论(2

狼性发作 2024-12-29 10:33:15

您需要将数据库复制到 /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/

迷爱 2024-12-29 10:33:15

Maybe you are forgetting to commit your Database Operations?
Example: http://www.higherpass.com/Android/Tutorials/Accessing-Data-With-Android-Cursors/3/

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