-
-
- #include
- #include
- #include
- #include "/usr/include/mysql/mysql.h"
-
- int main(int argc, char *argv[])
- {
- MYSQL my_connection;
- MYSQL_RES *res_ptr;
- MYSQL_ROW sqlrow;
- MYSQL_FIELD *fd;
- char aszflds[25][25];
- int res;
- int i,j,k;
-
- mysql_init(&my_connection);
-
-
- if (mysql_real_connect(&my_connection, "localhost", "root", "yourpasswd","cusemysql",0,NULL,CLIENT_FOUND_ROWS))
- {
- printf("Connection success\n");
- res = mysql_query(&my_connection, "select childno,fname,age from children where age<30");
-
- if (res)
- {
- printf("SELECT error:%s\n",mysql_error(&my_connection));
- }
- else
- {
- res_ptr=mysql_store_result(&my_connection);
- if(res_ptr)
- {
- printf("Retrieved %lu Rows\n",(unsigned long)mysql_num_rows(res_ptr));
-
- for(i=0;fd=mysql_fetch_field(res_ptr);i++)
- strcpy(aszflds[i],fd->name);
-
- printf("下面是检索出的各条记录信息:\n");
- j=mysql_num_fields(res_ptr);
- for(i=0;i
- printf("%s\t",aszflds[i]);
- printf("\n");
- while((sqlrow=mysql_fetch_row(res_ptr)))
- {
- for(i=0;i
- printf("%s\t",sqlrow[i]);
- printf("\n");
- }
- if (mysql_errno(&my_connection))
- {
- fprintf(stderr,"Retrive error:s\n",mysql_error(&my_connection));
- }
- }
- mysql_free_result(res_ptr);
- }
- mysql_close(&my_connection);
- }
-
- else
- {
- fprintf(stderr, "Connection failed\n");
-
- if (mysql_errno(&my_connection))
- {
- fprintf(stderr, "Connection error %d: %s\n",
- mysql_errno(&my_connection),
- mysql_error(&my_connection));
- }
- }
- return EXIT_SUCCESS;
- }
编译方法之一:gcc -o select1 select1.c `mysql_config --cflags --libs`
下面是我机子上的演示:
root@pipal-desktop:/home/pipal/mysql# gcc select1.c -o select1 `mysql_config --cflags --libs`
root@pipal-desktop:/home/pipal/mysql# ./select1
Connection success
Retrieved 3 Rows
下面是检索出的各条记录信息:
childno fname age
5 flower 10
10 Ann 5
20 花儿 22
此文章来自:http://pipal.javaeye.com/blog/288132
阅读(2823) | 评论(0) | 转发(0) |