数据库 --> MySQL使用
MySQL使用
代码:
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>
#include <unistd.h> //write头文件 #include <mysql/mysql.h>//包含MySQL头文件 #include <stdio.h> #include <string.h> #include <stdlib.h>
void connect_mysql(MYSQL * mysql ,char * str[],MYSQL **connect); void insert_mysql(MYSQL * mysql); void delete_mysql(MYSQL * mysql); void update_mysql(MYSQL * mysql); void select_mysql(MYSQL * mysql,MYSQL **connect); void show_table(MYSQL * mysql,MYSQL **connect); void set_char(MYSQL * mysql); void menu(); void quit(MYSQL **connect);
int main(int argc,char *argv[]) { MYSQL mysql, *connect = NULL; char *str[] = {argv[1],argv[2],argv[3]}; connect_mysql(&mysql,str,&connect); menu(); quit(&connect); return 0; } void set_char(MYSQL *mysql)//设置数据库的字符集 要先做 { int i = mysql_query(mysql,"set names utf8");//向mysql服务器端发送指令 //成功则返回0,失败则返回非0数据 if(i != 0) { printf("set char fail!!\n"); return; } } void select_mysql(MYSQL *mysql,MYSQL **connect) { if(!mysql) return; else{ write(1,"Input select SQL:\n",strlen("Input select SQL:\n")); char select_sql[256]; int n=0; memset(select_sql,0,sizeof(select_sql)); for(n;;n++)//读取标准输入直到;在去除; { select_sql[n]=getchar(); if(select_sql[n] == ';') { select_sql[n]=0; break; } } char * SQL = select_sql; int i = mysql_query(mysql,SQL); if(i != 0 ) { printf("Update SQL fail %s \n",mysql_error(mysql)); return; }else printf("Update SQL success\n"); } MYSQL_RES *res = mysql_store_result(mysql);//返回结果集放入MYSQL_RES中 int num_fields = mysql_num_fields(res);//查看结果集中行数 MYSQL_ROW row; while((row = mysql_fetch_row(res)))//检索一个结果集合的下一行。当在mysql_store_result()之后使用时, //没有更多的行可检索时,mysql_etch_row()返回NULL { int i; for(i=0;i<num_fields;i++) { printf("%s\n",row[i]); } } mysql_free_result(res);//必须清空结果集######## } void show_table(MYSQL *mysql,MYSQL **connect) { int i = mysql_query(mysql,"show tables"); if(i != 0) return; else{ MYSQL_RES * result = mysql_store_result(*connect); int num_fields = mysql_num_fields(result); MYSQL_ROW row; while((row = mysql_fetch_row(result))) { int i; for(i=0;i<num_fields;i++) { char *SQL; sprintf(SQL,"desc %s",row[i]); int n = mysql_query(mysql,SQL); MYSQL_RES * res= mysql_store_result(*connect); //可以调用mysql_fetch_row()来获取结果集中的行,或调用mysql_row_seek() //和mysql_row_tell()来获取或设置结果集中的当前行位置。 //一旦完成了对结果集的操作,必须调用mysql_free_result()。 int table_num = mysql_num_fields(res); MYSQL_ROW row1; while((row1 = mysql_fetch_row(res))) { int j; for(j=0;j<table_num;j++) { printf("%s\n",row1[j]); } } mysql_free_result(res); } } mysql_free_result(result); } } void update_mysql(MYSQL *mysql) { if(!mysql) return; else{ write(1,"Input update SQL:\n",strlen("Input update SQL:\n")); char update_sql[256]; int n=0; memset(update_sql,0,sizeof(update_sql)); for(n;;n++) { update_sql[n]=getchar(); if(update_sql[n] == ';') { update_sql[n]=0; break; } } char * SQL = update_sql; int i = mysql_query(mysql,SQL); if(i != 0 ) { printf("Update SQL fail %s \n",mysql_error(mysql)); return; }else printf("Update SQL success\n"); } } void delete_mysql(MYSQL *mysql) { if(!mysql) return; else{ write(1,"Input delete SQL:\n",strlen("Input delete SQL:\n")); char delete_sql[256]; int n=0; memset(delete_sql,0,sizeof(delete_sql)); for(n;;n++) { delete_sql[n]=getchar(); if(delete_sql[n] == ';') { delete_sql[n]=0; break; } } char * SQL = delete_sql; int i = mysql_query(mysql,SQL); if(i != 0 ) { printf("Delete SQL fail %s \n",mysql_error(mysql)); return; }else printf("Delete SQL success\n"); } } void insert_mysql(MYSQL *mysql) { if(!mysql) return; else{ write(1,"Input insert SQL:\n",strlen("Input insert SQL:\n")); char insert_sql[256]; int n=0; memset(insert_sql,0,sizeof(insert_sql)); for(n;;n++) { insert_sql[n]=getchar(); if(insert_sql[n] == ';') { insert_sql[n]=0; break; } } char * SQL = insert_sql; int i = mysql_query(mysql,SQL); if(i != 0 ) { printf("Insert SQL fail %s \n",mysql_error(mysql)); return; }else printf("Insert SQL success\n"); } } void connect_mysql(MYSQL *mysql,char *str[],MYSQL **connect) { char passwd[256]; memset(passwd,0,sizeof(passwd)); write(1,"passwd:",strlen("passwd:")); scanf("%s",passwd); passwd[strlen(passwd)]=0; mysql_init(mysql); *connect = mysql_real_connect(mysql,str[1],str[2],passwd,str[3],0,0,0); if(*connect == NULL) { printf("Connect database fail! %s\n",mysql_error(mysql)); return; }else{ printf("Connect success!\n"); } } void quit(MYSQL **connect) { mysql_close(*connect); *connect = NULL; printf("Bye\n"); exit(1); } void menu() { printf("###########################\n"); printf(" A -> SELECT:\n"); printf(" B -> UPDATE:\n"); printf(" C -> DELETE:\n"); printf(" D -> INSERT:\n"); printf(" E -> DESC TABLE:\n"); printf(" Q -> QUIT:\n"); printf("###########################\n"); write(1,"Input: ",strlen("Input: ")); }
1、找不到mysql.h,请安装
sudo apt-get install libmysqlclient-dev
2、编译不过,加 -lmysqlclient
参考:http://www.oschina.net/code/snippet_2325404_47438