Can';t插入“;将字符转换为Sqlite DB[Objective-C]

Can';t插入“;将字符转换为Sqlite DB[Objective-C],objective-c,sqlite,Objective C,Sqlite,我在sqlite db上插入了一些数据,它工作正常,但我注意到我不能插入包含字符“”的单词,这是一个常见问题吗?我应该更改解析文本并编辑我找到的每个“字符吗 这是我用来将数据插入数据库的代码: UICollectionViewCell *cell = (UICollectionViewCell *)button.superview.superview; NSIndexPath *indexPath = [self.customCollectionView indexPathFor

我在sqlite db上插入了一些数据,它工作正常,但我注意到我不能插入包含字符
”的单词,这是一个常见问题吗?我应该更改解析文本并编辑我找到的每个
字符吗

这是我用来将数据插入数据库的代码:

UICollectionViewCell *cell = (UICollectionViewCell *)button.superview.superview;
        NSIndexPath *indexPath = [self.customCollectionView indexPathForCell:cell];
        FolderProducts *item = _feedItems[indexPath.item];

        sqlite3_stmt    *statement;
        const char *dbpath = [databasePath UTF8String];

        if (sqlite3_open(dbpath, &Carrello) == SQLITE_OK)
        {
            NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO CarrelloMese (titolo, codice, prezzo, urlImg) VALUES (\"%@\", \"%@\", \"%@\", \"%@\")",item.nomeProdotto, item.codice, item.prezzo, item.urlImg];

            const char *insert_stmt = [insertSQL UTF8String];

            sqlite3_prepare_v2(Carrello, insert_stmt, -1, &statement, NULL);

            if (sqlite3_step(statement) == SQLITE_DONE)
            {

            } else {

            }

            sqlite3_finalize(statement);
            sqlite3_close(Carrello);
        }
您需要使用函数绑定SQLite语句。基本上,从语句中删除所有变量(在您的例子中为%@),并用“?”替换它们。SQLite知道一个?is必须是一个变量,因此不会与命令混淆

例如,假设您想绑定单词“INSERT”。使用?SQLite不会将此作为命令读取,然后标记错误

有关如何使用绑定功能的完整信息,请阅读文档(上面的链接)

以下是使用绑定(未测试)时代码的外观:


将变量绑定到prepare语句;这就是它的作用。@Droppy,你能再解释一点吗?准备好的语句被设计成可以准备一次语句,并在多次执行准备好的语句时将不同的值绑定到占位符中。这个绑定过程将负责转义特殊字符,这是您的问题所在。这不是最好的编码示例。
insertSQL
/
insert stmt
发生了什么?还缺乏错误检查和报告。您不应该鼓励每次在数据库上运行语句时打开/关闭数据库。我复制并粘贴了他的代码,然后添加了一个sqlite_bind_xxx()函数作为示例。我同意代码可以改进(我个人不想为NSStrings费心,直接使用char*),但我觉得这超出了问题的范围。只是一个打字错误,是sqlite3_bind_text,Ty,需要帮助@RASS请原谅我最后一个问题,这个绑定函数不应该适用于每种文本格式吗?因为现在它可以很好地与“”配合使用,但例如,我看到一个带ò的单词错误地保存在db中,我是否需要将.UTF8String更改为其他内容?谢谢你说实话我不确定。我知道只有0-127的值是为ascii字符保留的,像ø这样的字符由文本编辑器显示(我不确定它们是如何保存的,可能是属性字符串)。很可能无法在sqlite数据库中保存非ASCII字符。我建议你开始研究!
sqlite3_stmt    *statement;
        const char *dbpath = [databasePath UTF8String];

        if (sqlite3_open(dbpath, &Carrello) == SQLITE_OK)
        {
            NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO CarrelloMese (titolo, codice, prezzo, urlImg) VALUES (?,?,?,?)"];

            const char *insert_stmt = [insertSQL UTF8String];

            sqlite3_prepare_v2(Carrello, insert_stmt, -1, &statement, NULL);

            if (sqlite3_bind_text(statement, 0, item.nomeProdotto.UTF8String, item.nomeProdotto.length, SQLITE_STATIC) != SQLITE_OK) {
                NSLog(@"An error occurred");
            }
            // Etc etc
            // SQLite bind works like this: sqlite_bind_text/int/e.t.c(sqlite3_stmt,index_of_variable, value); 
            // there are optionally parameters for text length and copy type SQLITE_STATIC and SQLITE_TRANSIENT.

            if (sqlite3_step(statement) == SQLITE_DONE)
            {

            } else {

            }

            sqlite3_finalize(statement);
            sqlite3_close(Carrello);
        }