在我尝试将数据插入表SQLite时显示错误

时间:2017-11-13 10:44:24

标签: android sqlite android-sqlite

SQLite创建表查询MainActivity

sqLiteHelper.queryData("CREATE TABLE IF NOT EXISTS DETAILS(Id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, phone VARCHAR, location VARCHAR, remarks VARCHAR)");

插入数据查询SQLiteHelper.class

  public void insertData(String name, String phone, String location,String remarks)
{
    SQLiteDatabase database = getWritableDatabase();
    String sql = "INSERT INTO DETAILS VALUES (NULL,?, ?, ?, ?)";
    SQLiteStatement statement = database.compileStatement(sql);
    statement.clearBindings();

    statement.bindString(1, name);
    statement.bindString(2, phone);
    statement.bindString(3, location);
    statement.bindString(4, remarks);

    statement.executeInsert();
}

OnClick Save MainActivity.class

 mSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

           try {
                sqLiteHelper.insertData(
                        eName.getText().toString().trim(),
                        ePhonenumber.getText().toString().trim(),
                        eLocation.getText().toString().trim(),
                        eRemarks.getText().toString().trim());

                Toast.makeText(getApplicationContext(), "Added Successfully", Toast.LENGTH_SHORT).show();
                eName.setText("");
                ePhonenumber.setText("");
                eLocation.setText("");
                eRemarks.setText("");


                }
                catch(Exception e){
                    e.printStackTrace();
                }


            }


    });

将数据插入表格时出错:之前当我没有添加备注列时工作正常

E/SQLiteLog: (1) table DETAILS has 4 columns but 5 values were supplied
W/System.err: android.database.sqlite.SQLiteException: table DETAILS has 4  columns but 5 values were supplied (code 1): , while compiling: INSERT INTO DETAILS VALUES (NULL,?, ?, ?, ?)

W/System.err:     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
W/System.err:     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
W/System.err:     at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1070)
W/System.err:     at com.example.alpha.SQLiteHelper.insertData(SQLiteHelper.java:28)
W/System.err:     at com.example.alpha.MainActivity$1.onClick(MainActivity.java:61)

1 个答案:

答案 0 :(得分:0)

编辑插入查询。

public void insertData(String name, String phone, String location,String remarks)
{
    SQLiteDatabase database = getWritableDatabase();
    String sql = "INSERT INTO DETAILS VALUES (?, ?, ?, ?)";
    SQLiteStatement statement = database.compileStatement(sql);
    statement.clearBindings();

    statement.bindString(1, name);
    statement.bindString(2, phone);
    statement.bindString(3, location);
    statement.bindString(4, remarks);

    statement.executeInsert();
}
相关问题