发现了一篇文章,感觉还不错,不过没测试,是不是在linux下能用
http://www.unix-cd.com/unixcd12/article_view.asp?id=4138贴一段以前在windows上使用mysql的代码。
1#include <stdio.h>
2#include <string.h>
3#include <winsock2.h>
4#include "mysql.h"
5
6#pragma comment(lib,"libmySQL.lib")
7
8void main(){
9 MYSQL *mysql;
10 MYSQL_RES *res;
11 MYSQL_ROW row;
12 char *query;
13 int t,r;
14
15
16 mysql=mysql_init(mysql);
17 if (!mysql_real_connect(mysql,"localhost","root","","ceshi",0,NULL,0))
18 {
19 printf( "Error connecting to database: %s\n",mysql_error(mysql));
20 }
21 else
22 printf("Connected\n");
23
24 query="Insert Into admin(username,password) values('whoami','iampf')";
25 t=mysql_real_query(mysql,query,(unsigned int)strlen(query));
26 if(t)
27 {
28 printf("Insert Error:%s\n",mysql_error(mysql));
29 }
30
31 query="select * from admin";
32 t=mysql_real_query(mysql,query,(unsigned int) strlen(query));
33 if (t)
34 {
35 printf("Error making query: %s\n",
36 mysql_error(mysql));
37 }
38 else printf("Query made\n");
39 res=mysql_use_result(mysql);
40 for(r=0;r<mysql_field_count(mysql);r++){
41 row=mysql_fetch_row(res);
42 if(row<0) break;
43 for(t=0;t<mysql_num_fields(res);t++){
44 printf("%s ",row[t]);
45 }
46 printf("\n");
47 }
48 mysql_close(mysql);
49}
50
51