SQLite:如何在INSERT INTO上修复语法?

时间:2016-05-11 05:23:48

标签: android sqlite android-sqlite insert-into

我有一个CARDNUM列,我试图在名为TOTALCOUNT的表中增加+1。应用程序因“SET”

附近的语法错误而崩溃

这是我的INSERT声明:

db.execSQL("INSERT INTO "+ DBContract.DBEntry.TABLE_NAME_TOTALCOUNT+" SET "+ DBContract.DBEntry.COLUMN_NAME_CARDNUM + "="+ DBContract.DBEntry.COLUMN_NAME_CARDNUM+"+1");

我不对的是什么?

private static final String SQL_CREATE_CARDNUM =
            "CREATE TABLE IF NOT EXISTS "+ DBContract.DBEntry.TABLE_NAME_TOTALCOUNT +
            "( "+ DBContract.DBEntry.COLUMN_NAME_COUNTID +
            " INTEGER PRIMARY KEY UNIQUE , "+
            DBContract.DBEntry.COLUMN_NAME_CARDNUM +
            " INTEGER DEFAULT 1);";


// Push the database data to the RecyclerView
public List<UserData> getDataFromDB(){
    List<UserData> modelList = new ArrayList<>();        
    String query = "SELECT *,(SELECT " + DBContract.DBEntry.COLUMN_NAME_CARDNUM +
            " from " + DBContract.DBEntry.TABLE_NAME_TOTALCOUNT +") from "
            + DBContract.DBEntry.TABLE_NAME_USERINPUTS +";";

    SQLiteDatabase db = this.getReadableDatabase();

    db.beginTransaction();

    Cursor cursor = db.rawQuery(query, null);

        try {
            if (cursor.moveToFirst()) {
                do {
                    UserData userData = new UserData();
                    userData.setTodo(cursor.getString(1));
                    userData.setNote1(cursor.getString(2));
                    userData.setNote2(cursor.getString(3));
                    userData.setDuedate(cursor.getString(4));
                    userData.setDuetime(cursor.getString(5));
                    userData.setTimestamp(cursor.getLong6));
                    userData.setCardnum(cursor.getInt(7));

                    modelList.add(0, userData);
                    } while (cursor.moveToNext());
            }
        } finally {
              if(cursor !=null && !cursor.isClosed()){
              cursor.close();
              }
          }
    db.setTransactionSuccessful();
    db.endTransaction();
    return modelList;
}

Logard for CARDNUM显示“(从totalcount中选择cardnum)= 0。

3 个答案:

答案 0 :(得分:1)

INSERT的语法不正确。

请参阅以下内容:

http://www.sqlitetutorial.net/sqlite-insert http://www.techonthenet.com/sqlite/insert.php

例如:

INSERT INTO table1 (
 column1,
 column2 ,..)
VALUES
 (
 value1,
 value2 ,...);

答案 1 :(得分:0)

试试这个

db.execSQL("UPDATE "+ DBContract.DBEntry.TABLE_NAME_TOTALCOUNT+" SET "+ DBContract.DBEntry.COLUMN_NAME_CARDNUM + "="+ DBContract.DBEntry.COLUMN_NAME_CARDNUM+"+1");

建议使用id设置WHERE以更新特定行。

答案 2 :(得分:0)

您正尝试使用无法完成的 INSERT 增加列。但是,如果您仍想使用 INSERT 语句,则无法在 INSERT 中使用 SET INSERT 操作应如下所示:

INSERT INTO TABLE_NAME (cloumn1_name, column2_name, column3_name) VALUES (column1_value,cloumn2_value,column3_value)

您无法通过 INSERT 语句更新值。如果要更新值,则必须使用 UPDATE 语句。在这种情况下, UPDATE 语句如下所示:

UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition];