- 论坛徽章:
- 0
|
有关mysql的c开发
- #include <mysql.h>;
- #include <stdio.h>;
- #include <string.h>;
- main(){
- MYSQL *conn; //Mysql ½Ó¿Ú
- MYSQL_RES *result;
- MYSQL_ROW row;
-
- if((conn = mysql_init(NULL))==NULL){
- fprintf(stderr,"mysql_init() error:%s\n",mysql_error(conn));
- exit(1);
- }
-
- if(mysql_real_connect(conn,dbhost,dbuser,dbpass,dbname,dbport,NULL,0)==NULL){
- fprintf(stderr,"mysql_real_connect() error:%s\n",mysql_error(conn));
- exit(1);
- }
-
- sprintf(query,"select id from mytable");
-
- if( mysql_query(conn,query)!=0|| !(result = mysql_store_result(conn)) ){
- fprintf(stderr,"query error\n");
- return -1;
- }
-
- while((row = mysql_fetch_row(result))!=NULL){
- printf("id=%s\n",row[0]);
- }
-
- mysql_free_result(result);
- mysql_close(conn);
- }
复制代码 |
|