0

I'm trying to convert integer data to string , but I'm getting error cannot convert 'int*' to 'const char*' for argument '2' to 'char* strcat(char*, const char*)'

hey guys please fix these issue, where did I dowrong?

    #include <iostream>
    using namespace std;
    #include "sqlite3.h"
    #include<string.h>
    #include <sstream>
    #define STRING_MAX 32

    typedef struct metadata_t
    {
      char session_username[256];   
      int user_id[256];
      int session_id[256];
      char buffer[256];
    }metadata_t;

    void  insertdata1(metadata_t *data);

  int main (int argc, const char * argv[])
      {

      struct metadata_t *data;
        cin >> *data->user_id;
        cin >> data->session_username;
        cin >> *data->session_id;
        cin >> data->buffer;


         insertdata1(data);

         }

    void insertdata1(metadata_t *data)
        {

         // char *buff1,*buff2;
             sqlite3 *db;
             sqlite3_open("test1.db", & db);
             string createQuery = "CREATE TABLE IF NOT EXISTS items (user_id INTEGER PRIMARY KEY,   session_username TEXT,Session_id INTEGER,buffer TEXT,));";
             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;


            //itoa(*data->userid,buff1,10);  // not work
            //itoa(*data->userphone,buff2,10);// Integer to String Conversion

        /*   std::string  ss;
           ss << *data->user_id;
           ss << *data->session_id;
           std::string user_Id = ss.str();
           std::string session_Id = ss.str();  */ // not working
                   char *a="(";
                   char *d=")";
                   char *b="'";
                   char *c=",";
                   char str1[1000];
                   char * str2 = "";
                   char * g = ";"; 
               strcpy(str1, "INSERT INTO items (user_id,session_username,session_id,buffer)VALUES");

               strcat(str1,a);

               strcat(str1,b);
               strcat(str1,data->user_id);
               strcat(str1,b);
               strcat(str1,c);
               strcat(str1,b);
               strcat(str1,data->session_username);
               strcat(str1,b);
               strcat(str1,c);
               strcat(str1,b);
               strcat(str1,data->session_id);
               strcat(str1,b);
               strcat(str1,c);
               strcat(str1,b);
               strcat(str1,data->buffer);
               strcat(str1,b);
               strcat(str1,d);
               strcat(str1,g);

            std::string insertQuery = str1; // WORKS!
            sqlite3_stmt *insertStmt;
            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;




      }** 
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
Satyam
  • 1,672
  • 3
  • 20
  • 34
  • 1
    To convert an `int` to a `std::string` see http://stackoverflow.com/questions/10516196/append-an-int-to-a-stdstring/10516313#10516313 (from one of many). Why use `strcat()` and just not `std::ostringstream` or `operator+()`? – hmjd Jul 02 '12 at 13:30
  • 1
    Once you get past the compilation errors, you almost certaintly get a seg fault as no memory has been allocated for `data`: just use stack allocated `metadata_t`. – hmjd Jul 02 '12 at 13:33
  • thanks @ hmjd I'm new programmer in c++ so now I'm facing lots of problem ..... you are senior guy please you help me in c++ programming...) – Satyam Jul 02 '12 at 13:42
  • 1
    Make sure to get to know SQL injection attacks. The code as is seems pretty dangerous. – Sebastian Mach Jul 02 '12 at 14:34

1 Answers1

2

Recommend using a std::ostringstream (you need to #include <sstream>) to construct your query:

std::ostringstream str1;

str1 << "INSERT INTO items (user_id,session_username,session_id,buffer)VALUES"
     << a
     << b // and so on
     << g;

const std::string insertQuery = str1.str();

The following does not allocate any memory, but just declares a pointer (the struct is not required in C++):

struct metadata_t *data;

and any attempt to dereference it will result in undefined behaviour, most likely a segmentation fault:

cin >> *data->user_id; 

just declare it on the stack:

metadata_t data;

and pass it's address (or pass by reference) to insertdata1():

insertdata1(&data);

Prefer std::string to char* or char[]:

struct metadata_t
{
    std::string session_username;
    int user_id[256];  // Did you really mean an array of int here ?
    int session_id[256];
    std::string buffer;
};
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • @hmjd.. this database use in gsoap server for keep record ... now I'm trying to create dynamic database , used for transfer data between two user. – Satyam Jul 02 '12 at 14:30
  • @SatyamSrivastava, recommend posting a new question for a new issue. FYI, I am not familiar with sqlite so someone else may be better placed to help. Good luck. – hmjd Jul 02 '12 at 14:34