我正在尝试使用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;
}
我的选择查询工作正常,并且我在try1
至try9
中获得了价值。像try1 = 'xyz'
一样,我使用
string try1 = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)));
我不明白怎么了,请帮助我解决此问题。我尝试插入的方式是我不理解的错误
答案 0 :(得分: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
第三个参数是绑定到该参数的值。如果第三 sqlite3_bind_text()或sqlite3_bind_text16()的参数或 sqlite3_bind_blob()是NULL指针,则第四个参数是 忽略,最终结果与sqlite3_bind_null()相同。
在具有第四个参数的例程中,其值为数字 参数中的字节数。要明确的是:该值是 值中的字节数,而不是字符数。如果第四 sqlite3_bind_text()或sqlite3_bind_text16()的参数为负, 那么字符串的长度就是直到第一个字节的字节数 零终止符。
因此将其更改为以下内容,直到写入空字节为止。
sqlite3_bind_text(stmt, 1, try1.c_str(), -1, NULL);