首页 > 代码库 > C语言操作MYSQL的应用

C语言操作MYSQL的应用

#include <stdio.h>#include <stdlib.h>#include <winsock.h>   //这个非常重要,不然玩不转#include <mysql.h>int main(int argc, char *argv[]){	MYSQL my_connection;	MYSQL_RES *result;	MYSQL_ROW sql_row;	int res;	int rows,i;	mysql_init(&my_connection);	if(mysql_real_connect(&my_connection,"localhost","root","123456","test",0,NULL,0))	{        //添加数据                                                                                 res=mysql_query(&my_connection,"insert into t_title values(50,‘C‘);");                                                                                              //查询数据                                                                                   res=mysql_query(&my_connection,"select id,title from t_title;");                                                                                      if(!res)        {            result=mysql_store_result(&my_connection);            if(result)            {                rows=mysql_num_rows(result);                printf("数据总数:%d\n",rows);                                for(i=0;i<rows;i++)                {                   sql_row=mysql_fetch_row(result);                   printf("ID:%s\tTITLE:%s\n",sql_row[0],sql_row[1]);                }            }        }               mysql_free_result(result);                                                         		//关闭数据库连接 		mysql_close(&my_connection);	}	system("pause");	return 0;}

  

C语言操作MYSQL的应用