Insert in SQLite with C ++

0

With the "users" table created, I am ready to enter data in it. These data are entered by the user, and then inserted in the table. I have the following function:

int registrarUsuario(string name, string second){
            sqlite3 *db;
            int conn;
            int exec;
            string sql;

            // Abriendo conexion con la base de datos
            conn = sqlite3_open("usuarios.db", &db);
            if(SQLITE_OK != conn){
                system("clear");
                cerr << "\E[1;31mImposible introducir los datos datos\E[0;00m" << endl;
                cin.get();
                cin.sync();
                sqlite3_close(db);
                return 1;
            }
            else{
                system("clear");
                // Creando datos SQL
                sql = "INSERT INTO usuarios(Nombre,Apellidos) VALUES("+ name + "," + second + ");";
                // Introduciendo datos SQL
                exec = sqlite3_exec(db, sql.c_str(), NULL, NULL, NULL);
                if( SQLITE_OK == exec){
                    cout << "\E[1;32mDatos introducidos con exito\E[0;00m" << endl;
                    cin.get();
                    cin.sync();
                    sqlite3_close(db);
                    return 0;
                }
            }
        }

Once entered, the function is called with those parameters. The problem is that when the function is called the program is completely closed.

    
asked by Jogofus 31.05.2017 в 15:20
source

1 answer

0

It seems that they are inserting the fields wrong and you have a semicolon at the end. It should be like this:

sql = "INSERT INTO usuarios(Nombre,Apellidos) VALUES('"+ name + "','" + second + "')";

Although I recommend you use Try / Catch and know your exception

    
answered by 31.05.2017 в 15:24