Ormlite转义字符串方法? [英] Ormlite escape string method?

查看:185
本文介绍了Ormlite转义字符串方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,如果我要提供一个字符串:ormlite的转义函数,那么它需要是作为ormlite\的转义函数提供。

  TestDao.queryForFirst(TestDao.queryBuilder()。where()。 stats,stats)
.prepare())

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



如果不是,有什么方法?



谢谢!

解决方案


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


这是一个常见问题。正确的方法是使用 SelectArg 参数,以便SQL可以使用?结构类型这是另一个关于此问题的问题

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

这里是select-arg功能的文档:


http://ormlite.com/docs/select-arg


下面是关于ORMLite用户列表的一些讨论:


http:// groups .google.com / group / ormlite-dev / browse_thread / thread / 71d07202404f97e7



http://groups.google.com/group/ormlite-user/browse_thread/thread/7ced95c474031e3c


编辑:



正如@Moritz指出的,如果你是实际上更新数据库,还可以使用 SelectArg 使用 UpdateBuilder

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


Is there a native way of escaping strings for Ormlite for Android?

For example, if I want to supply a string: ormlite's escape func, it needs to be supplied as ormlite\'s escape func.

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

I tried using UpdateBuilder's escapeValue method, but it only makes the following change: 'ormlite's escape func'. It adds single quotes to beginning and end of the statement. Is there a native support for escaping strings to be sql injection safe?

If not, what are the ways to do it?

Thank you!

解决方案

I tried using UpdateBuilder's escapeValue method, but it only makes the following change: 'ormlite's escape func'. It adds single quotes to beginning and end of the statement. Is there a native support for escaping strings to be sql injection safe?

This is a FAQ. The proper way to do this is to use a SelectArg argument so the SQL can use a ? type of construct. Here's another question talking about this.

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

Here's the documentation on the select-arg functionality:

http://ormlite.com/docs/select-arg

And here's some discussions about this on the ORMLite user list:

http://groups.google.com/group/ormlite-dev/browse_thread/thread/71d07202404f97e7

http://groups.google.com/group/ormlite-user/browse_thread/thread/7ced95c474031e3c

Edit:

As @Moritz points out, if you are actually updating the database, you can also use the SelectArg with the UpdateBuilder:

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

这篇关于Ormlite转义字符串方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆