【发布时间】:2015-03-06 18:52:31
【问题描述】:
我正在使用 SQLite 在 android 中创建一个数据库。
我编写代码以在 onCreate() 方法中创建一个名为 products 的表。
然后当我调用 add() 方法向表中添加一些值时,我收到一个错误,即没有名为 products 的表。
这是我的 SQLiteHelper 类:
public class MySQLiteHelper extends SQLiteOpenHelper {
//variable declarations and some code
...
@Override
public void onCreate(SQLiteDatabase db) {
// SQL statement to create a products table
String CREATE_PRODUCTS_TABLE = "CREATE TABLE "+TABLE_NAME+ " ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"url TEXT, " +
"title TEXT, " +
"price INTEGER );";
// create products table
db.execSQL(CREATE_PRODUCTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(db);
}
void add(String url, String title, String price) {
this.url = url;
this.title = title;
this.price = price;
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put(KEY_URL, url);
values.put(KEY_TITLE, title);
values.put(KEY_PRICE, price);
// 3. insert
db.insert(TABLE_NAME, // table
null, //nullColumnHack
values); // key/value -> keys = column names/ values = column values
// 4. close
db.close();
}
我从一个类似的问题中读到 onCreate() 不是构造函数,它仅在数据库不存在时调用,但在我的情况下数据库也不存在,那么为什么不调用它呢?
我还了解到我们需要致电getWritableDatabase() 或getReadableDatabase()
但我不确定在我的代码中的何处进行此类调用。
我尝试将onCreate() 中的“表创建代码”放入我的add() 方法中。第一次运行时,我什么也没得到,但从下一次开始,我不断收到“表已存在”的错误。
现在,在调用add() 方法插入一些值之前,如何确保只正确创建表一次。有什么帮助吗?
编辑:
我的 Logcat 显示:
03-07 00:59:22.684 2107-2147/com.example.nikhil.amazon1 W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-07 00:59:22.684 2107-2147/com.example.nikhil.amazon1 W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5683060, error=EGL_SUCCESS
03-07 00:59:24.922 2107-2120/com.example.nikhil.amazon1 I/art﹕ Background sticky concurrent mark sweep GC freed 1464(89KB) AllocSpace objects, 7(2MB) LOS objects, 7% free, 9MB/9MB, paused 75.596ms total 203.616ms
03-07 00:59:25.338 2107-2413/com.example.nikhil.amazon1 E/SQLiteLog﹕ (1) no such table: products
03-07 00:59:25.339 2107-2413/com.example.nikhil.amazon1 E/SQLiteDatabase﹕ Error inserting price= 24,599.00 title=Google Nexus 5 D821 (16GB, Black) url=http://www.amazon.in/Google-Nexus-D821-16GB-Black/dp/B00GC1J55C/ref=sr_1_1?s=electronics&ie=UTF8&qid=1421161258&sr=1-1&keywords=Google
android.database.sqlite.SQLiteException: no such table: products (code 1): , while compiling: INSERT INTO products(price,title,url) VALUES (?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at com.example.nikhil.amazon1.MySQLiteHelper.add(MySQLiteHelper.java:86)
at com.example.nikhil.amazon1.FirstParsing$1.run(FirstParsing.java:71)
at java.lang.Thread.run(Thread.java:818)
我的问题与以下内容完全相同: 1)Android SQLiteOpenHelper : onCreate() method is not called. Why?
2) SQLiteOpenHelper failing to call onCreate?
3)SQLiteOpenHelper "onCreate" is not called? (the DB does not exist)
但我发现很难理解它并将其应用到我的代码中。
【问题讨论】:
-
你的代码是
"CREATE TABLE TABLE_NAME ( "。不应该是"CREATE TABLE "+TABLE_NAME+" ( "吗? -
哦!实际上我有一个名为 TABLE_NAME 的变量。为了避免复杂性,我没有将整个代码放在这里。
-
是的,但是您的代码正在创建一个名为“TABLE_NAME”的表,而不是“products”
-
我想我没有解释或者我不明白你,对不起...我的意思是
"CREATE TABLE TABLE_NAME ( "正在创建一个名为 TABLE_NAME 的表。你的代码应该是"CREATE TABLE "+TABLE_NAME+" ( "使用你的private static final String TABLE_NAME = "products"; -
我假设数据库文件仍然存在并且它当前的版本号为
1,这意味着你的onUpgrade方法永远不会被调用,因为数据库已经是最新版本。删除数据库文件。您可以通过在您的设备上卸载您的应用程序或清除您的应用程序的存储空间来做到这一点。您还可以增加数据库版本代码。但是,您更新的表创建代码现在有语法错误。您需要在字符串"CREATE TABLE"的末尾添加一个空格字符,否则生成的SQL 代码如下所示:CREATE TABLEproducts...。
标签: java android android-sqlite oncreate sqliteopenhelper