加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 09:39:32 所属栏目:百科 来源:网络整理
导读:我有以下SQLite代码: std::vectorstd::vectorstd::string InternalDatabaseManager::query(std::string query){ sqlite3_stmt *statement; std::vectorstd::vectorstd::string results; if(sqlite3_prepare_v2(internalDbManager,query.c_str(),-1,statemen
我有以下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,1014711,117915,175551,5908257,112996,2613,4359,0); INSERT INTO CpuUsage (NODE_ID,0);

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

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

谢谢,

佩德罗

编辑

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

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

int result = -1;
do {
    result = sqlite3_prepare_v2(internalDbManager,q,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参数将指向其余插入,因此您可以遍历所有插入,直到它们都已准备好.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读