我使用以下代码使用Sqlite创建数据库。我已将自动增量设置为列。当我尝试向表中插入值时,它会给出错误。
db.execSQL("CREATE TABLE IF NOT EXISTS "
+ SAMPLE_TABLE_NAME
+ " (user_id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR,age VARCHAR, zipcode VARCHAR,gender VARCHAR,isstored VARCHAR);");
db.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ("
+ "\"" + name + "\"," + "\"" + age
+ "\",\"" + zip + "\",\"" + gender + "\", \""
+ "ok" + "\");");
错误:
09-28 09:44:54.545: E/AndroidRuntime(3825): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.readometer/com.readometer.ReadometerSplash}:
android.database.sqlite.SQLiteException: table USERDETAILS has 6 columns but 5 values were supplied (code 1): , while compiling: INSERT INTO USERDETAILS Values ("kumar","23","1234","M", "ok");
答案 0 :(得分:1)
当您尝试插入时,应以这种方式插入。 SQlite框架将自动将自动递增的值插入user_id列。
INSERT INTO test.authors (username,age, zipcode,gender,isstored) VALUES ("kumar","23","1234","M", "ok");
答案 1 :(得分:1)
您应该做一些更干净的事情,首先将所有列属性声明为常量,然后使用以下代码在数据库中进行正确的插入:
ContentValues values = new ContentValues();
values.put(USERNAME, name);
values.put(AGE, age
values.put(ZIPCODE, zip);
values.put(GENDER, gender);
values.put(STORED, ok);
db.insert(SAMPLE_TABLE_NAME, null, values);
答案 2 :(得分:1)
试试这段代码:
SQLiteDatabase s =getWritableDatabase();
s.execSQL( "INSERT INTO requestmsg (username ,age , zipcode ,gender ,isstored ) " + "VALUES ('" + name + "', '" + age+ "','" + zip+ "','"+gender+"','ok')");
答案 3 :(得分:0)
试试如下:
sql = "INSERT INTO "+ SAMPLE_TABLE_NAME + "( username,age,zipcode,gender,isstored) " + "VALUES ('" + name + "', '" + age + "','" + zip+ "' ,'" +gender + "' ,'"ok"' )"; db.execSQL(sql);