I'm somewhat new to SQL and C++. I do believe the question I have yields a simple solution.
I have to create a sql query to insert some values into an existing table (const char *sql_query), with mixed strings and float values. My issue is getting the string assembly operation nailed down, without getting compiler errors.
Here is my attempt:
int main(int argc, char* argv[])
{
create_entry("mahut", "topa", "suma", 5.55, 6.66, 7.77);
return 0;
}
void create_entry(char col1[], char col2[], char col3[], float col4, float col5, float col6)
{
cout << ("create_entry") << endl;
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
char db_name[] = "db_test.db";
stringstream sql_query;
// assemble string
sql_query << "INSERT INTO Department1 (Date,Time,Accept,Factor1,Factor2,Factor3) VALUES ('" << col1 << "','" << col2 << "','" << col3 << "','" << col4 << "','" << col5 << "','" << col6 << "')";
string str = sql_query.str();
rc = sqlite3_open(db_name, &db);
if( rc )
{
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
}
else
{
fprintf(stderr, "Opened database successfully\n");
}
// execute SQL statement
rc = sqlite3_exec(db, str, callback, 0, &zErrMsg);
if( rc != SQLITE_OK )
{
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
else
{
fprintf(stdout, "Records created successfully\n");
}
sqlite3_close(db);
}
static int callback(void *db, int argc, char **argv, char **azColName)
{
cout << "Debug <in callback>" << endl;
for(int i=0; i<argc; ++i)
{
//printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
cout << ("%s", argv[i] ? argv[i] : "NULL") << endl;
}
tbl_exists = true;
return 0;
}
Particularly this is my problem:
sql_query << "INSERT INTO Department1 (Date,Time,Accept,Factor1,Factor2,Factor3) VALUES ('" << col1 << "','" << col2 << "','" << col3 << "','" << col4 << "','" << col5 << "','" << col6 << "')";
As I don't understand if there is a proper way to mix in float, integer and string into const char * I'm not sure what's wrong, but like I said I have tried it for quite some time to properly assemble that string but have failed, as I just don't grasp pointers and strings in C++. Compiling fails due to following error
error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const char*’ for argument ‘2’ to ‘int sqlite3_exec(sqlite3*, const char*, int (*)(void*, int, char**, char**), void*, char**)’ rc = sqlite3_exec(db, str, callback, 0, &zErrMsg);
I also get the following warning
warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
For your error, you're missing a .c_str()
conversion of the std::string to a C-style string. sqlite3_exec takes C-style const char *
strings, not C++ strings.
For your warning, you'll need to declare your parameters to create_entry()
as const char *
(or const char []
) - because they're literals (constants that should not be modified). Declaring them non-const opens you up to accidentally modifying them.
Assembling the string becomes easier if you are not doing it at all:
void create_entry(const char col1[], const char col2[], const char col3[],
float col4, float col5, float col6)
{
sqlite3 *db;
int rc;
const char *sql;
static const char db_name[] = "db_test.db";
sqlite3_stmt *stmt;
sql = "INSERT INTO Department1 (Date,Time,Accept,Factor1,Factor2,Factor3)"
" VALUES (?,?,?,?,?,?)";
rc = sqlite3_open(db_name, &db);
if( rc != SQLITE_OK )
{
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return;
}
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if( rc != SQLITE_OK )
{
fprintf(stderr, "Can't prepare query: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return;
}
sqlite3_bind_text(stmt, 1, col1, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, col2, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, col3, -1, SQLITE_STATIC);
sqlite3_bind_double(stmt, 4, col4);
sqlite3_bind_double(stmt, 5, col5);
sqlite3_bind_double(stmt, 6, col6);
rc = sqlite3_step(stmt);
if( rc != SQLITE_DONE )
{
fprintf(stderr, "Can't execute query: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
sqlite3_close(db);
return;
}
sqlite3_finalize(stmt);
sqlite3_close(db);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.