MySQL数据库简单用法

时间:2022-03-12 19:47:44

 

#include <Windows.h>
#include <mysql.h>
#include <iostream>
#include <string.h>


using namespace std;
#pragma comment (lib,"libmysql.lib")
int main()
{
MYSQL myCont;
int res = 0;
mysql_init(&myCont);
MYSQL_RES *result;
MYSQL_ROW sql_row;
MYSQL_FIELD *fd;
char column[32][32];
if(mysql_real_connect(&myCont,"192.162.4.145","root","root","db",3306,NULL,0))
{
cout<<"db connect succeed!\n";
mysql_query(&myCont,"SET NAMES GBK");//设置编码格式,否则在cmd下无法显示中文
res = mysql_query(&myCont,"select * from sp_admins");
if(!res)
{
result = mysql_store_result(&myCont);
if(result)
{
int i,j;
cout<<"number of result: "<<(unsigned long)mysql_num_rows(result)<<endl;
for(i=0;fd=mysql_fetch_field(result);i++)//获取列名
{
strcpy(column[i],fd->name);
}
j=mysql_num_fields(result);
for(i=0;i<j;i++)
{
printf("%s\t",column[i]);
}
printf("\n");
while(sql_row=mysql_fetch_row(result))//获取具体的数据
{
for(i=0;i<j;i++)
{
printf("%s\n",sql_row[i]);
}
printf("\n");
}

}
}
//res = mysql_query(&myCont,"insert into tmc_db values")
}
return 0;
}