C++ 在数据库中插入空行而不是传递的值
我试图使用c将值插入到C++ 在数据库中插入空行而不是传递的值,c++,visual-c++,sqlite,C++,Visual C++,Sqlite,我试图使用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, "Open
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;
我不明白怎么了,请帮我解决这个问题。我尝试插入的方式是错误的,我不理解您现在正在写入的
0
字节
说明:
sqlite3_bind_text(stmt, 1, try1.c_str(), 0, NULL);
语法是
int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
依照
第三个参数是要绑定到参数的值。如果第三个
sqlite3_bind_text()或sqlite3_bind_text16()或
sqlite3_bind_blob()是一个空指针,那么第四个参数是
忽略,最终结果与sqlite3_bind_null()相同
在有第四个参数的例程中,它的值是数字
参数中的字节数。需要明确的是:该值是
值中的字节数,而不是字符数。如果第四个
sqlite3_bind_text()或sqlite3_bind_text16()的参数为负,
然后字符串的长度是到第一个字节的字节数
零终止符
因此,将其更改为如下,直到写入空字节
sqlite3_bind_text(stmt, 1, try1.c_str(), -1, NULL);
我建议检查所有函数的返回码。也许你会得到一个错误指示,在这里有一个错误的代码:<代码> SqLeTe3**()/Cudio>调用。这不是C代码,而是C++。请为您的问题使用正确的语言标签。
sqlite3_bind_text(stmt, 1, try1.c_str(), -1, NULL);