使用sqlite3代码实现将词典导入数据库中文章来源地址https://www.toymoban.com/news/detail-660306.html
#include <head.h>
#include <sqlite3.h>
#include <strings.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
sqlite3 *db = NULL;
if(sqlite3_open("./dict.db",&db) != SQLITE_OK)
{
fprintf(stdin,"sqlite3_open:%s %d",\
sqlite3_errmsg(db),sqlite3_errcode(db));
return -1;
}
printf("sqlite3_open success\n");
char sql_c[128] = "create table if not exists Dictionary (word char,TRANSLATORS char)";
char *errmsg = NULL;
if(sqlite3_exec(db,sql_c,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_exec:%s %d",errmsg,sqlite3_errcode(db));
return -1;
}
printf("create table Dictionary success\n");
FILE * fd = fopen("./dict.txt","r");
if(fd < 0)
{
ERRO_MES("open");
return -1;
}
char buf[128] = "";
char word[128] = "";
char mean[128] = "";
char sql_i[128] = "";
while(1)
{
if(fgets(buf,sizeof(buf),fd) == NULL)
{
break;
}
buf[strlen(buf)-1] = 0;
for(int i=0;i<strlen(buf)-2;i++)
{
if(buf[i] != ' ' && buf[i+1] == ' ')
{
strncpy(word,buf,i+1);
}
else if(buf[i] == ' '&& buf[i+1] == ' ' &&buf[i+2]!= ' ')
{
strcpy(mean,(buf+i+2));
break;
}
}
bzero(sql_i,sizeof(sql_i));
sprintf(sql_i,"insert into Dictionary values ('%s','%s')",word,mean);
if(sqlite3_exec(db,sql_i,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_exec:%s %d",errmsg,sqlite3_errcode(db));
return -1;
}
}
if(sqlite3_close(db) != SQLITE_OK)
{
fprintf(stdin,"sqlite3_close:%s %d",\
sqlite3_errmsg(db),sqlite3_errcode(db));
return -1;
}
printf("sqlite3_close success\n");
fclose(fd);
return 0;
}
文章来源:https://www.toymoban.com/news/detail-660306.html
到了这里,关于sqlite3将词典导入数据库的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!