简体   繁体   English

无法将字符插入Sqlite DB [Objective-C]

[英]Can't insert " character into Sqlite DB [Objective-C]

I'm inserting some data on a sqlite db, It works fine but what I noticed is that I can't insert words that contains the character " , is it a common issue? should I change parse the text and edit every " character I find? 我正在sqlite db上插入一些数据,效果很好,但是我注意到我不能插入包含字符"单词,这是一个常见问题吗?我应该更改解析文本并编辑每个字符"找?

This is the code i'm using in order to insert data into my 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);
        }

You need to bind your SQLite statements using the sqlite3_bind_xxx() function. 您需要使用sqlite3_bind_xxx()函数绑定 SQLite语句。 Basically, you remove all variables from your statement (in your case the %@) and replace them with '?'. 基本上,您从语句中删除所有变量(在您的情况下为%@),并将其替换为“?”。 SQLite then knows that where an ? SQLite然后知道在哪里? is HAS to be a variable, and therefore doesn't get it mixed up with a command. 是HAS是变量,因此不会将其与命令混淆。

For example, say you wanted to bind the word "INSERT". 例如,假设您要绑定单词“ INSERT”。 Using ? 使用? SQLite won't read this as a command and then flag an error. SQLite不会将此作为命令读取,然后标记一个错误。

Read the docs (link above) for full information on how to use the bind function. 阅读文档(上面的链接)以获取有关如何使用bind函数的完整信息。

Here's what your code might look like with binding (UNTESTED): 使用绑定(未测试),代码可能如下所示:

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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM