[英]syntax error with insert statement
我在sqlite中使用c / c ++。 下面顯示的我的代碼可以編譯,但是輸入密碼后,我遇到了此錯誤。
SQL error: near "NSERT": syntax error
我無法確定語法錯誤到底在哪里。
碼
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <string>
#include <iostream>
void insertIntoTable(std::string userName,std::string password);
int main () {
std::string userName;
std::string password;
std::cout << "Enter a user name" << std::endl;
std::cin >> userName;
std::cout << "Enter a password" << std::endl;
std::cin >> password
insertIntoTable(userName,password);
}
//method to insert into table
void insertIntoTable(std::string userName,std::string password) {
sqlite3 *db;
char *zErrMsg = 0;
int rc;
const char *sql;
int i = 1;
int j = 1;
rc = sqlite3_open("test.db", &db);
if( rc )
{
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}else
{
fprintf(stderr, "Opened database successfully\n");
}
//query
sql = "INSERT INTO Login (_id,username,password,manager_id) " \
"VALUES ("+i,userName,password,j+");";
}
請幫忙。 謝謝
sql = "INSERT INTO Login (_id,username,password,manager_id) " \
"VALUES ("+i,userName,password,j+");";
廢話 您不能通過將非字符串值添加到C樣式的字符數組或指針來構建字符串。 並且逗號運算符不會在字符串后附加逗號,而是會在執行副作用后丟棄前一個表達式的值。 這意味着整個表達式等於
sql = "INSERT..." + i;
它將i
(1)的值添加到字符串文字的地址中以獲得指向第二個字符的指針; 因此總體結果為NSERT INTO Login (_id,username,password,manager_id) VALUES (
您想使用std::string
。 在C ++ 11中,有一些方便的函數可以將數字轉換為字符串:
std::string sql = "INSERT INTO Login (_id,username,password,manager_id) "
"VALUES (" + std::to_string(i) + ','
+ userName + ','
+ password + ','
+ std::to_string(j) + ");";
從歷史上看,您將需要一個字符串流
std::stringstream s;
s << "INSERT INTO Login (_id,username,password,manager_id) VALUES ("
<< i << ',' << userName << ',' << password << ',' << j << ");";
std::string sql = s.str();
所有這些答案都是錯誤的。 用於SQL查詢執行的字符串插值是進入注入領域的快速列車。 沒有理由您應該使用它。
正確的方法是在准備好的語句中使用sqlite3_bind
將值綁定到占位符,即代碼應如下所示:
sql = "INSERT INTO Login (_id,username,password,manager_id) "
"VALUES (?, ?, ?, ?);";
sqlite3_stmt* stmt;
rc = sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, nullptr);
// handle rc
// Now bind the parameters.
sqlite3_bind_int(stmt, 1, i);
sqlite3_bind_text(stmt, 2, userName.c_str(), userName.size(), SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 3, password.c_str(), password.size(), SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 4, j);
// Now execute.
看來您是VALUES陳述式的問題:
VALUES ("+i,userName,password,j+");";
應該
"VALUES ("+std::to_string(i)+","+userName+","+password+","+std::to_string(j)+");";
如果沒有std :: to_string(沒有C ++ 11),則需要其他方法將int轉換為std :: string,例如stringstream。
我百分之八十確定這是應該的樣子……不確定,因為它不是純SQL,而是
sql = "INSERT INTO `Login` (`_id`, `username`, `password`, `manager_id`) " \
"VALUES ("+i+", '"+userName+"', '"+password+"', "+j+");";
您不能建立這樣的字符串。
您有兩個選擇。.使用std::string
並使用+ operator
形成一個string
。 或者,如果您想使用char*
使用以下代碼
char *sql = malloc (NumberOfCharYouNeed); //allocate sufficient chars for your purpose.
sprintf (sql, "INSERT INTO Login (_id,username,password,manager_id) VALUES (\"+%d%s%s%d+\")",i,userName,password,j);
注意:確保使用后釋放(sql)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.