开发者

how to create database file(.sqlite) using Sqlite for Android?

i have created table using Sqlite3.and also inserted record in that table.its running successfully.but now i have created another table in same database..and written class for it. now data is not inserted in another table though i have created table in same database and written respective code for it n XML as well. i have given toast mes开发者_如何转开发sage after inserting record.it showing me record inserted successfully at position -1..and showing error like this "table is not exists or file encrypted." and also if i tried to create table using Sqlite cmd then table is created but i am not getting where that file get stored??? can anyone tell me how to solve this problem.? if you want code then will put here..

Thanks in Advance---


It is probably because your application is still working with the old database. If you check for an existing database before creating it, then your app would find the old one and use that. But the old database doesn't have the new table you added.

You should include some sort of versioning of your database and check that when you create your database. Then if the existing database is not up to the latest version, you can overwrite it.

A quick solution is to completely uninstall your application from the phone/emulator and then reinstall/run it, then the database will be up to date.


https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/ use this software for create table..

you can create multiple table, edit data ,operation can perform in this software..


Android uses provides an easy method of database access through custom SQLiteOpenHelper classes. This lets you give your current database a version number which you can increment every time you make a change to a schema.

When you access a database for the first time, it will call the onCreate() method and allow you to construct the database.

When you access the database in the future the SQLiteOpenHelper will check the version of the installed database against the one defined in the project and will call it's onUpgrade() method that provides the old version number. This means that you can provide custom SQL commands to update each legacy version of the database to the current version individually.

If you're just developing, it's easy to increment the number and provide a method like this one.

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS notes");
    onCreate(db);
}

Note, this is taken from the Android SDK Sample Notepad Tutorial which covers this topic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜