Reputation: 65
How to fix this error
/home/andi/Desktop/latihan/sqlite c++/sqlite insert.cpp:37:76: error: invalid operands of types ‘const char [53]’ and ‘char [50]’ to binary ‘operator+’
This is the code. I hope my code is readable, sorry for my poor English
# include <stdio.h>
# include <iostream>
# include <sqlite3.h>
# include <stdlib.h>
using namespace std;
int main(void)
{
sqlite3 *conn;
sqlite3_stmt *res;
int error = 0;
int rec_count = 0;
const char *errMSG;
const char *tail;
char sql_lite[900]=" ";
int x,number;
char name[50];
char username[50];
char password[50];
error = sqlite3_open("data.dat", &conn);
if (error)
{
printf("Can not open database");
}
printf("Enter the number of data to be inserted\n");
scanf("%d",&x);
for(number=x;number>0;number--)
{
cout<<"name : ";cin>>name;
cout<<"username : ";cin>>username;
cout<<"password : ";cin>>password;
}
sprintf(sql_lite, "INSERT INTO login (name,username,password) VALUES ('"+ username + "','" + name + "','" + password +"');");
error = sqlite3_exec(conn, sql_lite, 0, 0, 0);
error = sqlite3_prepare_v2(conn, "SELECT * FROM login order by No",1000, &res, &tail);
if (error != SQLITE_OK)
{
printf("We did not get any data!");
exit(0);
}
printf("=======================================\n");
while (sqlite3_step(res) == SQLITE_ROW)
{
printf("%d|", sqlite3_column_int(res, 0));
printf("%s|", sqlite3_column_text(res, 1));
printf("%s|", sqlite3_column_text(res, 2));
printf("%s|", sqlite3_column_text(res, 3));
printf("%u\n", sqlite3_column_int(res, 4));
rec_count++;
}
printf("=======================================\n");
printf("We received %d records.\nTotal rows=%d\n",rec_count,SQLITE_ROW);
sqlite3_finalize(res);
sqlite3_close(conn);
return 0;
}
Upvotes: 0
Views: 661
Reputation: 24766
Here issue is not about SQL.
You can't use +
operator for char
array concatenation. For that you have to use sprintf
. see this example
char buffer [50];
char* a="aaaa", b="bbbbbbb";
sprintf (buffer, "%s plus %s , a, b);
But I suggest you to use std::string
instead of char
array if you programming in C++.
How to use string
#include <iostream>
#include <string>
int main()
{
std::string name="",username="",password="";
std::cout << "name : "; std::cin >> name;
std::cout << "username : "; std::cin >> username;
std::cout << "password : "; std::cin >> password;
std::string query = "INSERT INTO login (name,username,password) VALUES ('" + username + "','" + name + "','" + password + "');";
std::cout << query << std::endl;
return 0;
}
Upvotes: 1