Question:
How is data ( int
, long long
, double
) sqlite3
to sqlite3
?
Actually, I managed to figure out how to write values of the string
type:
sqlite3_stmt *stmt;
sqlite3* db;
int rc; // return code
char *errmsg; // pointer to an error string
//open out db
rc = sqlite3_open(nameDB.c_str(), &db);
//to answer on our request to open
char* request_status = 0;
if (rc != SQLITE_OK) {
printf("ERROR opening SQLite DB: %s\n", sqlite3_errmsg(db));
goto out;
}
printf("opened SQLite handle successfully.\n");
//create sql request (create columns)
statistic_query = "CREATE TABLE IF NOT EXISTS MY_TABLE(uno, dos, tres)";
//compiling request
sqlite3_prepare_v2(db, statistic_query.c_str(), strlen(statistic_query.c_str()), &stmt, NULL);
//send sql request to database
rc = sqlite3_step(stmt);
//and now create sql request with our data
statistic_query = "INSERT INTO MY_TABLE(uno, dos, tres) VALUES (string1,sting2, string3);";
sqlite3_prepare_v2(db, statistic_query.c_str(), strlen(statistic_query.c_str()), &stmt, NULL);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
{
printf("ERROR inserting data: %s\n", sqlite3_errmsg(db));
goto out;
}
sqlite3_finalize(stmt);
That is, in this way, you can form a string from values of the string
type for an sql query.
The question is how to write different values into one row in the database (1 column = int
, 2 column long long
, 3 column double
).
For writing different types, sqlite has the corresponding functions:
long long: int sqlite3_bind_int64(sqlite3_stmt*, int, long long int);
double: int sqlite3_bind_double(sqlite3_stmt*, int, double);
int: int sqlite3_bind_int(sqlite3_stmt*, int, int);
But it is not very clear to me how to implement my task using these functions. (ps: also the second question, how can the corresponding cell types be set?).
Answer:
Actually, I figured it out, I found the answer on the official website:
https://sqlite.org/datatype3.html
SQL database engines that use rigid typing will usually try to automatically convert values to the appropriate datatype.
Consider this: CREATE TABLE t1 (a INT, b VARCHAR (10));
INSERT INTO t1 (a, b) VALUES ('123', 456);
Rigidly-typed database will convert the string '123' into an integer 123 and the integer 456 into a string '456' prior to doing the insert.
That is, SQLite automatically converts the values as they are written and read into a valid format.
And it really works.