【问题标题】:Using prepared statement with sqlite transaction statement [closed]将准备好的语句与 sqlite 事务语句一起使用 [关闭]
【发布时间】:2012-02-08 08:41:48
【问题描述】:

我正在尝试在 sqlite 中插入大量数据,以提高性能,我正在使用 transaction 语句。我的代码如下所示: CSVParser 解析器;

   query.exec("BEGIN TRANSACTION;");
   while (!file.atEnd())
    {
           line = file.readLine();
           if (line == "")
                  continue;
            parser << line.toStdString(); // Feed the line to the parser
                // Now extract the columns from the line
            parser >> sCol1 >> sCol2 >> sCol3 >> sCol4;

            // Method one to insert data
            sqlQuery = "INSERT INTO Person(phone, name, firstname, lastname) VALUES (" \
                         "'" + QString(sCol1.c_str()) + \
                         "','" + QString(sCol2.c_str()) + \
                         "','" + QString(sCol4.c_str()) + \
                         "','" + QString(sCol3.c_str()) + "')";
            //query.exec(sqlQuery);

            // Method two to insert data
             query.prepare("INSERT INTO Person(phone, name, firstname, lastname) VALUES (:phone, :name, firstname, :lastname");
             query.bindValue(":phone", QString(sCol1.c_str()));
             query.bindValue(":name", QString(sCol2.c_str()));
             query.bindValue(":firstname", QString(sCol4.c_str()));
             query.bindValue(":lastname", QString(sCol3.c_str()));
             query.exec();
             cout << query.lastError().text().toStdString();
      }

   query.exec("END TRANSACTION;");  

如果我使用第一种方法,数据插入正常,但是如果文本包含像这样的字符“'”,则可能稍后会出现问题。为了避免这种情况,我切换到prepared statement,一个新的问题,没有插入数据。我看到的唯一错误:

参数计数不匹配 参数计数不匹配 参数计数 mismatch 参数 count mismatch 参数 count mismatch 参数 计数不匹配 参数计数不匹配 参数计数不匹配 是不是我在这里失踪了?

【问题讨论】:

    标签: c++ transactions sqlite qt4


    【解决方案1】:

    不知道是不是这个原因:

    query.prepare("INSERT INTO Person(phone, name, firstname, lastname)
        VALUES (:phone, :name, firstname, :lastname");
                                                  ^^^
             missing close paren inside the string^^^
    

    【讨论】:

    • 向前滚动编辑器使其出现,再次感谢。
    猜你喜欢
    • 2021-10-11
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 2014-04-11
    • 2012-06-07
    • 2021-10-13
    相关资源
    最近更新 更多