c language mysql database access method

Reference Example:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<mysql.h>
#define MAX_COLUMN_LEN 32
int main(int argc , char *argv[])
{
MYSQL db;
MYSQL_RES *res;
MYSQL_ROW row;

//初始化数据库
mysql_init(&db);
//连接数据库
if(mysql_real_connect(&db,"127.0.0.1","root","huawei","information_schema",0,NULL,0))
{
printf("connect!!!\n");
}

//查询
if (mysql_real_query(&db, "select * from CHARACTER_SETS", (unsigned long)strlen("select * from CHARACTER_SETS")))
{
printf("mysql_real_query failed\n");
return 0;res = mysql_store_result (& db);// store the result set
}



IF (RES == NULL)
{
the printf ( "mysql_store_result failed \ n-");
return 0;
}

// repeat reading line, and the output value of the first field, until the row is NULL
the while (row = mysql_fetch_row (RES) )
{
the printf ( "% S% S% S% S \ n-", Row [0], Row [. 1], Row [2], Row [. 3]);
}

// set the result release
mysql_free_result (RES);
/ / close connection Mysql
mysql_close (& DB);


return 0;
}

Guess you like

Origin www.cnblogs.com/longzhiwen/p/11487771.html