【问题标题】:Showing error while i am trying to insert data to table SQLite尝试将数据插入表 SQLite 时显示错误
【发布时间】:2018-04-26 00:36:30
【问题描述】:

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 保存 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)

【问题讨论】:

  • 您的表只有 4 列,其中一列是 Id (autoincrement)
  • 编辑您的插入查询
  • 我不知道编辑查询出了什么问题..你能告诉我吗?
  • 看起来您在管理 sqlite 架构时遇到了问题。发布 sqliteopenhelper 生命周期回调(onCreate() 等)。 sqLiteHelper.queryData("CREATE TABLE IF NOT EXISTS ...") 不是使用 sqlite open helper 管理架构的正确方法。
  • 我没有得到你@laalto

标签: android sqlite android-sqlite


【解决方案1】:

编辑插入查询。

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();
}

【讨论】:

  • 如果我删除 Null 将如何添加 id.. 仍然尝试这个我得到 E/SQLiteLog: (20) 语句在 5 时中止:[INSERT INTO DETAILS VALUES (?, ?, ? , ?)] 数据类型不匹配
  • INSERT INTO DETAILS (name,phone,location,remarks) VALUES (?, ?, ?, ?) 试试这个。
猜你喜欢
  • 1970-01-01
  • 2011-08-05
  • 1970-01-01
  • 2011-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多