sqlite在创建表时抛出异常

时间:2012-10-06 08:48:50

标签: android android-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>

public class 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>

请帮帮我。

3 个答案:

答案 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)");

答案 1 :(得分:1)

好吧,“item”不在SQLite的keyword list中,但是当我看到之前出现的错误时......

尝试将您的表名更改为其他内容,看看是否能解决您的问题。

答案 2 :(得分: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");

因为Sqlite

中的“INTEGER PRIMARY KEY”不需要AUTOINCREMENT关键字