将字典导入数据库中
#include <myhead.h>
int sql_insert(sqlite3* db,char* key,char* value);
int main(int argc, const char *argv[])
{
//打开数据库
sqlite3* db=NULL;
if(sqlite3_open("./dict.db",&db)!=SQLITE_OK){
printf("%d:%s,__%d__\n",sqlite3_errcode(db),sqlite3_errmsg(db),__LINE__);
return -1;
}
printf("打开数据库成功\n");
//打开数据表
char* errmsg=NULL;
char sql[100]="create table if not exists dict (key char,value char);";
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK){
printf("%s,__%d__\n",errmsg,__LINE__);
}
FILE* fd_r=fopen("./dict.txt","r");
char key[50]="";
char value[50]="";
int res1=0;
char* res2=NULL;
//将文本循环读出来
while(1){
res1=fscanf(fd_r,"%s",key);
res2=fgets(value,sizeof(value),fd_r);
if(res1==-1||res2==NULL){
break;
}
//写入数据库
sql_insert(db,key,value);
// printf("%s %s\n",key,value);
}
//关闭数据库
if(sqlite3_close(db)!=SQLITE_OK){
printf("%d:%s,__%d__\n",sqlite3_errcode(db),sqlite3_errmsg(db),__LINE__);
return -1;
}
printf("关闭数据库成功\n");
fclose(fd_r);
}
int sql_insert(sqlite3* db,char* key,char* value)
{
char sql[100]="";
char* errmsg=NULL;
sprintf(sql,"insert into dict values ('%s','%s');",key,value);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK){
printf("%s,__%d__\n",errmsg,__LINE__);
return -1;
}
return 0;
}
使用 sqlitebrowser dict.db 查看图形化界面