我使用“通过字符串连接的组件”加入了三个对象。 joinString的NSLog给了我这个:
joined string is (
"722.3300",
"4924.6700",
"225.38"
)
当我将其添加到数组时,它显示如下:
array objects are:
{
"obj1",
"obj2",
"(\n \"722.3300\",\n \"4924.6700\",\n \"225.38\"\n)",
"obj4"
}
我尝试将这些值插入到我的sqlite
中 insertString=[NSString stringWithFormat:@"INSERT INTO MY_T (field1) VALUES (\"%@\")",[array objectatIndex:0]]; //this executes perfectly
insertString=[NSString stringWithFormat:@"INSERT INTO MY_T (field1) VALUES (\"%@\")",[array objectatIndex:1]]; //this executes perfectly
insertString=[NSString stringWithFormat:@"INSERT INTO MY_T (field1) VALUES (\"%@\")",[array objectatIndex:2]]; //oops...somethings wrong here
insertString=[NSString stringWithFormat:@"INSERT INTO MY_T (field1) VALUES (\"%@\")",[array objectatIndex:3]]; //this executes perfectly
有没有其他方法可以插入连接的字符串?请帮助我解决这个问题。
答案 0 :(得分:1)
你应该使用单引号(')而不是双引号(")来sqlite字符串。
insertString=[NSString stringWithFormat:@"INSERT INTO MY_T (field1) VALUES ('%@')",[array objectatIndex:0]];
insertString=[NSString stringWithFormat:@"INSERT INTO MY_T (field1) VALUES ('%@')",[array objectatIndex:1]];
insertString=[NSString stringWithFormat:@"INSERT INTO MY_T (field1) VALUES ('%@')",[array objectatIndex:2]];
insertString=[NSString stringWithFormat:@"INSERT INTO MY_T (field1) VALUES ('%@')",[array objectatIndex:3]];
如果您要插入的字符串包含单引号,则应使用two single quotes ('')对其进行转义。请参阅-
stringByReplacingOccurrencesOfString:withString:
。
insertString=[NSString stringWithFormat:@"INSERT INTO MY_T (field1) VALUES ('%@')", [[array objectatIndex:0] stringByReplacingOccurrencesOfString:@"'" withString:@"''"]];
insertString=[NSString stringWithFormat:@"INSERT INTO MY_T (field1) VALUES ('%@')", [[array objectatIndex:1] stringByReplacingOccurrencesOfString:@"'" withString:@"''"]];
insertString=[NSString stringWithFormat:@"INSERT INTO MY_T (field1) VALUES ('%@')", [[array objectatIndex:2] stringByReplacingOccurrencesOfString:@"'" withString:@"''"]];
insertString=[NSString stringWithFormat:@"INSERT INTO MY_T (field1) VALUES ('%@')", [[array objectatIndex:3] stringByReplacingOccurrencesOfString:@"'" withString:@"''"]];