Take the 2-minute tour ×

I am using this code for inserting data , but insert data in hard code, but my need is to insert data from user side using c++ , can any one insist me:**

#include <iostream>
 using namespace std;
#include "sqlite3.h"

sqlite3 *db;
sqlite3_open("test1.db", & db);

string createQuery = "CREATE TABLE IF NOT EXISTS items (userid INTEGER PRIMARY KEY, ipaddr TEXT,username TEXT,useradd TEXT,userphone INTEGER,age INTEGER, "
                                                "time TEXT NOT NULL DEFAULT (NOW()));";
  sqlite3_stmt *createStmt;
  cout << "Creating Table Statement" << endl;
  sqlite3_prepare(db, createQuery.c_str(), createQuery.size(), &createStmt, NULL);
  cout << "Stepping Table Statement" << endl;
  if (sqlite3_step(createStmt) != SQLITE_DONE) cout << "Didn't Create Table!" << endl;

  string insertQuery = "INSERT INTO items (time, ipaddr,username,useradd,userphone,age) VALUES ('', '','rahul da','benerghatta','9966524824',24);"; // WORKS!
  sqlite3_stmt *insertStmt;`enter code here`
  cout << "Creating Insert Statement" << endl;
  sqlite3_prepare(db, insertQuery.c_str(), insertQuery.size(), &insertStmt, NULL);
  cout << "Stepping Insert Statement" << endl;
  if (sqlite3_step(insertStmt) != SQLITE_DONE) cout << "Didn't Insert Item!" << endl;

  string selectQuery = "select * from items where ipaddr='' & username= 'rahul';";
  sqlite3_stmt *selectStmt;
     cout << "Creating select Statement" << endl;
     sqlite3_prepare(db, selectQuery.c_str(), selectQuery.size(), &selectStmt, NULL);
     cout << "Stepping select Statement" << endl;
     if (sqlite3_step(selectStmt) != SQLITE_DONE) cout << "Didn't Select Item!" << endl;

     cout << "Success!" << endl;

   return 0;
share|improve this question
-1: Your question is incomplete. Please explain in more detail what exactly you're trying to do. Where is this "user side" you're speaking of, for example. –  Nicol Bolas Jun 7 '12 at 13:59
its not user side it just server side , i'm using gsoap...) –  Satyam Jun 8 '12 at 4:55

1 Answer 1

up vote 1 down vote accepted

It really depends on what from the data you've got is stored in. If you've got it in specific variables then you could use std::stringsteam to acheive what you want to do:

std::stringstream insertQuery;
insertQuery << "INSERT INTO items (time, ipaddr,username,useradd,userphone,age)"
               " VALUES ('" << time
            << "', '" << ipaddr
            << "', '" << username
            << "', '" << useradd
            << "', '" << userphone
            << "', " << age << ")";
sqlite3_prepare(db, insertQuery.str().c_str(), insertQuery.size(), &insertStmt, NULL);

Note the additional call .str() on the stringstream to return a string.

Of course, you'll have to ensure that your data is valid for the type of columns you're inserting it into. This is especially true if your input is coming straight from users - watch out for common gotchas such as embedded quotes and meta characters.

share|improve this answer
thanks a lot :) –  Satyam Jun 8 '12 at 4:52

Your Answer


