【问题标题】:How to insert string that contains " character in sqlite ios如何在sqlite ios中插入包含“字符的字符串
【发布时间】:2014-07-18 18:15:06
【问题描述】:

我正在使用以下代码,但是 sqlite 给了我错误

 NSArray* a = [NSArray arrayWithObjects:
              @"\""
              @"\\ \"",
              nil];
quantity=[quantity stringByReplacingOccurrencesOfString:[a objectAtIndex:0 ] withString:[a objectAtIndex:1]];
queryString = [NSString stringWithFormat:@"INSERT INTO SHOPINGLIST (QUANTITY) VALUES (\"%@\")",quantity];

它给出的错误是:- 在“diax1”附近:查询 INSERT INTO SHOPINGLIST 中的语法错误 原始数量=1片,中(4-1/2"diax1/8"厚)

【问题讨论】:

    标签: ios objective-c sqlite special-characters


    【解决方案1】:

    要在 iOS 的 SQLite 中插入特殊字符,请使用如下占位符(另请检查 Sqlite 中允许哪些特殊字符):

    queryString = @"INSERT INTO SHOPINGLIST (QUANTITY) VALUES (?)";
    sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, queryString, -1, &compiledStatement, NULL) == SQLITE_OK) {
            sqlite3_bind_text(compiledStatement, 1, [quantity UTF8String], -1, SQLITE_TRANSIENT);
      // Use sqlite3_bind_text for string values. For other type of values, check sqlite documentation.
     // .... And So on.
    
        }
    

    【讨论】:

    • 谢谢,它的工作原理.. 还有一个问题,如何插入浮点值,因为上面的语句只适用于字符串。我的意思是浮点值我应该替换(?)还是什么?
    • 对于浮点值使用 sqlite3_bind_double。检查此链接:sqlite.org/c3ref/bind_blob.html。我建议您应该遵循一些 sqlite 教程来清除您的概念。
    • @GauravSingh 数量是多少?
    • @AugustinJose 在我的例子中,数量是NSString
    【解决方案2】:

    对于双精度值,使用这个:-

    sqlite3_bind_double(compiledStatement, 1, [quantity UTF8String], -1, SQLITE_TRANSIENT);
    

    【讨论】:

      猜你喜欢
      • 2010-09-14
      • 1970-01-01
      • 2021-03-07
      • 2013-05-29
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多