我按如下方式创建表格:
_id
但是当我尝试插入用户时,我得到以下异常:
android.database.sqlite.SQLiteException:表用户有3列,但提供了2个值(代码1):,同时编译:INSERT INTO用户VALUES(' testuser',' textpassword&# 39)
由于AUTOINCREMENT
是主键,它为什么期待它?我也试过GridView
,但它没有用。
答案 0 :(得分:3)
试试这个
@Override
public void onCreate(SQLiteDatabase db) {
String sql =
"CREATE TABLE IF NOT EXISTS users (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
"name TEXT NOT NULL, " +
"password TEXT NOT NULL);";
db.execSQL(sql);
sql = "INSERT INTO users(name, password) VALUES ('testuser', 'textpassword')";
db.execSQL(sql);
}
AUTOINCREMENT NOT NULL is missing in your insert query which will increase the value of _id field automatically.
答案 1 :(得分:2)
您应该指定要插入的值:
sql = "INSERT INTO users(name, password) VALUES ('testuser', 'textpassword')";