sqlite3数据库的实现

sqlite3代码实现数据库的插入、删除、修改、退出功能

#include 
#include 
#include 
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");


}

你可能感兴趣的:(数据库,sqlite)