Я успешно создал 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();
}