gpt4 book ai didi

c++ - SQLite 数据库与 c/c++

转载 作者:行者123 更新时间:2023-11-30 17:08:52 25 4
gpt4 key购买 nike

如何修复此错误

/home/andi/Desktop/latihan/sqlite c++/sqlite insert.cpp:37:76:错误:类型“const char [53]”和“char [50]”到二进制的无效操作数运算符+'

这是代码。我希望我的代码是可读的,抱歉我的英语不好

# 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;
}

最佳答案

这里的问题与 SQL 无关。您不能使用 + 运算符进行 char 数组串联。为此,您必须使用 sprintf。请参阅此示例

  char buffer [50];
char* a="aaaa", b="bbbbbbb";
sprintf (buffer, "%s plus %s , a, b);

但如果您使用 C++ 编程,我建议您使用 std::string 而不是 char 数组。

如何使用字符串

#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;
}

关于c++ - SQLite 数据库与 c/c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33491222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com