gpt4 book ai didi

c++ - 在数据库中插入空行而不是传递值

转载 作者:行者123 更新时间:2023-11-30 04:48:54 25 4
gpt4 key购买 nike

我正在尝试使用 c 向 sqlite3 插入值,但不是传递值而是插入表中的空白行

int main() {
sqlite3 *db;
sqlite3_stmt *stmt;
int rc ;
rc = sqlite3_open("mydb.db", &db);

if (rc)
{
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
}
else
{
fprintf(stderr, "Opened database successfully\n");
}

sqlite3_prepare_v2(db, "select * FROM `99940` a INNER JOIN (SELECT rowid FROM `Search_99940` ('B Rujesh P Balakrishnan')) b ON b.rowid = a.rowid WHERE upper(a.circle) = ('TAMIL NADU')", -1, &stmt, NULL);

sqlite3_bind_int(stmt, 0, 16);

if ((rc = sqlite3_step(stmt)) == SQLITE_ROW)
{
string try1 = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)));
string try2 = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2)));
string try3 = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3)));
string try4 = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 4)));
string try5 = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 5)));
string try6 = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 6)));
string try7 = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 7)));
string try8 = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 8)));
string try9 = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 9)));


char* errorMessage;
sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, &errorMessage);

char buffer[] = "INSERT INTO `99946` (Number,Full_Name,Address,Date,Circle,Operator,Alterno,IDProof,SimType) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
sqlite3_prepare_v2(db, buffer, strlen(buffer), &stmt, NULL);

for (unsigned i = 0; i < 1; i++)
{
sqlite3_bind_text(stmt, 1, try1.c_str(), 0, NULL);
sqlite3_bind_text(stmt, 2, try2.c_str(), 0, NULL);
sqlite3_bind_text(stmt, 3, try3.c_str(), 0, NULL);
sqlite3_bind_text(stmt, 4, try4.c_str(), 0, NULL);
sqlite3_bind_text(stmt, 5, try5.c_str(), 0, NULL);
sqlite3_bind_text(stmt, 6, try6.c_str(), 0, NULL);
sqlite3_bind_text(stmt, 7, try7.c_str(), 0, NULL);
sqlite3_bind_text(stmt, 8, try8.c_str(), 0, NULL);
sqlite3_bind_text(stmt, 9, try9.c_str(), 0, NULL);

if (sqlite3_step(stmt) != SQLITE_DONE)
{
printf("Commit Failed!\n");
}
else
{
printf("Success!\n");
}
sqlite3_reset(stmt);
}
sqlite3_exec(db, "COMMIT TRANSACTION", NULL,NULL, &errorMessage);
sqlite3_finalize(stmt);
}

return 0;

我的选择查询工作正常,而且我在 try1try9 中获取值。像 try1 = 'xyz' 我用

得到那个值
string try1 = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)));

有什么问题我不明白请帮我解决这个问题。我尝试插入的方式是错误的我不明白

最佳答案

截至目前,您正在写入 0 个字节。

解释:

    sqlite3_bind_text(stmt, 1, try1.c_str(), 0, NULL);

语法是

int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));

根据 sqlite3

The third argument is the value to bind to the parameter. If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16() or sqlite3_bind_blob() is a NULL pointer then the fourth parameter is ignored and the end result is the same as sqlite3_bind_null().

In those routines that have a fourth argument, its value is the number of bytes in the parameter. To be clear: the value is the number of bytes in the value, not the number of characters. If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16() is negative, then the length of the string is the number of bytes up to the first zero terminator.


于是改为如下写直到空字节。

sqlite3_bind_text(stmt, 1, try1.c_str(), -1, NULL);

关于c++ - 在数据库中插入空行而不是传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55650452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com