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[],argv[],argv[]};
connect_mysql(&mysql,str,&connect);
menu();
quit(&connect);
return ;
}
void set_char(MYSQL *mysql)//设置数据库的字符集 要先做
{
int i = mysql_query(mysql,"set names utf8");//向mysql服务器端发送指令
//成功则返回0,失败则返回非0数据
if(i != )
{
printf("set char fail!!\n");
return;
} }
void select_mysql(MYSQL *mysql,MYSQL **connect)
{
if(!mysql)
return;
else{
write(,"Input select SQL:\n",strlen("Input select SQL:\n"));
char select_sql[];
int n=;
memset(select_sql,,sizeof(select_sql));
for(n;;n++)//读取标准输入直到;在去除;
{
select_sql[n]=getchar();
if(select_sql[n] == ';')
{
select_sql[n]=;
break;
}
}
char * SQL = select_sql;
int i = mysql_query(mysql,SQL);
if(i != )
{
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=;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 != )
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=;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=;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(,"Input update SQL:\n",strlen("Input update SQL:\n"));
char update_sql[];
int n=;
memset(update_sql,,sizeof(update_sql));
for(n;;n++)
{
update_sql[n]=getchar();
if(update_sql[n] == ';')
{
update_sql[n]=;
break;
}
}
char * SQL = update_sql;
int i = mysql_query(mysql,SQL);
if(i != )
{
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(,"Input delete SQL:\n",strlen("Input delete SQL:\n"));
char delete_sql[];
int n=;
memset(delete_sql,,sizeof(delete_sql));
for(n;;n++)
{
delete_sql[n]=getchar();
if(delete_sql[n] == ';')
{
delete_sql[n]=;
break;
}
}
char * SQL = delete_sql;
int i = mysql_query(mysql,SQL);
if(i != )
{
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(,"Input insert SQL:\n",strlen("Input insert SQL:\n"));
char insert_sql[];
int n=;
memset(insert_sql,,sizeof(insert_sql));
for(n;;n++)
{
insert_sql[n]=getchar();
if(insert_sql[n] == ';')
{
insert_sql[n]=;
break;
}
}
char * SQL = insert_sql;
int i = mysql_query(mysql,SQL);
if(i != )
{
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[];
memset(passwd,,sizeof(passwd));
write(,"passwd:",strlen("passwd:"));
scanf("%s",passwd);
passwd[strlen(passwd)]=;
mysql_init(mysql);
*connect = mysql_real_connect(mysql,str[],str[],passwd,str[],,,);
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();
}
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(,"Input: ",strlen("Input: "));
}

1、找不到mysql.h,请安装

sudo apt-get install libmysqlclient-dev

2、编译不过,加 -lmysqlclient

参考:http://www.oschina.net/code/snippet_2325404_47438

05-08 08:04