在条件 C++ 循环语句中将条目插入 SQLite3 table

Insert entry into SQLite3 table in a conditional C++ loop statement

我在我的 C++ 程序中使用 SQLite3 头文件并尝试创建一个 table 并向其插入数据,它在常规输入上工作正常。

当我在带有变化变量的 C++ 循环中使用它时显示错误。

我正在使用数据库插入我从 RS-232 读取的数据。

这是我的代码:

                sqlite3 *db;
                char *zErrMsg = 0;
                int rc;
                char *sql;
                std::string sql_str;
                std::ostringstream temp;
                std::string command;

                /* Open database */
                rc = sqlite3_open("test_1.db", &db);
                if (rc){
                    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
                    exit(0);
                }
                else{
                    fprintf(stderr, "Opened database successfully\n");
                }

                std::string str;
                std::ostringstream oss;
                oss << id_count; // stornig the primary id int values into a string 

                str = "INSERT INTO M_DATA (ID, DETAILS) VALUES(";
                str += oss.str(); //copying the int primary id
                str += ", '";

                std::string str_t1(szBuffer);  //Copying character aray to a string
                str += str_t1; //concatening the string

                str += "');";

                //printing what the database takes
                //output_file << std::endl << str << std::endl;

                char * writable = new char[str.size() + 1];
                std::copy(str.begin(), str.end(), writable);
                writable[str.size()] = '[=11=]'; // don't forget the terminating 0

                sql = writable;

                output_file << std::endl << "## SQL COMMAND : " << sql << "#" << std::endl;

                // don't forget to free the string after finished using it
                delete[] writable;

                rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);


                if (rc != SQLITE_OK){
                    fprintf(stderr, "SQL error: %s\n", zErrMsg);
                    output_file << std::endl << "** SQL ERROR : " << zErrMsg << "*" << std::endl;
                    sqlite3_free(zErrMsg);

                }
                else{
                    fprintf(stdout, "Records created successfully\n");

                }
                //  _sleep(3000);
                sqlite3_close(db);

我的问题是我有一个每次都会更改的 szBuffer,我必须将它作为新条目插入 table。

有没有办法增加主键并将我的字符串存储到其中?

单行的 sz 缓冲区将提供如下数据: 例如:

szBuffer :
ersion = 1 [SPI]: MinorVersion = 2 [SPI]: Real Time = 1434260351 [SPI]: SR # = SBB-ST1000090

我传递的字符串中的SQL命令是这样的:

SQL COMMAND :
INSERT INTO M_DATA (ID, DETAILS) VALUES(9, 'ersion = 1 [SPI]: MinorVersion = 2 [SPI]: Real Time = 1434260351 [SPI]: SR # = SBB-ST1000090');

我得到的错误是这样的:

SQL ERROR :
near "¸”_": syntax error

我不确定我这样做是对还是错。 我们可以在循环中使用插入语句吗?我是否以正确的方式传递字符串? (当我打印出来时,它看起来对我来说是正确的。) 但是为什么会出现错误?

有没有更好的方法来输入我的数据? 我对此很陌生,所以我尝试在互联网上搜索,但没有人按照我的方式去做。 请帮忙。

非常感谢。

(几乎)从不通过字符串连接构建 SQL 语句。使用准备好的语句并绑定参数值。

// Prepare the statement
sqlite3_stmt* stmt;
int result = sqlite3_prepare_v2(db, "INSERT INTO M_DATA (ID, DETAILS) VALUES(?, ?);", -1, &stmt, nullptr);
// TODO: Handle when result != SQLITE_OK

while(/* whatever you wanted to loop on */)
{
  // Bind in the parameter values
  result = sqlite3_bind_int(stmt, 1, id_count);
  // TODO: Handle when result != SQLITE_OK
  result = sqlite3_bind_text(stmt, 2, szBuffer, -1, SQLITE_STATIC);
  // TODO: Handle when result != SQLITE_OK

  // Invoke the statement
  result = sqlite3_step(stmt);
  // TODO: Handle when result != SQLITE_OK

  // Reset the statement to allow binding variables on the next iteration
  result = sqlite3_reset(stmt);
}

// Release the statement
sqlite3_finalize(stmt);