#include<myhead.h>
int main(int argc, const char *argv[])
{
//打开数据库
sqlite3 *db;
if(sqlite3_open("./dict.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[128]="create table if not exists stu1(world char,mean 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("sqlite3_exec success\n");
//打开文件
FILE *fp;
if((fp=fopen("./dict.txt","r"))==NULL)
{
perror("fopen error");
return -1;
}
char buf[128];
char world[128];
char mean[256];
while((fgets(buf,sizeof(buf),fp))!=NULL)
{
bzero(world,sizeof(world));
bzero(mean,sizeof(mean));
int i=0;
while(buf[i++]!=' ');
buf[i]='\0';
strcpy(world,buf);
while(buf[i++]==' ');
strcpy(mean,buf+i);
printf("%s\t",world);
printf("%s\n",mean);
//加入数据库
sprintf(sql,"insert into stu1 values(\"%s\",\"%s\");",world,mean);
errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"sqlite3_exec:%s __%d__\n",errmsg,__LINE__);
return -1;
}
bzero(buf,sizeof(buf));
}
printf("%s\n",world);
//关闭数据库
if(sqlite3_close(db)!=SQLITE_OK)
{
fprintf(stderr,"sqlite3_close:%s %d __%d__\n",\
sqlite3_errmsg(db),sqlite3_errcode(db),__LINE__);
return -1;
}
printf("sqlite3_close success\n");
fclose(fp);
return 0;
}
网络结构day7
于 2023-09-04 21:11:23 首次发布