#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
static char* server_groups[]={"embedded","server","this_program_server",(char*)NULL};
int MysqlOpt(char *database,char* username ,char* password,char* address,char* sqlstr)
{
MYSQL mysql;
mysql_library_init(0,NULL,server_groups);//初始化MYSQL数据库
mysql_init(&mysql);//初始化MYSQL标识符,用于连接
if(!mysql_real_connect(&mysql,"localhost","root","root","db_record",0,NULL,0))
{
fprintf(stderr,"无法连接到数据库,错误原因是:%s/n",mysql_error(&mysql));
return -1;
}
mysql_query(&mysql,sqlstr);
mysql_close(&mysql);//关闭数据库连接
mysql_library_end();//关闭MySQL库
return EXIT_SUCCESS;
}
int MysqlQueryFile(char *database,char* username ,char* password,char* address,char* sqlstr,FILE *fp)
{
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
int t,r;
mysql_library_init(0,NULL,server_groups);//初始化MYSQL数据库
mysql_init(&mysql);//初始化MYSQL标识符,用于连接
if(!mysql_real_connect(&mysql,"localhost","root","root","db_record",0,NULL,0))
{
fprintf(stderr,"无法连接到数据库,错误原因是:%s/n",mysql_error(&mysql));
return -1;
}
t=mysql_real_query(&mysql,sqlstr,(unsigned int)strlen(sqlstr));
if(t){
printf("查询数据库失败%s/n",mysql_error(&mysql));
mysql_close(&mysql);//关闭数据库连接
mysql_library_end();//关闭MySQL库
return -1;
}
else {
res=mysql_store_result(&mysql);//返回查询的全部结果集
while(row=mysql_fetch_row(res)){//mysql_fetch_row取结果集的下一行
for(t=0;t<mysql_num_fields(res);t++){//结果集的列的数量
//printf("%s/t",row[t]);
fwrite(row[t], sizeof(char), sizeof(row[t])-1, fp);
}
//printf("/n");
fwrite("\n", sizeof(char), 1, fp);
}
}
mysql_free_result(res);//操作完毕,查询结果集
mysql_close(&mysql);//关闭数据库连接
mysql_library_end();//关闭MySQL库
return EXIT_SUCCESS;
}
const char * MysqlQueryString(char *database,char* username ,char* password,char* address,char* sqlstr)
{
static char result[2048]="null";
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
int t,r;
mysql_library_init(0,NULL,server_groups);//初始化MYSQL数据库
mysql_init(&mysql);//初始化MYSQL标识符,用于连接
if(!mysql_real_connect(&mysql,"localhost","root","root","db_record",0,NULL,0))
{
fprintf(stderr,"无法连接到数据库,错误原因是:%s/n",mysql_error(&mysql));
return result;
}
t=mysql_real_query(&mysql,sqlstr,(unsigned int)strlen(sqlstr));
if(t){
printf("查询数据库失败%s/n",mysql_error(&mysql));
mysql_close(&mysql);//关闭数据库连接
mysql_library_end();//关闭MySQL库
return result;
}
else {
memset(result,0,sizeof(result));
res=mysql_store_result(&mysql);//返回查询的全部结果集
while(row=mysql_fetch_row(res)){//mysql_fetch_row取结果集的下一行
strcat(result,"#");
for(t=0;t<mysql_num_fields(res);t++){//结果集的列的数量
//printf("%s/t",row[t]);
strcat(result,row[t]);
strcat(result,"|");
}
//printf("/n");
}
}
mysql_free_result(res);//操作完毕,查询结果集
mysql_close(&mysql);//关闭数据库连接
mysql_library_end();//关闭MySQL库
return result;
}
int main(){
long ii = 10000000L;
clock_t start, finish;
double duration;
char resu[2048];
memset(resu, '\0', 128);
strcpy(resu, MysqlQueryString("db_record","root","root","localhost","select * from friends;"));
FILE *fp;
fp = fopen("test", "w");
MysqlQueryFile("db_record","root","root","localhost","select * from friends;",fp);
fclose(fp);
MysqlOpt("db_record","root","root","localhost","create table test1(name varchar(20), age varchar(10));;");
printf( "Time to do %ld empty loops is ", ii );
start = clock();
MysqlOpt("db_record","root","root","localhost","insert into test1(name , age )values('aa','bbb');");
printf("%s\n",resu);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%f seconds\n", duration );
return 1;
}
========================================
gcc -g -o MysqlOption MysqlOption.c -I/usr/include/mysql -L/usr/lib -lmysqlclient -lpthread -lm -lrt -ldl
=======================================
pfkj@pfkj-desktop:~/wls/jianggq$ cat main.c
#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
#include <sqlite3.h>
int main()
{
char sql[128];
FILE *fd;
char resu[2048];
int (*sqlopt)(char *database,char* username ,char* password,char* address,char* sqlstr);
int (*sqlsingle)(char *database,char* username ,char* password,char* address,char* sqlstr);
const char * (*sqlquery)(char *database,char* username ,char* password,char* address,char* sqlstr,FILE *fp);
void *handle;
handle=dlopen("./libMysqlOption.so",RTLD_LAZY);//open lib file
sqlopt=dlsym(handle,"MysqlOpt");//call dlsym function
sqlquery=dlsym(handle,"MysqlQueryFile");//call dlsym function
sqlsingle=dlsym(handle,"MysqlQueryString");//call dlsym function
memset(sql, 0, 128);
strcpy(sql, "create table tbtest2( data varchar(20))");
sqlopt("db_record","root","root","localhost",sql);
strcpy(sql, "insert into tbtest2(data) values(\"hello\")");
sqlopt("db_record","root","root","localhost",sql);
fd = fopen("test", "w");
fwrite("Result: \n", sizeof(char), 10, fd);
memset(sql, '\0', 128);
strcpy(sql, "select * from tbtest2");
sqlquery("db_record","root","root","localhost",sql,fd);
memset(resu, '\0', 128);
strcpy(resu, sqlsingle("db_record","root","root","localhost",sql));
printf("%s",resu);
strcpy(sql, "update tbtest2 set data='updaterow' where rowid=1");
sqlopt("db_record","root","root","localhost",sql);
fclose(fd);
dlclose(handle);
return 0;
}