Can't open database-Android

前端 未结 3 1698
感动是毒
感动是毒 2021-01-13 12:22

I\'m developing a simple Android application with SQL. I followed the following guides - http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applicatio

相关标签:
3条回答
  • 2021-01-13 12:52

    I suggest to use an other tutorial. To create a database outside of the devices is "a little bit" wired. I learned it from a book, but i think this tutorial uses the right procedure.

    Edit: Ok, the tutorial is for a special case

    Most all of the Android examples and tutorials out there assume you want to create and populate your database at runtime and not to use and access an independent, preloaded database with your Android application.

    The method I'm going to show you takes your own SQLite database file from the "assets" folder and copies into the system database path of your application so the SQLiteDatabase API can open and access it normally.

    0 讨论(0)
  • 2021-01-13 12:58

    You need to create the database in the onCreate method of your database helper (the extension of SQLiteOpenHelper). Your call to getReadableDatabase() won't return anything until a database has been created in the context of the application.

    db.execSQL("CREATE TABLE " + TABLE_NAME
    + " (_id INTEGER PRIMARY KEY AUTOINCREMENT,"
    + "foo TEXT, bar TEXT);");
    

    Something along those lines. Obviously fill in your own data scheme.

    Then you can read the data from the database you have in your assets folder and insert them into the database using the sqlite commands.

    Here's a tutorial that I used to get started:

    http://www.devx.com/wireless/Article/40842

    0 讨论(0)
  • 2021-01-13 12:59

    Open your database in sqlite database browser and add a new table called "android_metadata",

    you can execute the following SQL statement to do it:

    CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')

    Now insert a single row with the text 'en_US' in the "android_metadata" table:

    INSERT INTO "android_metadata" VALUES ('en_US')

    Then, it is necessary to rename the primary id field of your tables to "_id" so Android will know where to bind the id field of your tables.

    now copy the new database file into your projects assets folder, it will work.....

    0 讨论(0)
提交回复
热议问题