2012-10-06 37 views
0

我創建一個簡單的表,但在執行表例外,它拋出這樣的錯誤:sqlite的罰球上創建表

 


    10-06 09:15:28.606: E/AndroidRuntime(595): 
    android.database.sqlite.SQLiteException: near "item": error: 
    create table item (_id integer primary key autoincrement, 
    item_id text not null, 
    item_name text not null); 

 

我dbhelper類是: -

<code> 

公衆類DbHelper延伸SQLiteOpenHelper {

private static final String CREATE_ITEM_TABLE = String.format("CREATE TABLE %s (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s TEXT NOT NULL, %s TEXT NOT NULL);", 
       "item","_id", "item_id", "item_name"); 
private static final String DATABASE_NAME = "applicationdata"; 
private static final int DATABASE_VERSION = 1; 

public DbHelper(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

// Method is called during creation of the database 
@Override 
public void onCreate(SQLiteDatabase database) { 
    database.execSQL(CREATE_ITEM_TABLE); 
} 

// Method is called during an upgrade of the database, 
// e.g. if you increase the database version 
@Override 
public void onUpgrade(SQLiteDatabase database, int oldVersion, 
     int newVersion) { 
    Log.w(DbHelper.class.getName(), "Upgrading database from version " 
      + oldVersion + " to " + newVersion 
      + ", which will destroy all old data"); 
    database.execSQL("DROP TABLE IF EXISTS item"); 
    onCreate(database); 
} 

}

</code> 

請幫幫我。

+0

發佈您的創建表查詢代碼。 –

+1

看到這個問題:http://stackoverflow.com/questions/12488718/android-database-sqlite-sqliteexception-near-syntax-error –

+0

看到我的答案在我的情況下工作.. –

回答

1
db.execSQL("CREATE TABLE if not exists item(id INTEGER PRIMARY KEY AUTOINCREMENT," 
      + "item_id TEXT NOT NULL," 
      + "item_name TEXT NOT NULL)"); 
+0

我也試過了...它的不工作..我看到創建語句在調試它是完美的,但無法找出什麼hppening –

+0

嘗試卸載您的應用程序,並再次運行,如果你創建表已存在(表存在後第一次運行)沒有'如果不存在'你的陳述將失敗。 –

+0

修復了錯誤的答案,請重試 –

1

好,「項目」是不是在SQLite中的keyword list,但是當我看到這樣的錯誤之前拿出這...

試着改變你的表名別的東西看看是否能解決你的問題。

1

也許一些空間問題(AUTOINCREMENT前)和語法:

private static final String CREATE_ITEM_TABLE = String.format("CREATE TABLE %s (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s TEXT NOT NULL, %s TEXT NOT NULL);", 
      "item","_id", "item_id", "item_name"); 

或者只是:

private static final String CREATE_ITEM_TABLE = String.format("CREATE TABLE %s (%s INTEGER PRIMARY KEY, %s TEXT NOT NULL, %s TEXT NOT NULL);", 
      "item","_id", "item_id", "item_name"); 

爲AUTOINCREMENT關鍵字不符合 「INTEGER PRIMARY KEY」 以及必要的Sqlite