#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include <sqlite3.h>
/** function: 插入* @param [ in] * @param [out] * @return */
int do_insert(sqlite3* db)
{int ID;char NAME[20];float SCORE;printf("请输入ID:\t");scanf("%d",&ID);printf("请输入NAME:\t");scanf("%s",NAME);printf("请输入SCORE:\t");scanf("%f",&SCORE);char sql[128] = "";sprintf(sql,"insert into stu values(%d,\"%s\",%g);",ID,NAME,SCORE);printf("sql = %s __%d__\n",sql,__LINE__);char *errmsg = NULL;if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){fprintf(stderr,"Line:%d sqlite3_exec:%s\n",__LINE__,errmsg);return -1;}printf("insert into stu success\n");return 0;
}/** function: 删除* @param [ in] * @param [out] * @return */
int do_delete(sqlite3* db)
{char sql[128]="";char str[20]="";int delete_id;printf("请输入要删除的id号>>>>>>");scanf("%d", &delete_id);while(getchar()!=10);sprintf(sql, "delete from stu where id=%d;",delete_id);char* errmsg = NULL;if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK){fprintf(stderr, "__%d__sqlite3_exec: %s\n", __LINE__, errmsg);return -1;}printf("delete table stu success\n");return 0;}/** function: 查找* @param [ in] * @param [out] * @return */
int do_select(sqlite3* db)
{return 0;
}int main(int argc, const char *argv[])
{sqlite3* db = NULL;if(sqlite3_open("./my.db",&db) != SQLITE_OK){printf("Line:%d__sqlite3_open:%s\n",__LINE__,sqlite3_errmsg(db));return -1;}printf("sqlite3_open success\n");//创建表char sql[128] = "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){printf("Line:%d sqlite3_exec:%s\n",__LINE__,sqlite3_errmsg(db));return -1;}printf("create table stu success\n");char c = 0;while(1){printf("----------------------------------\n");printf("--------------1、插入-------------\n");printf("--------------2、删除-------------\n");printf("--------------3、修改-------------\n");printf("--------------4、查询-------------\n");printf("--------------5、退出-------------\n");printf("----------------------------------\n");printf("请输入选项>>>>>>>");c = getchar();while(getchar() != 10);switch(c){case '1':do_insert(db);break;case '2':do_delete(db);break;case '3':// do_update();break;case '4':// do_select(db);break;case '5':goto END;break;default:printf("输入有误,请重新输入>>>>\n");}}END://释放数据库的内存空间if(sqlite3_close(db) != SQLITE_OK){printf("Line:%d__sqlite3_close:%s\n",__LINE__,sqlite3_errmsg(db));return -1;}printf("sqlite3_close success\n");db = NULL;return 0;
}