#include<th.h>
#include<sqlite3.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
int do_insert(sqlite3 * db,char * word,char * paraphrase)
{
char sql[123] = "";
sprintf(sql,"insert into stu values (\"%s\",\"%s\")",word,paraphrase);
char * errmsg = NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_exec:%s __%d__\n",errmsg,__LINE__);
return -1;
}
return 0;
}
int main(int argc, const char *argv[])
{
sqlite3 * db;
if(sqlite3_open("./sq.db",&db) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_open: %s %d __%d__\n",\
sqlite3_errmsg(db),sqlite3_errcode(db),__LINE__);
return -1;
}
printf("sqlite3_open success\n");
char sql[123] = "create table if not exists stu (words char, paraphrase char);";
char * errmsg = NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_exec:%s __%d__\n",errmsg,__LINE__);
return -1;
}
printf("create table stu success\n");
FILE * fp = fopen("./dict.txt","r");
if(fp == NULL)
{
ERR_MSG("fopen error");
return -1;
}
char buf[123] = "";
int i = 0;
int j = 0;
char word[50] = "";
char paraphrase[50] = "";
while(fgets(buf,sizeof(buf),fp) != NULL)
{
bzero(word,sizeof(word));
bzero(paraphrase,sizeof(paraphrase));
for(i = 0; i < sizeof(buf); i++)
{
if(buf[i] == '.')
{ int fd;
if(fd = open("dict.txt",O_RDONLY) == -1)
{
ERR_MSG("open error");
return -1;
}
for(j = i - 1; j > 0; j--)
{
if(buf[j] == ' ')
{
buf[j - 1] = 0;
break;
}
}
}
}
sprintf(word,"%s",buf);
sprintf(paraphrase,"%s",buf + j);
if(do_insert(db,word,paraphrase) < 0)
{
break;
}
bzero(buf,sizeof(buf));
}
if(sqlite3_close(db) != SQLITE_OK)
{
return -1;
}
printf("close success\n");
return 0;
}
DAY41
最新推荐文章于 2024-08-17 11:04:06 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)