-1

请帮助我这里是我的代码。在启动之前它不会显示任何错误。但是当我运行应用程序时,它不显示任何数据。当我检查数据库文件时,没有表 Created Kidly 帮帮我

感谢和问候 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();
        }
    }
}
4

1 回答 1

1

以下语句包含错误:

myDB.execSQL("INSERT INTO "
                 + TableName
                 + " (Field1, Field2)"
                 + " VALUES ('Gurmeet', Tiwana);");

由于Field1Field2都是VARCHAR字段,“Tiwana”这个词需要用单引号括起来,所以我认为插入失败,这就是你看不到任何数据的原因。将语句更改为:

myDB.execSQL("INSERT INTO "
                 + TableName
                 + " (Field1, Field2)"
                 + " VALUES ('Gurmeet', 'Tiwana');");
于 2013-10-19T11:51:08.217 回答