将dict.txt导入到数据库
#include <head.h>
#include <sqlite3.h>
int do_insert(int i,char *str,sqlite3 *db);
int main(int argc, const char *argv[])
{//创建泵打开一个数据库sqlite3 *db = NULL;if(sqlite3_open("./my.db",&db) != SQLITE_OK){fprintf(stderr,"sqlite3_open:[%d] %s __%d__",\sqlite3_errcode(db),sqlite3_errmsg(db),__LINE__);return -1;}printf("打开数据库my.db成功\n");//创建一个表格 在sqlite中 create table stu (字段名 类型,字段名 类型~~~~);char sql[128] = "create table if not exists dict (id int primary key, words char, translate char);";char *errmsg = NULL;if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_exec:[%d] %s __%d__",\sqlite3_errcode(db),sqlite3_errmsg(db),__LINE__);return -1;}printf("create table dict success\n");//以读的方式打开dict.txt文件FILE *fp = fopen("./dict.txt","r");int i = 1;char buf[128] = "";while(1){if(fgets(buf,sizeof(buf),fp) == NULL){break;}do_insert(i++,buf,db);bzero(buf,sizeof(buf));}//关闭数据库if(sqlite3_close(db) != SQLITE_OK){fprintf(stderr,"sqlite3_close:[%d] %s __%d__",\sqlite3_errcode(db),sqlite3_errmsg(db),__LINE__);return -1;}printf("数据库关闭成功\n");fclose(fp);return 0;
}//插入(全文字段插入)
int do_insert(int i,char *str,sqlite3 *db)
{char sql[128] = "";sprintf(sql,"insert into dict values (%d, \"%s);",i,str);char *errmsg = NULL;char *p =NULL;int n = strlen(sql);sql[n-3] = '"';for(int j=32;j<=strlen(sql);j++){if(sql[j] == ' '){sql[j]='"';sql[j+1]=',';sql[j+2]='"';break;}}if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_exec:[%d] %s __%d__\n",\sqlite3_errcode(db),sqlite3_errmsg(db),__LINE__);return -1;}printf("%d insert into dict values success\n",i);return 0;
}
dict文件(部分)
结果为:
一些不成熟的功能
#include <head.h>
#include <sqlite3.h>
int do_insert(sqlite3 *db);
int do_delete(sqlite3 *db);
int do_update(sqlite3 *db);
//int do_select(sqlite3 *db);
int main(int argc, const char *argv[])
{//创建泵打开一个数据库sqlite3 *db = NULL;if(sqlite3_open("./my.db",&db) != SQLITE_OK){fprintf(stderr,"sqlite3_open:[%d] %s __%d__",\sqlite3_errcode(db),sqlite3_errmsg(db),__LINE__);return -1;}printf("打开数据库my.db成功\n");//创建一个表格 在sqlite中 create table stu (字段名 类型,字段名 类型~~~~);char sql[128] = "create table if not exists dict (id int primary key, words char, translate char);";char *errmsg = NULL;if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_exec:[%d] %s __%d__",\sqlite3_errcode(db),sqlite3_errmsg(db),__LINE__);return -1;}printf("create table dict success\n");char choose = 0;while(1){printf("------------------------------\n");printf("-----------1.插入-------------\n");printf("-----------2.删除-------------\n");printf("-----------3.修改-------------\n");printf("-----------4.查询-------------\n");printf("-----------5.退出-------------\n");printf("------------------------------\n");printf("请输入>>> ");choose = getchar();while(getchar() != 10);switch(choose){case '1':do_insert(db);break;case '2':do_delete(db);break;case '3':do_update(db);break;case '4':// do_select(db);break;case '5':goto END;default:printf("输入无效,请重新输入>>> ");}}END://关闭数据库if(sqlite3_close(db) != SQLITE_OK){fprintf(stderr,"sqlite3_close:[%d] %s __%d__",\sqlite3_errcode(db),sqlite3_errmsg(db),__LINE__);return -1;}printf("数据库关闭成功\n");return 0;
}//插入(全文字段插入)
int do_insert(sqlite3 *db)
{int id = 0;char words[128] = "";char translate[128] = "";char sql[128] = "";printf("输入序号 单词 解释\n");scanf("%d %s %s",&id,words,translate);while(getchar() != 10);sprintf(sql,"insert into dict values (%d, '%s', '%s');",id,words,translate);char *errmsg = NULL;if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_exec:[%d] %s __%d__",\sqlite3_errcode(db),sqlite3_errmsg(db),__LINE__);return -1;}printf("insert into dict values success\n");return 0;
}//删除表格中所有的数据
int do_delete(sqlite3 *db)
{char table[128] = "";char sql[128] = "";printf("输入要删除记录的表格\n");scanf("%s",table);while(getchar() != 10);sprintf(sql,"delete from %s;",table);char *errmsg = NULL;if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_exec:[%d] %s __%d__",\sqlite3_errcode(db),sqlite3_errmsg(db),__LINE__);return -1;}printf("delete from %s success\n",table);return 0;
}//修改表格中记录以id号为限制条件
int do_update(sqlite3 *db)
{char table[128] = "";int id = 0;char words[128] = "";char sql[128] = "";printf("请输入要修改的表格,该修改为修改单词,所以输入格式为表格名称 序号 单词\n");scanf("%s %d %s",table,&id,words);while(getchar() != 10);sprintf(sql,"update %s set words='%s' where id=%d;",table,words,id);char *errmsg = NULL;if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_exec:[%d] %s __%d__",\sqlite3_errcode(db),sqlite3_errmsg(db),__LINE__);return -1;}printf("update success\n");return 0;
}
/*
//查看所有记录
int do_select(sqlite3 *db)
{printf("请输入要查看的表格\n");char table[128] = "";char sql[128] = "";scanf("%s",table);while(getchar() != 10);sprintf(sql,"select * from %s;",table);char *errmsg = NULL;if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_exec:[%d] %s __%d__",\sqlite3_errcode(db),sqlite3_errmsg(db),__LINE__);return -1;}printf("select success\n");return 0;
}
*/