create database test;
use test;
create table hanzi (id int(11),name varchar(20));
hanzi.c
#include <fcntl.h>
#include <ldap.h>
#include <iconv.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdarg.h>
#ifdef _WIN32
#include "c:/mysql/include/mysql.h"
#else
#include <mysql/mysql.h>
#include <signal.h>
#include <pthread.h>
#endif
#define OUTLEN 256
void mysql_exec(MYSQL *ldap_billingd,char *sql)
{
if ((mysql_query(ldap_billingd,"begin"))<0)
{
printf("ldap_billingd: %s",mysql_error(ldap_billingd));
mysql_query(ldap_billingd,"rollback");
}
if ((mysql_query(ldap_billingd,sql))<0)
{
printf("ldap_billingd: %s",mysql_error(ldap_billingd));
mysql_query(ldap_billingd,"rollback");
}
mysql_query(ldap_billingd,"commit");
}
int main(int argc,char **argv)
{
FILE* fd=NULL;
char line[OUTLEN];
char lineold[OUTLEN];
int rc=0;
char out[OUTLEN];
MYSQL *ldap_billingd=NULL;
char *dbhost_billing="127.0.0.1";
char *dbuser_billing="root";
char *dbpassword_billing="";
int dbport_billing=3306;
char sql[1024];
int id=0;
if (argc!=2)
{
printf("usage is :execfilename hanzifilename/n");
exit(0);
}
if ((ldap_billingd= mysql_init(NULL))==NULL)
{
printf("ldap_billingd: %s",mysql_error(ldap_billingd));
}
log("mysql_init is finished! /n");
if (mysql_real_connect(ldap_billingd,dbhost_billing,dbuser_billing,dbpassword_billing,"test",dbport_billing,NULL,0) <0)
{
printf("-----------------ldap_billingd: %s----------------",mysql_error(ldap_billingd));
}
printf("mysql_real_connect is finished! /n");
fd=fopen(argv[1],"r");
if (fd==NULL)
{
printf("mysql real init_init(); fd==NULL had finished!configfile=%s !/n",argv[1]);
exit(1);
}
id=0;
while (!feof(fd))
{
memset(line,0,256);
//------读一行字符,该行的字符数不大于num-1------
fgets(line,256,fd);
memset(sql,0,1024);
sprintf(sql,"insert into hanzi /
values('%d','%s') /
",id,line);
mysql_exec(ldap_billingd, sql);
id++;
}
}
hanzi.txt
张三
李四
王五
gcc -ggdb3 -I/usr/local/mysql/include/ -L/usr/lib/mysql -lmysqlclient -o hanzi hanzifile.c
./hanzi hanzi.txt