Android SQLite Вставка данных - PullRequest
0 голосов
/ 19 октября 2011

Я пытаюсь вставить некоторые данные в недавно созданную базу данных Android SQLite с помощью этого фрагмента кода, но она не работает.

Вот ошибка:

10-19 19:05:39.793: INFO/Database(413): sqlite returned: error code = 1, msg = near ",": syntax error

Я знаю, что код нуждается в чистке, но я сделаю это позже ... ^^

Вот код:

SQLiteDatabase db = this.sqlHelper.getWritableDatabase();
ContentValues dv = new ContentValues();
dv.put("`createdTime`", h.createdTime);
dv.put("`type`", h.type);
dv.put("`what`", h.what);
dv.put("`where`", h.where);
dv.put("`readableWhat`", h.readableWhat);
dv.put("`readableWhere`", h.readableWhere);
dv.put("`accuracy`", h.accuracy);
dv.put("`isMnemo`", h.isMnemo);
dv.put("`isPlaceCode`", h.isPlaceCode);
dv.put("`isCoords`", h.isCoords);
dv.put("`isNear`", h.isNear);
dv.put("`isEverywhere`", h.isEverywhere);

Cursor c = db.query("HISTORY",
        new String[]{
                "`type`",
                "`what`",
                "`where`",
                "`readableWhat`",
                "`readableWhere`",
                "`accuracy`",
                "`isMnemo`",
                "`isPlaceCode`",
                "`isCoords`",
                "`isNear`",
                "`isEverywhere`",
        },
        "`type`='" + h.type + "'" + "," +
        "`what`='" + h.what + "'" + "," +
        "`where`='" + h.where + "'" + "," +
        "`readableWhat`='" + h.readableWhat + "'" + "," +
        "`readableWhere`='" + h.readableWhere + "'" + "," +
        "`accuracy`='" + h.accuracy  +"'" + "," +
        "`isMnemo`='" + h.isMnemo + "'" + "," +
        "`isPlaceCode`='" + h.isPlaceCode + "'" + "," +
        "`isCoords`='" + h.isCoords + "'" + "," +
        "`isNear`='" + h.isNear + "'" + "," +
        "`isEverywhere`='" + h.isEverywhere + "'"
        ,
        null, null, null, null);
if (!c.moveToFirst())
{
    PJUtils.log("INSERT " + h.readableWhat + " - " + h.readableWhere);
    db.insertOrThrow("HISTORY", "", dv);
}
else
{
    PJUtils.log("UPDATE " + h.readableWhat + " - " + h.readableWhere);
    db.update("CONFIG",
        dv,
        "`type`='" + h.type + "'" + "," +
        "`what`='" + h.what + "'" + "," +
        "`where`='" + h.where + "'" + "," +
        "`readableWhat`='" + h.readableWhat + "'" + "," +
        "`readableWhere`='" + h.readableWhere + "'" + "," +
        "`accuracy`='" + h.accuracy  +"'" + "," +
        "`isMnemo`='" + h.isMnemo + "'" + "," +
        "`isPlaceCode`='" + h.isPlaceCode + "'" + "," +
        "`isCoords`='" + h.isCoords + "'" + "," +
        "`isNear`='" + h.isNear + "'" + "," +
        "`isEverywhere`='" + h.isEverywhere + "'"
        ,
        null);
}
c.close();

Ответы [ 2 ]

2 голосов
/ 19 октября 2011

Вместо

+ "," +

используйте

+ " and " +
0 голосов
/ 22 сентября 2013

Попробуйте по вашему запросу:

            String[] columns = new String[]{
                "type",
                "what",
                "where",
                "readableWhat",
                "readableWhere",
                "accuracy",
                "isMnemo",
                "isPlaceCode",
                "isCoords",
                "isNear",
                "isEverywhere" };
        String selection = "type=? AND what=? AND where=? AND readableWhat=? AND readableWhere=? AND accuracy=? AND isMnemo=? AND isPlaceCode=? AND isCoords=? AND isNear=? AND isEverywhere=?";
        String[] selectionArgs = new String[]{
                h.type,
                h.what,
                h.where,
                h.readableWhat,
                h.readableWhere,
                h.accuracy,
                h.isMnemo,
                h.isPlaceCode,
                h.isCoords,
                h.isNear,
                h.isEverywhere
        };
        Cursor c = db.query("HISTORY", columns, selection, selectionArgs, null, null, null);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...