sqlite3代码实现数据库的插入、删除、修改、退出功能文章来源地址https://www.toymoban.com/news/detail-654349.html
#include <head.h>
#include <sqlite3.h>
#include <unistd.h>
int do_insert(sqlite3 *db);
int do_delete(sqlite3 *db);
int do_update(sqlite3 *db);
int main(int argc, const char *argv[])
{
sqlite3 *db = NULL;
if(sqlite3_open("./my.db",&db) != SQLITE_OK)
{
fprintf(stdin,"sqlite3_open:%s %d",\
sqlite3_errmsg(db),sqlite3_errcode(db));
return -1;
}
printf("sqlite3_open success\n");
char sql[128] = "create table if not exists Dictionary (word char,TRANSLATORS char)";
char *errmsg = NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_exec:%s %d",errmsg,sqlite3_errcode(db));
return -1;
}
printf("create table Dictionary success\n");
char choose = 0;
while(1)
{
printf("**********************\n");
printf("********1.插入********\n");
printf("********2.删除********\n");
printf("********3.修改********\n");
printf("********4.退出********\n");
printf("**********************\n");
printf("请输入>>> ");
scanf("%s",&choose);
getchar();
switch(choose)
{
case '1': do_insert(db); break;
case '2': do_delete(db); break;
case '3': do_update(db); break;
case '4': goto END; break;
default:printf("输入错误,请重新输入\n");
}
}
END:
if(sqlite3_close(db) != SQLITE_OK)
{
fprintf(stdin,"sqlite3_close:%s %d",\
sqlite3_errmsg(db),sqlite3_errcode(db));
return -1;
}
return 0;
}
int do_update(sqlite3 *db)
{
if(sqlite3_open("./my.db",&db) != SQLITE_OK)
{
fprintf(stdin,"sqlite3_open:%s %d",\
sqlite3_errmsg(db),sqlite3_errcode(db));
return -1;
}
char buf[128] = "";
char buf_w[128] = "";
printf("请输入要修改的单词>>> ");
scanf("%s",buf);
printf("请输入要修改单词的翻译>>> ");
scanf("%s",buf_w);
char *errmsg;
char sql[128] = "";
sprintf(sql,"update Dictionary set TRANSLATORS='%s' where word='%s'",buf,buf_w);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_exec:%s %d\n",errmsg,sqlite3_errcode(db));
return -1;
}
printf("do_update success\n");
return 0;
}
int do_insert(sqlite3 *db)
{
if(sqlite3_open("./my.db",&db) != SQLITE_OK)
{
fprintf(stdin,"sqlite3_open:%s %d",\
sqlite3_errmsg(db),sqlite3_errcode(db));
return -1;
}
char *errmsg;
char sql[128] = "";
char buf_word[128] = "";
char buf_TRAN[128] = "";
printf("请输入单词>>> ");
scanf("%s",buf_word);
printf("请输入翻译>>> ");
scanf("%s",buf_TRAN);
sprintf(sql,"insert into Dictionary values ('%s','%s')",buf_word,buf_TRAN);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_exec:%s %d\n",errmsg,sqlite3_errcode(db));
return -1;
}
printf("do_insert success\n");
return 0;
}
int do_delete(sqlite3 *db)
{
if(sqlite3_open("./my.db",&db) != SQLITE_OK)
{
fprintf(stdin,"sqlite3_open:%s %d",\
sqlite3_errmsg(db),sqlite3_errcode(db));
return -1;
}
char *errmsg;
char buf[128] = "";
printf("请输入要删除的单词>>> ");
scanf("%s",buf);
char sql[128] = "";
sprintf(sql,"delete from Dictionary where word='%s'",buf);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_exec:%s %d\n",errmsg,sqlite3_errcode(db));
return -1;
}
printf("do_delete success\n");
}
文章来源:https://www.toymoban.com/news/detail-654349.html
到了这里,关于sqlite3数据库的实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!