上一个问题:我正在关注SQLite教程,现在我想再向表中添加两列,但现在有错误吗?这里有什么问题?请帮忙。
logcat的:
08-10 15:13:29.077: E/SQLiteLog(17055): (1) table country has no column named add
08-10 15:13:29.082: E/SQLiteDatabase(17055): Error inserting add=Feg dest=Fhd code=5495 cap=Egd name=A
08-10 15:13:29.082: E/SQLiteDatabase(17055): android.database.sqlite.SQLiteException: table country has no column named add (code 1): , while compiling: INSERT INTO country(add,dest,code,cap,name) VALUES (?,?,?,?,?)
08-10 15:13:29.082: E/SQLiteDatabase(17055): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-10 15:13:29.082: E/SQLiteDatabase(17055): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1012)
我的SQLite java类
@Override
public void onCreate(SQLiteDatabase db) {
String createQuery = "CREATE TABLE country (_id integer primary key autoincrement,name, cap, code, dest, add);";
db.execSQL(createQuery);
}
答案 0 :(得分:2)
请更改此
db.execSQL("CREATE TABLE if not exists countryname(id INTEGER PRIMARY KEY AUTOINCREMENT," + " name" + " TEXT ," + " cape" + " TEXT," + " code" + " TEXT," + " dest" + " TEXT," + " mem" +" TEXT);");
到
db.execSQL("CREATE TABLE if not exists countryname(id INTEGER PRIMARY KEY AUTOINCREMENT," + " name" + " TEXT ," + " cape" + " TEXT," + " code" + " TEXT," + " dest" + " TEXT," + " mem" +" TEXT)");
你在TEXT之后有; ..然后请删除你的旧数据库并再次尝试它确实有效。
答案 1 :(得分:1)
您不能使用任何保留关键字作为列名。 add是一个保留关键字,它不能用作列名,除非你引用它像'add',“add”,add
或将它包装在像[add]这样的括号中。
答案 2 :(得分:0)
可能是因为“添加”是SQLite reserved word。您需要引用它,以表明它是列名。
INSERT INTO country(dest,code,[add],cap,name)
在链接中,您实际上可以使用“添加”,“添加”,[添加]或add
中的任何一种。