我正在编写一个简单的代码,试图了解如何在data / data / getPackageName()/ databases文件夹中创建数据库文件。代码如下:
package com.package.test;
import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteDatabase db = openOrCreateDatabase("MYDB",MODE_PRIVATE,null);
db .execSQL("CREATE TABLE IF NOT EXISTS MyTable (LastName VARCHAR, FirstName VARCHAR, AGE INT(3));");
db.execSQL("INSERT INTO MyTables VALUES ('Brimelow', 'lee', 37);");
db.close();
}
}
问题是,当我尝试运行上面的代码时,我会收到以下错误:
12-18 10:35:21.568: I/Database(2090): sqlite returned: error code = 1, msg = no such table: MyTables
12-18 10:35:21.568: E/Database(2090): Failure 1 (no such table: MyTables) on 0x125ef0 when preparing 'INSERT INTO MyTables VALUES ('Brimelow', 'lee', 37);'.
12-18 10:35:21.568: W/dalvikvm(2090): threadid=1: thread exiting with uncaught exception (group=0x4001d890)
12-18 10:35:21.583: E/AndroidRuntime(2090): FATAL EXCEPTION: main
12-18 10:35:21.583: E/AndroidRuntime(2090): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.package.test/com.package.test.MainActivity}: android.database.sqlite.SQLiteException: no such table: MyTables: INSERT INTO MyTables VALUES ('Brimelow', 'lee', 37);
同样在Eclipse中,当我在DDMS视图中的文件浏览器中搜索时,我似乎无法在“data”文件夹中找到任何内容。我知道数据库表无法创建,但我没有看到问题的来源。有人可以帮忙吗?
感谢。
答案 0 :(得分:3)
您正在创建“MyTable”表,但在“MyTables”
中插入您的值将其更改为:
db.execSQL("INSERT INTO MyTable VALUES ('Brimelow', 'lee', 37);");