【发布时间】:2018-10-06 06:58:30
【问题描述】:
我已经成功创建了一个 TABLE 并尝试在其中插入字符串文本。尝试 sqlite3_execute(sqlstatement) 时出现错误输出。尝试转换为 const char*。我是 SQLite 和 Visual C++ 的新手。我究竟做错了什么?还有一种方法可以成功检索 SQLite 错误消息,这会有所帮助,因为我找不到使用 Visual c++ 的方法。谢谢!
private:System::Void SaveButton_Click(System::Object^ sender, System::EventArgs^ e) {
// Convert double to string
String^ convert;
std::string MapString[16];
String^ NameRaw = SaveName->Text;
std::string nameString = msclr::interop::marshal_as< std::string >(NameRaw);
// double array to string
for (int i = 0; i < 15; i++) {
convert = MeasurementsAndPricesPublic[i].ToString();
std::string convertString = msclr::interop::marshal_as< std::string >(convert);
MapString[i] = convertString;
}
// SQLITE DATABASE CODE
sqlite3* db = NULL;
sqlite3_stmt* query = NULL;
// created table for the first run
/*std::string generateTable = "CREATE TABLE PARAMETERS("
"Name TEXT NOT NULL,"
"PatchFL TEXT NOT NULL,"
"PatchFW TEXT NOT NULL,"
"PatchSL TEXT NOT NULL,"
"PatchSW TEXT NOT NULL,"
"PatchHL TEXT NOT NULL,"
"PatchHW TEXT NOT NULL,"
"AvailableFL TEXT NOT NULL,"
"AvailableFW TEXT NOT NULL,"
"AvailableSL TEXT NOT NULL,"
"AvailableSW TEXT NOT NULL,"
"AvailableHL TEXT NOT NULL,"
"AvailableHW TEXT NOT NULL,"
"AvailableFP TEXT NOT NULL,"
"AvailableSP TEXT NOT NULL,"
"AvailableHP TEXT NOT NULL);";*/
// Table statement I suspect an error here
std::string sqlstatement = "INSERT INTO PARAMETERS (Name, PatchFL, PatchFW, PatchSL, PatchSW, PatchHL, PatchHW, AvailableFL, AvailableFW, AvailableSL, AvailableSW, AvailableHL, AvailableHW, AvailableFP, AvailableSP, AvailableHP) VALUES ('"
+ nameString + "','"
+ MapString[0] + "','"
+ MapString[1] + "','"
+ MapString[2] + "','"
+ MapString[3] + "','"
+ MapString[4] + "','"
+ MapString[5] + "','"
+ MapString[6] + "','"
+ MapString[7] + "','"
+ MapString[8] + "','"
+ MapString[9] + "','"
+ MapString[10] + "','"
+ MapString[11] + "','"
+ MapString[12] + "','"
+ MapString[13] + "','"
+ MapString[14] + ");";
// statement for testing did not work either
const char* sql_insert = "INSERT INTO PARAMETERS (Name, PatchFL, PatchFW, PatchSL, PatchSW, PatchHL, PatchHW, AvailableFL, AvailableFW, AvailableSL, AvailableSW, AvailableHL, AvailableHW, AvailableFP, AvailableSP, AvailableHP) VALUES (A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A);";
// Trying to convert to char with no success
char * buffer = new char[sqlstatement.length()];
strcpy(buffer, sqlstatement.c_str());
int ret = 0;
do // avoid nested if's
{
// initialize engine
if (SQLITE_OK != (ret = sqlite3_initialize()))
{
MessageBox::Show("Failed to initialize library. Error: ", ret.ToString());
break;
}
// open connection to a DB
if (SQLITE_OK != (ret = sqlite3_open_v2("parameters.db", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)))
{
MessageBox::Show("Failed to open connection to database. Error: ", ret.ToString());
break;
}
// executing statement I GET ERROR HERE
if (SQLITE_OK != (ret = sqlite3_exec(db, buffer, NULL, NULL, errmsg)))
{
MessageBox::Show("Failed to prepare the statement. "); //I get this message
break;
}
MessageBox::Show("Successfully saved parameters");
//// prepare the statement
//if (SQLITE_OK != (ret = sqlite3_prepare_v2(db, "SELECT 2012", -1, &query, NULL)))
//{
// MessageBox::Show("Failed to prepare the statement. Error: ", ret.ToString(), sqlite3_errmsg(pDb).ToString());
// break;
//}
//// step to 1st row of data
//if (sqlite_row != (ret = sqlite3_step(query))) // see documentation, this can return more values as success
//{
// printf("failed to step: %d, %s\n", ret, sqlite3_errmsg(pdb));
// break;
//}
//// ... and print the value of column 0 (expect 2012 here)
//printf("value from sqlite: %s", sqlite3_column_text(query, 0));
} while (false);
// cleanup
if (NULL != query) sqlite3_finalize(query);
if (NULL != db) sqlite3_close(db);
sqlite3_shutdown();
}
【问题讨论】:
标签: visual-studio visual-c++ sqlite c++-cli