一、插入
#include <stdio.h>
#include <sqlite3.h>
void do_insert(sqlite3 *db);
int main(int argc, const char *argv[])
{
//创建并打开一个数据库
sqlite3 *db = NULL;
if(sqlite3_open("./mysql.db",&db) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_open: %s __%d__\n",sqlite3_errmsg(db),__LINE__);
return -1;
}
printf("open datebase success...\n");
//创建一个表
char *sql = "create table if not exists stu (id int,name char,score float);";
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("create table stu success...\n");
char choose = 0;
while(1)
{
printf("------------------------------\n");
printf("----------1.INSERT------------\n");
printf("----------2.DELETE------------\n");
printf("----------3.UPDATE------------\n");
printf("----------4.SELECT------------\n");
printf("----------5.EXIT--------------\n");
printf("------------------------------\n");
printf("please enter>>>");
choose = getchar();
while(getchar()!=10);
switch(choose)
{
case '1':
do_insert(db);break;
case '2':
// do_delete();break;
case '3':
// do_update();break;
case '4':
// do_select();break;
case '5':
goto END;
default:
printf("enter error\n");
}
}
END:
//关闭数据库
if(sqlite3_close(db) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_close: %s __%d__\n",sqlite3_errmsg(db),__LINE__);
return -1;
}
return 0;
}
void do_insert(sqlite3 *db)
{
char sql[128] = "";
char id = 0;
char name[20] = "";
float score = -1;
char *errmsg = NULL;
printf("please enter id>>>");
id = getchar();
getchar();
printf("please enter name>>>");
scanf("%s",name);
getchar();
printf("please enter score>>>");
scanf("%f",&score);
getchar();
sprintf(sql,"insert into stu values (%d,'%s',%f);",id,name,score);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_exec: %s __%d__\n",errmsg,__LINE__);
return;
}
printf("insert success...\n");
return;
}
二、导入电子词典
#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
void do_insert(sqlite3 *db);
int main(int argc, const char *argv[])
{
//创建并打开一个数据库
sqlite3 *db = NULL;
if(sqlite3_open("./mysql.db",&db) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_open: %s __%d__\n",sqlite3_errmsg(db),__LINE__);
return -1;
}
printf("open datebase success...\n");
//创建一个表
char *sql = "create table if not exists dict (id int,words char,trans 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("create table stu success...\n");
//插入单词
int res = -1;
int i = 0;
char c = 0;
char words[64] = "";
char trans[128] = "";
char mysql[256] = "";
FILE *fp = fopen("./dict.txt","r");
if(fp == NULL)
{
perror("open");
return -1;
}
while(1)
{
bzero(words,sizeof(words));
res = fscanf(fp,"%s",words);
if(res == EOF)
break;
while(1)
{
bzero(trans,sizeof(trans));
fscanf(fp,"%c",trans);
if(strcmp(trans," ") != 0)
break;
}
fseek(fp,-1,SEEK_CUR);
bzero(trans,sizeof(trans));
while(1)
{
fscanf(fp,"%s",trans+strlen(trans));
fscanf(fp,"%c",&c);
if(c == '\n')
break;
}
sprintf(mysql,"insert into dict values (%d,'%s','%s');",++i,words,trans);
if(sqlite3_exec(db,mysql,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_exec: %s %d __%d__\n",errmsg,sqlite3_errcode(db),__LINE__);
return -1;
}
}
//关闭数据库
if(sqlite3_close(db) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_close: %s __%d__\n",sqlite3_errmsg(db),__LINE__);
return -1;
}
return 0;
}
文章来源地址https://www.toymoban.com/news/detail-661715.html
文章来源:https://www.toymoban.com/news/detail-661715.html
到了这里,关于通过C实现sqlite3操作,(增删改查),导入电子词典的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!