来自C的SQLite多插入只是添加了第一个

我有以下SQLite代码:

std::vector<std::vector<std::string> > InternalDatabaseManager::query(std::string query)
{
    sqlite3_stmt *statement;
    std::vector<std::vector<std::string> > results;

    if(sqlite3_prepare_v2(internalDbManager, query.c_str(), -1, &statement, 0) == SQLITE_OK)
    {
        int cols = sqlite3_column_count(statement);
        int result = 0;
        while(true)
        {
            result = sqlite3_step(statement);

            std::vector<std::string> values;
            if(result == SQLITE_ROW)
            {
                for(int col = 0; col < cols; col++)
                {
                    std::string s;
                    char *ptr = (char*)sqlite3_column_text(statement, col);
                    if(ptr) s = ptr;

                    values.push_back(s);
                }
                results.push_back(values);
            } else
            {
                break;
            }
        }

        sqlite3_finalize(statement);
    }

    std::string error = sqlite3_errmsg(internalDbManager);
    if(error != "not an error") std::cout << query << " " << error << std::endl;

    return results;
}

当我尝试传递查询字符串时:

INSERT INTO CpuUsage (NODE_ID, TIME_ID, CORE_ID, USER, NICE, SYSMODE, IDLE, IOWAIT, IRQ, SOFTIRQ, STEAL, GUEST) VALUES (1, 1, -1, 1014711, 117915, 175551, 5908257, 112996, 2613, 4359, 0, 0); INSERT INTO CpuUsage (NODE_ID, TIME_ID, CORE_ID, USER, NICE, SYSMODE, IDLE, IOWAIT, IRQ, SOFTIRQ, STEAL, GUEST) VALUES (1, 1, 0, 1014711, 117915, 175551, 5908257, 112996, 2613, 4359, 0, 0); INSERT INTO CpuUsage (NODE_ID, TIME_ID, CORE_ID, USER, NICE, SYSMODE, IDLE, IOWAIT, IRQ, SOFTIRQ, STEAL, GUEST) VALUES (1, 1, 1, 1014711, 117915, 175551, 5908257, 112996, 2613, 4359, 0, 0); 

它只会插入第一个插入.使用其他一些工具精简版SQLiteStudio它可以正常运行.

有什么想法可以帮助我吗?

谢谢,

佩德罗

编辑

我的查询是一个std :: string.

const char** pzTail;
const char* q = query.c_str();

int result = -1;
do {
    result = sqlite3_prepare_v2(internalDbManager, q, -1, &statement, pzTail);
    q = *pzTail;
}
while(result == SQLITE_OK);

这给了我描述:无法将’const char *’转换为’const char **’以将参数’5’转换为’int sqlite3_prepare_v2(sqlite3 *,const char *,int,sqlite3_stmt *,const char *)’

解决方法:

SQLite的prepare_v2只会从字符串中的第一个插入创建一个语句.您可以将其视为“流行前沿”机制.

int sqlite3_prepare_v2(
  sqlite3 *db,            /* Database handle */
  const char *zSql,       /* SQL statement, UTF-8 encoded */
  int nByte,              /* Maximum length of zSql in bytes. */
  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
);

http://www.sqlite.org/c3ref/prepare.html

If pzTail is not NULL then *pzTail is made to point to the first byte
past the end of the first SQL statement in zSql. These routines only
compile the first statement in zSql, so *pzTail is left pointing to
what remains uncompiled.

pzTail参数将指向其余插入,因此您可以遍历所有插入,直到它们都已准备好.

另一种选择是一次只进行一次插入,这使得其余的处理代码通常更简单一些.

通常情况下,我看到人们会在相同的交易下对他们进行评估.但事实并非如此.第二个可能会失败,第一个和第三个仍然会成功.

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能