【问题标题】:Ormlite escape string method?Ormlite转义字符串方法?
【发布时间】:2011-06-19 05:38:04
【问题描述】:

Android 版 Ormlite 是否有本地转义字符串的方法?

例如,如果我想提供一个字符串:ormlite's escape func,它需要作为 ormlite\'s escape func 提供。

TestDao.queryForFirst(TestDao.queryBuilder().where().like("stats", stats)
    .prepare())

我尝试使用 UpdateBuilder 的 escapeValue 方法,但它只进行了以下更改: 'ormlite 的转义函数'。它在语句的开头和结尾添加单引号。是否有原生支持转义字符串以保证 sql 注入安全?

如果没有,有什么方法可以做到?

谢谢!

【问题讨论】:

  • like()-Method 中的第一个字符串可能是潜在的注入安全漏洞吗?

标签: android sql string escaping ormlite


【解决方案1】:

我尝试使用 UpdateBuilder 的 escapeValue 方法,但它只进行了以下更改:'ormlite's escape func'。它在语句的开头和结尾添加单引号。是否有原生支持转义字符串以保证 sql 注入安全?

这是一个常见问题解答。执行此操作的正确方法是使用 SelectArg 参数,以便 SQL 可以使用 ?构造类型。这是另一个question talking about this

SelectArg selectArg = new SelectArg(stats);
TestDao.queryForFirst(
    TestDao.queryBuilder().where().like("stats", selectArg).prepare());

这是select-arg functionality 上的文档。

编辑:

正如@Moritz 指出的那样,如果您实际上是在更新数据库,您还可以将SelectArgUpdateBuilder 一起使用:

SelectArg arg = new SelectArg("Some value");
updateBuilder.updateColumnValue(MY_COLUMN, arg);

【讨论】:

  • 顺便说一句,这也适用于更新语句:SelectArg arg = new SelectArg(); arg.setValue("Some value"); updateBuilder.updateColumnValue(MY_COLUMN, arg);
猜你喜欢
  • 2013-07-05
  • 1970-01-01
  • 1970-01-01
  • 2012-09-14
  • 2014-04-27
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
相关资源
最近更新 更多