请帮助我,这是我的代码。在启动之前它不会显示任何错误。但 当我运行应用程序 它不显示任何数据。 当我检查数据库文件时,没有表创建 小心帮助我
谢谢&问候 Gurmeet Singh
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SQLiteDatabase myDB= null;
String TableName = "Dictionary";
String Data="";
try {
myDB = this.openOrCreateDatabase("Punjabishabdkosh", MODE_PRIVATE, null);
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ TableName
+ " (Field1 VARCHAR, Field2 VARCHAR);");
myDB.execSQL("INSERT INTO "
+ TableName
+ " (Field1, Field2)"
+ " VALUES ('Gurmeet', Tiwana);");
Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null);
int Column1 = c.getColumnIndex("Field1");
int Column2 = c.getColumnIndex("Field2");
c.moveToFirst();
if (c != null) {
do {
String Name = c.getString(Column1);
int Age = c.getInt(Column2);
Data =Data +Name+"/"+Age+"\n";
}while(c.moveToNext());
}
TextView tv = new TextView(this);
tv.setText(Data);
setContentView(tv);
} catch(Exception e) {
Log.e("Error", "Error", e);
} finally {
if (myDB != null)
myDB.close();
}
}
}
答案 0 :(得分:1)
以下语句包含错误:
myDB.execSQL("INSERT INTO "
+ TableName
+ " (Field1, Field2)"
+ " VALUES ('Gurmeet', Tiwana);");
由于Field1
和Field2
都是VARCHAR
字段,“Tiwana”一词需要用单引号括起来,所以我认为插入失败了,这就是为什么你不要看到任何数据。将语句更改为:
myDB.execSQL("INSERT INTO "
+ TableName
+ " (Field1, Field2)"
+ " VALUES ('Gurmeet', 'Tiwana');");