C Principal
#include<stdio.h>
#include<sqlite3.h>
#include<fcntl.h>
#include<string.h>
void callback(){
}
int main(int argc, const char *argv[])
{
sqlite3* db;
if(sqlite3_open("./mysq.db",&db)!=SQLITE_OK){
printf("sql open error: %s\n",sqlite3_errmsg(db));
return -1;
}
char* errmsg;
char *sql;
sql="create table if not exists dict(id INTEGER PRIMARY KEY AUTOINCREMENT,name text,explain text);";
int res = sqlite3_exec(db,sql,0,NULL,&errmsg);
if(res!=SQLITE_OK){
fprintf(stderr,"sql open error: %s\n",sqlite3_errmsg(db));
return -1;
}
FILE *fd = fopen("./dict.txt","r");
if(fd<0){
perror("open");
return -1;
}
char buf_val[128]="",buf_value[128]="",str[250]="";
char *scanf_res;
int res_insert;
char readc;
int i=0,j=0,flag=0;
while(1){
sql=NULL;
flag =0;
scanf_res = fgets(str,sizeof(str),fd);
i=0;j=0;
while(1){
if(*(str+i)=='\n')
{
break;
}
if(0==flag)
{
if(*(str+i)==32){
flag=1;
}else{
*(buf_val+i)=*(str+i);
}
}else{
*(buf_value+j)=*(str+i);
j++;
}
i++;
}
if(scanf_res==NULL){
printf("into success\n");
break;
}
sql=sqlite3_mprintf("insert into dict(name,explain)values('%q','%q');",buf_val,buf_value);
res_insert = sqlite3_exec(db,sql,0,0,&errmsg);
if(res_insert!=SQLITE_OK){
printf("sqlerr: %s\n",errmsg);
sqlite3_free(errmsg);
break;
}
bzero(buf_val,sizeof(buf_val));
bzero(buf_value,sizeof(buf_value));
}
if(fclose(fd)<0)
{
perror("close");
return -1;
}
if(sqlite3_close(db)!=SQLITE_OK){
fprintf(stderr,"sql open error: %s\n",sqlite3_errmsg(db));
return -1;
}
return 0;
}