/*************************************************/ //name:connect to mysql
//writter:1jjk
//complie: gcc -Wall -O2 -o b b.c -I/usr/local/mysql/include/mysql/ -lmysqlclient
//mail:lingjiujianke@gmail.com
/*************************************************/ #include<mysql.h> #include<stdio.h> #include<stdlib.h> #include<unistd.h>
MYSQL *connect_mysql(char *hostname, char *username, char *passwd, char *dbname, \ unsigned portnum, char *socketname, unsigned flags) { MYSQL *conn; conn = mysql_init(NULL); if (conn == NULL) { return(NULL); } if (mysql_real_connect(conn, hostname, username, passwd, dbname, portnum, \ socketname, flags) == NULL) { printf("error\n"); return(NULL); } return(conn); }
int main() { FILE * stream; char ch[2048]; char *str=NULL; MYSQL *returnvalue=NULL; char strsql[2048]; returnvalue=connect_mysql("localhost","username","userpasswd","dir",0,NULL,0); stream = fopen("qq.txt","r"); while((str=fgets(ch,2048,stream))!=NULL) { snprintf(strsql,2048,"INSERT INTO `bknet_ftplist` ( `listname` ) VALUES (\"%s\")\n", str); if( mysql_query(returnvalue, strsql) != 0 ) { printf("error\n"); exit(0); } } fclose(stream); return 0; }
|