I am trying to develop android application (first time), so I'm using SQLite to store data. But every time I run the application all data are null! I think the problem is when I run the application it recreates all the tables and they come as new empty tables!
Can anyone help or give example how to check if the tables are already created, and if yes copy the data, if not create and that means it's a first time! How can I store data for a long time by using SQLite? If I cant what is the best way to store data?
//food
private static final String food= "food";
//---- columns -----
private static final String fid = "fid";
private static final String fname= "fname";
private static final String category = "category";
private static final String quantity= "quantity";
private static final String unit= "unit";
private static final String carbs= "carbs";
//food
private static final String DATABASE_CREATE_Food="CREATE TABLE "
+food+" ("+fid
+ " INTEGER PRIMARY KEY AUTOINCREMENT , "
+fname+ " TEXT NOT NULL, "
+category+" TEXT Not NULL, "
+quantity+" Double Not Null, "
+carbs+" Integer NOT NULL, "
+unit+" TEXT NOT NULL"
+");";
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE_Food);
}
/** Called when the DATABASE_VERSION is changed to higher version */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MyAppDB.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS" + food);
onCreate(db);
}