[英]insert special character on SQLite with FMDB
I have an issue with inserting a value into table by FMDB. 我在通过FMDB将值插入表中时遇到问题。 I have SQL string like this: 我有这样的SQL字符串:
NSString *sql = [NSString stringWithFormat:@"INSERT INTO table( COMPANY, PAGE_NO ) VALUES ('%@',%d,%d)",value1,value2,value3];
And I use FMDB like this for SQL string above: 我在上面的SQL字符串中使用FMDB这样的代码:
[FMDatabase: 0x433e80> executeUpdate: sql]
It works, but when I use value1 = @"Test'12"
. 它有效,但是当我使用value1 = @"Test'12"
。 It has a character " ' "
and it fails. 它包含字符" ' "
,但失败。
Please help me, I want to keep using the method executeUpdate
from FMDB 请帮助我,我想继续使用FMDB中的executeUpdate
方法
Thanks 谢谢
Looks like you're not passing the correct number of parameters to your query, you're listing 2 columns and passing 3 parameters. 看起来您没有向查询传递正确数量的参数,您列出了2列并传递了3个参数。 I'll assume that's a typo. 我认为这是一个错字。
You should really parameterise your query; 您应该真正将查询参数化; use ?
使用?
where the parameters should go and instead pass them to executeUpdate
. 参数应该放在哪里,然后将其传递给executeUpdate
。 That way, problematic characters will be handled automatically. 这样,有问题的字符将被自动处理。
That would change your code to something more like; 那会将您的代码更改为更类似的内容;
NSString *sql = @"INSERT INTO table(COMPANY, PAGE_NO) VALUES (?,?)";
[database executeUpdate:sql, value1, [NSNumber numberWithInt:value2], nil];
Note the call to numberWithInt
that is required to convert int
to an NSNumber
, since executeUpdate
requires all its arguments to be objects, not primitive types. 注意将int
转换为NSNumber
所需的对numberWithInt
的调用,因为executeUpdate
要求其所有参数都是对象,而不是原始类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.