c++连接MySQL数据库
c++中连接MySqL数据库
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
int main()
{
MYSQL *mysql;
MYSQL_RES *res;
MYSQL_ROW row;
char query[1024];
int t,r;
mysql=mysql_init(NULL);
if(!mysql_real_connect(mysql,"test","root","123","test",0,NULL,0))
{
printf( "Error connecting to database: %s\n",mysql_error(mysql));
}
else
printf("Connected\n");
sprintf(query,"select * from first");
t=mysql_query(mysql,query);
if(t)
{
printf("Error making query:%s\n",mysql_error(mysql));
}
else
printf("Query made\n");
res=mysql_use_result(mysql);
for(r=0;r<mysql_field_count(mysql);r++)
{
row=mysql_fetch_row(res);
if(row<0)
break;
for(t=0;t<mysql_num_fields(res);t++)
printf("%s",row[t]);
printf("\n");
}
mysql_close(mysql);
return 0;
}
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
int main()
{
MYSQL *mysql;
MYSQL_RES *res;
MYSQL_ROW row;
char query[1024];
int t,r;
mysql=mysql_init(NULL);
if(!mysql_real_connect(mysql,"test","root","123","test",0,NULL,0))
{
printf( "Error connecting to database: %s\n",mysql_error(mysql));
}
else
printf("Connected\n");
sprintf(query,"select * from first");
t=mysql_query(mysql,query);
if(t)
{
printf("Error making query:%s\n",mysql_error(mysql));
}
else
printf("Query made\n");
res=mysql_use_result(mysql);
for(r=0;r<mysql_field_count(mysql);r++)
{
row=mysql_fetch_row(res);
if(row<0)
break;
for(t=0;t<mysql_num_fields(res);t++)
printf("%s",row[t]);
printf("\n");
}
mysql_close(mysql);
return 0;
}