我正在尝试开发 android 应用程序(第一次),所以我使用 SQLite 来存储数据。但是每次我运行应用程序时,所有数据都是空的!我认为问题在于当我运行应用程序时,它会重新创建所有表,并且它们以新的空表的形式出现!
任何人都可以帮助或举例说明如何检查表是否已经创建,如果是则复制数据,如果没有创建,这意味着这是第一次!如何使用 SQLite 长时间存储数据?如果我不能存储数据的最佳方式是什么?
//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);
}