gpt4 book ai didi

c++ - 如何使用 cpp 程序将值插入数据库(mySql)?

转载 作者:行者123 更新时间:2023-11-29 19:53:22 24 4
gpt4 key购买 nike

void SignUp()
{
MYSQL* conn;
MYSQL_ROW row;
MYSQL_RES* res;
int nQueryState = 0;
int nId = 0;
char szName[45];
char szPassWord[45];
char szQuestion[45];
char szPhone[45];

cout<<"Enter the Id";
cin>>nId;

cout<<"Enter the Name";
cin>>szName;

cout<<"Enter the Password";
cin>>szPassWord;

cout<<"Enter the Question";
cin>>szQuestion;

cout<<"Enter the Phone";
cin>>szPhone;

conn = mysql_init(0);
conn =mysql_real_connect(conn,"localhost","root","12356","hari",0,NULL,0);
nQueryState = mysql_query(conn,"insert into userdetails values(nId,szName,szPassword,szQuestion,szPhone)");

if(0!=nQueryState)
{
cout<<"\n\nConnection not Established";
}
}

我想将详细信息输入到名为 userdetails 的表中。它给我一个输出,使得连接未建立。请检查它并帮助我。我正在使用 codeblock ide 和 mySql5.1

最佳答案

尝试如下:

MYSQL mysql,*connection;
MYSQL_RES result;
MYSQL_ROW row;


mysql_init(&mysql);
connection =mysql_real_connect(&mysql,"localhost","root","12356","hari",0,NULL,0);

if (connection==NULL)
{
cout<<mysql_error(&mysql)<<endl;
}
else{
nQueryState = mysql_query(&mysql,"insert into userdetails values('"+nId+"','"+szName+"','"+szPassword+"','"+szQuestion+"','"+szPhone+"')");

if (nQueryState !=0) {
cout << mysql_error(connection) << endl;
return 1;
}
}

mysql_close(&mysql);

引用:http://www.codeproject.com/Questions/337901/How-to-insert-data-in-database-using-Cplusplus

关于c++ - 如何使用 cpp 程序将值插入数据库(mySql)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40759004/

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