head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <sqlite3.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <pthread.h>
#include <sys/select.h>
#include <sys/time.h>
#include <poll.h>
#include <sys/un.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d ",__LINE__);\
perror(msg);\
}while(0)
#define ERR_MSG_1(msg) ERR_MSG(msg);return -1
#define IF_RET_ERR(ret,fi,msg) do{\
if (ret fi)\
{\
ERR_MSG_1(msg);\
}\
}while(0)
#define IF_RET_ERR_0(ret,msg) IF_RET_ERR(ret,<0,msg)
#define IF_SQL_EXEC_ERR(ret) do{\
if (ret !=SQLITE_OK)\
{\
fprintf(stderr,"line:%d %s\n",__LINE__,errmsg);\
return -1;\
}\
}while(0)
#define SUCCESS(msg) printf(msg " success __%d__\n",__LINE__)
#endif
main.c
#include "head.h"
int main(int argc, const char *argv[])
{
//创建并打开数据库
sqlite3 *db=NULL;
if(sqlite3_open("./mydict.db",&db)!=SQLITE_OK)
{
fprintf(stderr,"__%d__sqlite3_open failed\n",__LINE__);
}
SUCCESS("sqlite3_open");
//创建表格
char sql[128]="create table if not exists dict(word char,trans char)";
char * errmsg;
IF_SQL_EXEC_ERR(sqlite3_exec(db,sql,NULL,NULL,&errmsg));
SUCCESS("table stu create");
FILE* fp=fopen("./dict.txt","r");
IF_RET_ERR(fp,==NULL,"fopen");
char buf[128];
char word[128];
char *trans=NULL;
while (1)
{
bzero(buf,sizeof(buf));
bzero(word,sizeof(word));
if(fgets(buf,sizeof(buf),fp)==NULL)
{
break;
}
sscanf(buf,"%s",word);
trans=buf;
LOOP:
while(*trans++!=' ');
if(*trans==' ')
{
trans+=2;
}
else
{
goto LOOP;
}
//printf("%s:%s",word,translation);
bzero(sql,sizeof(sql));
sprintf(sql,"insert into dict values(\"%s\",\"%s\")",word,trans);
//printf("%s\n",sql);
IF_SQL_EXEC_ERR(sqlite3_exec(db,sql,NULL,NULL,&errmsg));
}
END:
//关闭数据库
if(sqlite3_close(db)!=SQLITE_OK)
{
fprintf(stderr,"__%d__sqlite3_close failed\n",__LINE__);
}
fclose(fp);
return 0;
}