问题
Okay, I made a database and i realised that each time i change something in the database i have to uninstall then reinstall the Application :( which is very frustrating... here is my code for my data base hopefully you can help me ! i dont know whats wrong with my code:
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Cook_tab_snacks_data extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Snacks";
public Cook_tab_snacks_data(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS snacks (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT, " +
"disc TEXT, " +
"photo TEXT, " +
"prep TEXT, " +
"thumb TEXT, " +
"ingre TEXT, " +
"howto TEXT, " +
"info TEXT, " +
"snackId INTEGER)";
db.execSQL(sql);
ContentValues values = new ContentValues();
values.put("name", "Name 1");
values.put("disc", "here is the description");
values.put("photo", "stub.png");
values.put("thumb", "stub.png");
values.put("prep", "takes 30 mins");
values.put("ingre", "the ingredients of the snack");
values.put("howto", "how to make this thing");
values.put("info", "basically its this much calorie and such and such");
db.insert("snacks", "name", values);
values.put("name", "Name 2");
values.put("disc", "here is the description");
values.put("photo", "stub.png");
values.put("thumb", "ic_launcher.png");
values.put("prep", "takes 500 mins");
values.put("ingre", "the ingredients of the snack");
values.put("howto", "how to make this thing");
values.put("info", "basically its this much calorie and such and such");
db.insert("snacks", "name", values);
values.put("name", "Name 3");
values.put("disc", "here is the description");
values.put("photo", "stub.png");
values.put("thumb", "stub.png");
values.put("ingre", "the ingredients of the snack");
values.put("howto", "how to make this thing");
values.put("info", "basically its this much calorie and such and such");
db.insert("snacks", "name", values);
values.put("name", "Name 4");
values.put("disc", "here is the description");
values.put("photo", "stub.png");
values.put("ingre", "the ingredients of the snack");
values.put("howto", "how to make this thing");
values.put("info", "basically its this much calorie and such and such");
db.insert("snacks", "name", values);
values.put("name", "Name 5");
values.put("disc", "here is the description");
values.put("photo", "stub.png");
values.put("ingre", "the ingredients of the snack");
values.put("howto", "how to make this thing");
values.put("info", "basically its this much calorie and such and such");
db.insert("snacks", "name", values);
values.put("name", "Name 6");
values.put("disc", "here is the description");
values.put("photo", "stub.png");
values.put("ingre", "the ingredients of the snack");
values.put("howto", "how to make this thing");
values.put("info", "basically its this much calorie and such and such");
db.insert("snacks", "name", values);
values.put("name", "Name 7");
values.put("disc", "here is the description");
values.put("photo", "stub.png");
values.put("ingre", "the ingredients of the snack");
values.put("howto", "how to make this thing");
values.put("info", "basically its this much calorie and such and such");
db.insert("snacks", "name", values);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS snacks");
onCreate(db);
}}
I can't seem to add any item , or edit any item without having to unistall the application then reinstalling :( HELP PLEASE!!! thanks alot!
回答1:
Instead of uninstalling and installing the application again, simply increment the version number of your database to call onUpgrade method.
public Cook_tab_snacks_data(Context context) {
super(context, DATABASE_NAME, null, 2); //previously 1, now 2
}
as in your onUpgrade method, first it will delete the existing table and then will call onCreate method to recreate the table(s)
回答2:
Your onUpgrade function is not getting called because the version of the database is not changing. If you increment the version number every time you make a change to the database, the onUpgrade function will drop it and recreate it.
The last input parameter to the super constructor is the database version number :
public Cook_tab_snacks_data(Context context) {
super(context, DATABASE_NAME, null, 1);
}
回答3:
onCreate
of the SQLiteOpenHelper is called only when the database is created.
If you want to add values to the database, you can do so in an Activity or Service by instantiating your subclass, calling getWritableDatabase()
and then calling insert
in the same way you are doing in onCreate
.
If you want to change the schema, you need to do that in onUpgrade
.
The notepad example is a good place to learn about using a SQLiteDatabase in an app if you need some practice.
回答4:
You have too because the database is created just once and if you want to change anything about it you have to uninstall it.
this is the way things works
来源:https://stackoverflow.com/questions/9727137/updating-sqlite-database-android