【问题标题】:Android: store in SQLite a string with the character ' [duplicate]Android:在SQLite中存储一个带有字符'的字符串[重复]
【发布时间】:2012-10-09 19:14:32
【问题描述】:

可能重复:
How to escape special characters like ' in sqlite in android

我正在使用表单将一些值存储在 Android 的 SQLite 数据库中。如果任何字段包含字符 ' 则会发生错误,因此我尝试对其进行转义,但错误仍然存​​在。

这是我的代码:

public boolean createCalendar(String id, String name, String descr) {
    try{
        String newDescr = descr.replace("'", "\\'");
        db.execSQL("INSERT INTO Calendar (id, name, description) VALUES " +
                "('"+id+"','"+name+"','"+newDescr+"');");
       ...
    }
}

这是错误:

sqlite returned: error code = 1, msg = near "siksjs": syntax error, db=/data/data/com.pfc.app/databases/SSDB
android.database.sqlite.SQLiteException: near "siksjs": syntax error: , while compiling: INSERT INTO Calendar (id, name, description) VALUES ('43779fcb-3d8c-650b-b529-50810506375e','kdldlls','ddk \' siksjs');

我应该如何存储它?

谢谢!

【问题讨论】:

  • 使用DatabaseUtils.sqlEscapeString(String)as per this answer。它正是为此而设计的。

标签: android sqlite character store quote


【解决方案1】:

使用

p_query = "select * from mytable where name_field = ?";
mDb.rawQuery(p_query, new String[] { uvalue });

或者

String newDescr = descr.replaceAll("'", "''");
猜你喜欢
  • 1970-01-01
  • 2016-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-15
  • 1970-01-01
相关资源
最近更新 更多