sqlite3 and C++ : insert user values

Hi everyone,

I'm trying to insert into the database I created, values entered by the user.
But I can't find how to insert them it looks like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 //Go for the user infos
   cout << "What is your ID ? : ";
   cin >> id;
   cout << "What is you letter ? : ";
   cin >> letter;

   cout << "Your letter is "<<letter<< " and your ID is "<<id<<"\n";
   
   //Create a table for user infos

   sql1 = "CREATE TABLE USER( ID INT NOT NULL, LET CHAR[1] NOT NULL,)";
   
	   rc = sqlite3_exec(db, sql1, callback2, 0, &zErrMsg);
   if( rc != SQLITE_OK ){
   cout << "SQL error: %s\n\n"<< zErrMsg;
      sqlite3_free(zErrMsg);
   }else{
      cout << "Table created successfully\n";
   }

//Insert user infos in the table

   sql2 = "INSERT INTO USER (ID, LET) VALUES (" << id <<", '"<< letter <<"')";
	   
	   rc = sqlite3_exec(db, sql2, callback2, 0, &zErrMsg);
   if( rc != SQLITE_OK ){
     cout << "SQL error: %s\n"<< zErrMsg;
      sqlite3_free(zErrMsg);
   }else{
      cout << "Records created successfully\n";
   }


I don't know how to proceed, I receive a error here for the sql2 statement

error C2296: '<<' : illegal, left operand has type 'const char [36]'



thanks for your help.
Last edited on
Is sql2 a std::string or it is a char*?

Either way, stream operators works only on streams.
If sql2 is a string, you can use:
"INSERT INTO USER (ID, LET) VALUES (" + std::to_string(id) + ", '" + std::_to_string(letter) + "')";
or (even if it is char*) you can use string streams.
Thank you for your answer.

sql2 is a char*.

here are my variables :

1
2
3
4
5
char *sql;
char *sql1;
char *sql2;
int id;
char letter[1];


Last edited on
Try to not use c-strings and prefer C++ one:

1
2
3
std::string sql2 = "INSERT INTO USER (ID, LET) VALUES (" + std::to_string(id) + 
                   ", '" + std::to_string(letter) + "')";
rc = sqlite3_exec(db, sql2.c_str(), callback2, 0, &zErrMsg);
I get this error :

error C2665: 'std::to_string' : none of the 3 overloads could convert all the argument types

Argh. why do you use 1 element array instead of simple char.
1
2
3
4
char letter;
//
std::string sql2 = "INSERT INTO USER (ID, LET) VALUES (" + std::to_string(id) + 
                   ", '" + letter + "')";
Ok, the problem has been solved by using a cast ..

sql2 = "INSERT INTO USER (ID, LET) VALUES (" + to_string(static_cast<long long>(id)) + ", '" + letter + "')";

The code works.

Thanks for your help MiiNiPaa
Another question :
What if I need to insert a float ?
1) you did not need to use a cast. Removing to_string call on letter did actual job.
2) use to_string() function. It has overload for floats: http://en.cppreference.com/w/cpp/string/basic_string/to_string
Thank you.

I also tried to insert some others entries in a new table but I get the same errors.
I dont see why.

1
2
3
4
int year1;
char name;

sql4 = "INSERT INTO USER3 (NAME, YEAR) VALUES ('" +  name + "', " + to_string(year1) + ")";


All in the same main().
Last edited on
1
2
int year;
to_string(year1)
Are you getting "undefined reference" error?
Sry I edited.

Im getting

error C2110: '+' : cannot add two pointers	
error C2668: 'std::to_string' : ambiguous call to overloaded function	
Last edited on
Try to use stringstreams.
Looks like it will be easier for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <sstream>
//...
std::ostringstream temp;
std::string command;
temp << "CREATE TABLE USER( ID INT NOT NULL, LET CHAR[1] NOT NULL,)";
command = temp.str();
rc = sqlite3_exec(db, command.c_str(), callback2, 0, &zErrMsg);
//...
temp.str("");
temp << "INSERT INTO USER (ID, LET) VALUES (" << id <<", '"<< letter <<"')";
command = temp.str();
rc = sqlite3_exec(db, command.c_str(), callback2, 0, &zErrMsg);
//...
temp.str("");
temp << "INSERT INTO USER3 (NAME, YEAR) VALUES ('" <<  name << "', " << year1 << ")";
command = temp.str();
rc = sqlite3_exec(db, command.c_str(), callback2, 0, &zErrMsg);
You can also wrap sqlite3_exec() function in your own to avoid manually extracting string and calling c_str() on result.
Topic archived. No new replies allowed.