2

我想插入现有应用程序数据表的应用程序类别。我不知道我做的是否正确,但在我做的事情中我也得到了这个错误。

我应该如何正确地做到这一点?

主要活动

    AlertDialog.Builder builder = new AlertDialog.Builder(
            MainActivity.this);
    builder.setTitle("Please Select Category of : " + AppName); 
    builder.setItems(shareItems,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {
                    // TODO Auto-generated method stub
                    String itemz = String.valueOf(shareItems[item]);
                    mySQLiteAdapter.insertcategory(AppName, itemz);
                }
            });
    AlertDialog shareAlert = builder.create();
    shareAlert.show();

处理程序

 public long insertcategory(String AppName, String itemz) {
            // TODO Auto-generated method stub
            Cursor crack = sqlitedb.rawQuery("INSERT INTO " + KEY_CATEGORY
                    + " VALUES ( ' " + itemz + " ' ) WHERE " + KEY_NAME
                    + " LIKE ?", new String[] { "%" + AppName+ "%" });
            return 0;
        }

更新的查询 这是我用来更新表的。itemz 将值设为“游戏”,但未更新

public long insertcategory(String AppName, String itemz) {
        // TODO Auto-generated method stub

        ContentValues values = new ContentValues();
        values.put(KEY_CATEGORY, itemz);
        return sqlitedb.update(MYDATABASE_TABLE, values, KEY_NAME + " LIKE ?",
                new String[] { "%" + AppName + "%" });

    }

表创建为

private static final String SCRIPT_CREATE_DATABASE = "create table "
            + MYDATABASE_TABLE + " (" + KEY_ID
            + " integer primary key autoincrement, " + KEY_NAME
            + " text not null, " + KEY_PACK + " text not null, " + KEY_PERM
            + " text not null, " + KEY_LEVEL + " text not null, "
            + KEY_CATEGORY + " text);";
4

3 回答 3

7

The INSERT command does not have a WHERE clause because it inserts a completely new record.

It appears you want to set/change the value in an existing record. This would be done with an UPDATE command:

UPDATE MyTable SET category = 'Entertainment' WHERE name LIKE ?
于 2013-02-27T13:55:45.257 回答
1

You can't use INSERT INTO then use WHERE clause.

INSERT INTO means you're adding a new row in a table, which doesn't exist before. You can use WHERE on UPDATE and SELECT statement but not on INSERT INTO.

I'm guessing you mean UPDATE instead of INSERT INTO:

Try this:

public long insertcategory(String AppName, String itemz) {
            // TODO Auto-generated method stub
            Cursor crack = sqlitedb.rawQuery("UPDATE " + KEY_CATEGORY
                    + " SET yourFieldNameHere = ' " + itemz + " '  WHERE " + KEY_NAME
                    + " LIKE ?", new String[] { "%" + AppName+ "%" });
            return 0;
        }

For insert, try this:

   public long insertcategory(String AppName, String itemz) {
                // TODO Auto-generated method stub
                Cursor crack = sqlitedb.rawQuery("INSERT INTO " + KEY_CATEGORY
                        + " VALUES(' " + itemz + " ')" });
                return 0;
            }

Change yourFieldNameHere with the name of your table's field.

于 2013-02-27T13:56:00.217 回答
0

您不能在 INSERT INTO 中使用 WHERE。如果使用 INSERT 将添加一个新行。如果您需要更新 KEY_NAME LIKE "%AppName% 使用 UPADATE 的行

于 2013-02-27T14:08:25.313 回答