当前位置:   article > 正文

嵌入式数据库sqlite3_常见的嵌入式数据库

常见的嵌入式数据库

目录

一、数据库

数据库的基本概念​编辑

 常用的数据库

基于嵌入式的数据库

二、SQLite

SQLite基础

SQLite安装

安装可视化工具

数据库命令:   

​编辑

​编辑

三、sqlite3 数据库 C语言 API 

四、基于数据库的学生管理系统

​编辑

五、一个小作业


一、数据库

数据库的基本概念

 常用的数据库

大型数据库

Oracle公司是最早开发关系数据库的厂商之一,其产品支持最广泛的操作系统平台。目前Oracle关系数据库产品的市场占有率名列前茅。

IBM 的DB2是第一个具备网上功能的多媒体关系数据库管理系统,支持包Linux在内的一系列平台。

中型数据库

Server是微软开发的数据库产品,主要支持windows平台。

小型数据库

mySQL是一个小型关系型数据库管理系统,开发者为瑞典MySQL AB公司,2008年被Sun公司收购,开放源码。现在Sun又被Oracle收购了

数据库种类大全及区别_seniorShen的博客-CSDN博客_数据库分类

我接触过的第一款就是免费开源的MySQL,在JavaWeb开发中常用

后面学习大数据接触到了第一个NoSQL它叫Hbase  ,这些非关系型数据库常用于大数据处理中。 

基于嵌入式的数据库

基于嵌入式Linux的数据库主要有SQLite, Firebird, Berkeley DB, eXtremeDB

Firebird是关系型数据库,功能强大,支持存储过程、SQL兼容等

SQLite关系型数据库,体积小,支持ACID事务

Berkeley DB中并没有数据库服务器的概念,它的程序库直接链接到应用程序中

eXtremeDB是内存数据库,运行效率

嵌入式数据库_百度百科

二、SQLite

SQLite基础

SQLite的源代码是C,其源代码完全开放。SQLite第一个Alpha版本诞生于20005月。 他是一个轻量级的嵌入式数据库

SQLite有以下特性

零配置一无需安装和管理配置;

储存在单一磁盘文件中的一个完整的数据库;

数据库文件可以在不同字节顺序的机器间自由共享;

支持数据库大小至2TB

足够小,全部源码大致3万行c代码,250KB

比目前流行的大多数数据库对数据的操作要快;

SQLite安装

准备好安装包

 

这时候dpkg的弊端就出现了,老师给的安装包太老了,相关依赖都木有,所以还是apt安装吧

先更新下软件包sudo apt update

 报错了

先执行

sudo pkill -KILL appstreamcli

在执行

wget -P /tmp https://launchpad.net/ubuntu/+archive/primary/+files/appstream_0.9.4-1ubuntu1_amd64.deb https://launchpad.net/ubuntu/+archive/primary/+files/libappstream3_0.9.4-1ubuntu1_amd64.deb 

 最后执行

sudo dpkg -i /tmp/appstream_0.9.4-1ubuntu1_amd64.deb /tmp/libappstream3_0.9.4-1ubuntu1_amd64.deb

运行完了再来更新

 

我真是yue了,算了先不管它

直接  sudo apt install sqlite3

 吐了呀

如何解决apt-get中Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify的问题_疏狂的博客-CSDN博客_unmet dependencies. try 'apt --fix-broken install'

 按照这个解决一下第一步   sudo apt --fix-broken install

 太好了没报错

 我在更新

爱了爱了

 成功好像在招手

查看版本

安装可视化工具

sudo apt-get install sqlitebrowser

sqlitebrowser 数据库名查看数据库

数据库命令:   

1)系统命令 , 都以'.'开头
         .help   帮助

         .exit      退出
         .quit    退出sql命令行
         .table   查看表
         .schema  查看表的结构

英文文档

.archive ...           Manage SQL archives: ".archive --help" for details
.auth ON|OFF           Show authorizer callbacks
.backup ?DB? FILE      Backup DB (default "main") to FILE
.bail on|off           Stop after hitting an error.  Default OFF
.binary on|off         Turn binary output on or off.  Default OFF
.cd DIRECTORY          Change the working directory to DIRECTORY
.changes on|off        Show number of rows changed by SQL
.check GLOB            Fail if output since .testcase does not match
.clone NEWDB           Clone data into NEWDB from the existing database
.databases             List names and files of attached databases
.dbinfo ?DB?           Show status information about the database
.dump ?TABLE? ...      Dump the database in an SQL text format
                         If TABLE specified, only dump tables matching
                         LIKE pattern TABLE.
.echo on|off           Turn command echo on or off
.eqp on|off|full       Enable or disable automatic EXPLAIN QUERY PLAN
.excel                 Display the output of next command in a spreadsheet
.exit                  Exit this program
.expert                EXPERIMENTAL. Suggest indexes for specified queries
.fullschema ?--indent? Show schema and the content of sqlite_stat tables
.headers on|off        Turn display of headers on or off
.help                  Show this message
.import FILE TABLE     Import data from FILE into TABLE
.imposter INDEX TABLE  Create imposter table TABLE on index INDEX
.indexes ?TABLE?       Show names of all indexes
                         If TABLE specified, only show indexes for tables
                         matching LIKE pattern TABLE.
.limit ?LIMIT? ?VAL?   Display or change the value of an SQLITE_LIMIT
.lint OPTIONS          Report potential schema issues. Options:
                         fkey-indexes     Find missing foreign key indexes
.load FILE ?ENTRY?     Load an extension library
.log FILE|off          Turn logging on or off.  FILE can be stderr/stdout
.mode MODE ?TABLE?     Set output mode where MODE is one of:
                         ascii    Columns/rows delimited by 0x1F and 0x1E
                         csv      Comma-separated values
                         column   Left-aligned columns.  (See .width)
                         html     HTML <table> code
                         insert   SQL insert statements for TABLE
                         line     One value per line
                         list     Values delimited by "|"
                         quote    Escape answers as for SQL
                         tabs     Tab-separated values
                         tcl      TCL list elements
.nullvalue STRING      Use STRING in place of NULL values
.once (-e|-x|FILE)     Output for the next SQL command only to FILE
                         or invoke system text editor (-e) or spreadsheet (-x)
                         on the output.
.open ?OPTIONS? ?FILE? Close existing database and reopen FILE
                         The --new option starts with an empty file
.output ?FILE?         Send output to FILE or stdout
.print STRING...       Print literal STRING
.prompt MAIN CONTINUE  Replace the standard prompts
.quit                  Exit this program
.read FILENAME         Execute SQL in FILENAME
.restore ?DB? FILE     Restore content of DB (default "main") from FILE
.save FILE             Write in-memory database into FILE
.scanstats on|off      Turn sqlite3_stmt_scanstatus() metrics on or off
.schema ?PATTERN?      Show the CREATE statements matching PATTERN
                          Add --indent for pretty-printing
.selftest ?--init?     Run tests defined in the SELFTEST table
.separator COL ?ROW?   Change the column separator and optionally the row
                         separator for both the output mode and .import
.session CMD ...       Create or control sessions
.sha3sum ?OPTIONS...?  Compute a SHA3 hash of database content
.shell CMD ARGS...     Run CMD ARGS... in a system shell
.show                  Show the current values for various settings
.stats ?on|off?        Show stats or turn stats on or off
.system CMD ARGS...    Run CMD ARGS... in a system shell
.tables ?TABLE?        List names of tables
                         If TABLE specified, only list tables matching
                         LIKE pattern TABLE.
.testcase NAME         Begin redirecting output to 'testcase-out.txt'
.timeout MS            Try opening locked tables for MS milliseconds
.timer on|off          Turn SQL timer on or off
.trace FILE|off        Output each SQL statement as it is run
.vfsinfo ?AUX?         Information about the top-level VFS
.vfslist               List all available VFSes
.vfsname ?AUX?         Print the name of the VFS stack
.width NUM1 NUM2 ...   Set column widths for "column" mode
                         Negative values right-justify
 

 2)sql语句, 都以‘;’结尾

        1-- 创建一张表
            create table stuinfo(id integer, name text, age integer, score float);
        
        2-- 插入一条记录
            insert into stuinfo values(1001, 'zhangsan', 18, 80);
            insert into stuinfo (id, name, score) values(1002, 'lisi', 90);

        3-- 查看数据库记录
            select * from stuinfo;
            select * from stuinfo where score = 80;
            select * from stuinfo where score = 80 and name= 'zhangsan';
            select * from stuinfo where score = 80 or name='wangwu';
            select name,score from stuinfo;  查询指定的字段
            select * from stuinfo where score >= 85 and score < 90;

        4-- 删除一条记录
            delete from stuinfo where id=1003 and name='zhangsan';

插入的时候把wangyu插到得分里了,不对怎么删王五了重来

 

 

SQLite不会检查数据类型

        5-- 更新一条记录
            update stuinfo set age=20 where id=1003;
            update stuinfo set age=30, score = 82 where id=1003;

        6-- 删除一张表
            drop table stuinfo;

        7-- 增加一列
            alter table stuinfo add column sex char;

 

        8-- 删除一列(不支持直接删除,创建新表删除旧表将新表名改成旧表)
            create table stu as select id, name, score from stuinfo;
            drop table stuinfo;
            alter table stu rename to stuinfo;

     9、数据库设置主键和自增:
     create table info(id integer primary key autoincrement, name vchar);

三、sqlite3 数据库 C语言 API 

 int sqlite3_open(
      const char *filename,   /* Database filename (UTF-8) */
      sqlite3 **ppDb          /* OUT: SQLite db handle */
     );
    功能:打开数据库
    参数:filename  数据库名称
          ppdb      数据库句柄
    返回值:成功为0 SQLITE_OK ,出错 错误码

    int sqlite3_close(sqlite3* db);
    功能:关闭数据库
    参数
    返回值:成功为0 SQLITE_OK ,出错 错误码

   const char *sqlite3_errmsg(sqlite3*db);
    功能:得到错误信息的描述
 
   int sqlite3_exec(
   sqlite3* db,                                  /* An open database */
  const char *sql,                           /* SQL to be evaluated */
  int (*callback)(void* arg,int,char**,char**),  /* Callback function */
  void * arg,                                    /* 1st argument to callback */
  char **errmsg                              /* Error msg written here */
  );
  功能:执行一条sql语句
  参数

        db  数据库句柄
        sql sql语句
        callback  回调函数,只有在查询时,才传参
        arg      为回调函数传递参数
        errmsg  错误消息
  返回值:成功 SQLITE_OK

查询回调函数:
int (*callback)(void* arg,int ncolumns ,char** f_value,char** f_name),  /* Callback function */
功能:查询语句执行之后,会回调此函数
参数:arg   接收sqlite3_exec 传递来的参数
      ncolumns 列数
      f_value 列的值得地址
      f_name   列的名称
返回值:0,
           
  
int sqlite3_get_table(
  sqlite3 *db,          /* An open database */
  const char *zSql,     /* SQL to be evaluated */
  char ***pazResult,    /* Results of the query */
  int *pnRow,           /* Number of result rows written here */
  int *pnColumn,        /* Number of result columns written here */
  char **pzErrmsg       /* Error msg written here */
);
void sqlite3_free_table(char **result);

查询

四、基于数据库的学生管理系统

编译一下结果不认识sqlite3库

GCC编译错误:/ usr / bin / ld:找不到-lsqlite3.lib(GCC Compile Error: /usr/bin/ld: cannot find -lsqlite3.lib)_电脑培训

看了一下外国友人的解释我应该安一个这个

sudo apt-get install libsqlite3-dev

 

又出问题了主机和虚拟机间复制黏贴失效了

ubantu 与 主机 之间无法复制粘贴_51CTO博客_ubuntu与windows之间不能复制粘贴

sudo apt-get install open-vm-tools-desktop

好了,把代码粘过来

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sqlite3.h>
  5. #define DATABASE "student.db"
  6. #define N 128
  7. int do_insert(sqlite3 *db)
  8. {
  9. int id;
  10. char name[32] = {};
  11. char sex;
  12. int score;
  13. char sql[N] = {};
  14. char *errmsg;
  15. printf("Input id:");
  16. scanf("%d", &id);
  17. printf("Input name:");
  18. scanf("%s", name);
  19. getchar();
  20. printf("Input sex:");
  21. scanf("%c", &sex);
  22. printf("Input score:");
  23. scanf("%d", &score);
  24. sprintf(sql, "insert into stu values(%d, '%s', '%c', %d)", id, name, sex, score);
  25. if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
  26. {
  27. printf("%s\n", errmsg);
  28. }
  29. else
  30. {
  31. printf("Insert done.\n");
  32. }
  33. return 0;
  34. }
  35. int do_delete(sqlite3 *db)
  36. {
  37. int id;
  38. char sql[N] = {};
  39. char *errmsg;
  40. printf("Input id:");
  41. scanf("%d", &id);
  42. sprintf(sql, "delete from stu where id = %d", id);
  43. if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
  44. {
  45. printf("%s\n", errmsg);
  46. }
  47. else
  48. {
  49. printf("Delete done.\n");
  50. }
  51. return 0;
  52. }
  53. int do_update(sqlite3 *db)
  54. {
  55. int id;
  56. char sql[N] = {};
  57. char name[32] = "zhangsan";
  58. char *errmsg;
  59. printf("Input id:");
  60. scanf("%d", &id);
  61. sprintf(sql, "update stu set name='%s' where id=%d", name,id);
  62. if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
  63. {
  64. printf("%s\n", errmsg);
  65. }
  66. else
  67. {
  68. printf("update done.\n");
  69. }
  70. return 0;
  71. }
  72. int callback(void *arg, int f_num, char ** f_value, char ** f_name)
  73. {
  74. int i = 0;
  75. for(i = 0; i < f_num; i++)
  76. {
  77. // printf("%-8s %s", f_value[i], f_name[i]);
  78. printf("%-8s", f_value[i]);
  79. }
  80. printf("++++++++++++++++++++++");
  81. putchar(10);
  82. return 0;
  83. }
  84. int do_query(sqlite3 *db)
  85. {
  86. char *errmsg;
  87. char sql[N] = "select count(*) from stu where name='zhangsan';";
  88. if(sqlite3_exec(db, sql, callback,NULL , &errmsg) != SQLITE_OK)
  89. {
  90. printf("%s", errmsg);
  91. }
  92. else
  93. {
  94. printf("select done.\n");
  95. }
  96. }
  97. int do_query1(sqlite3 *db)
  98. {
  99. char *errmsg;
  100. char ** resultp;
  101. int nrow;
  102. int ncolumn;
  103. if(sqlite3_get_table(db, "select * from stu", &resultp, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
  104. {
  105. printf("%s\n", errmsg);
  106. return -1;
  107. }
  108. else
  109. {
  110. printf("query done.\n");
  111. }
  112. int i = 0;
  113. int j = 0;
  114. int index = ncolumn;
  115. for(j = 0; j < ncolumn; j++)
  116. {
  117. printf("%-10s ", resultp[j]);
  118. }
  119. putchar(10);
  120. for(i = 0; i < nrow; i++)
  121. {
  122. for(j = 0; j < ncolumn; j++)
  123. {
  124. printf("%-10s ", resultp[index++]);
  125. }
  126. putchar(10);
  127. }
  128. return 0;
  129. }
  130. int main(int argc, const char *argv[])
  131. {
  132. sqlite3 *db;
  133. char *errmsg;//errmsg是一个二级指针,定义一个一级指针取地址。
  134. int n;
  135. if(sqlite3_open(DATABASE, &db) != SQLITE_OK)
  136. {
  137. printf("%s\n", sqlite3_errmsg(db));
  138. return -1;
  139. }
  140. else
  141. {
  142. printf("open DATABASE success.\n");
  143. }
  144. if(sqlite3_exec(db, "create table if not exists stu(id int, name char , sex char , score int);",NULL, NULL, &errmsg) != SQLITE_OK)//分号可加可不加
  145. {
  146. printf("%s\n", errmsg);
  147. }
  148. else
  149. {
  150. printf("Create or open table success.\n");
  151. }
  152. while(1)
  153. {
  154. printf("********************************************\n");
  155. printf("1: insert 2:query 3:delete 4:update 5:quit\n");
  156. printf("********************************************\n");
  157. printf("Please select:");
  158. scanf("%d", &n);
  159. switch(n)
  160. {
  161. case 1:
  162. do_insert(db);
  163. break;
  164. case 2:
  165. do_query(db);
  166. // do_query1(db);
  167. break;
  168. case 3:
  169. do_delete(db);
  170. break;
  171. case 4:
  172. do_update(db);
  173. break;
  174. case 5:
  175. printf("main exit.\n");
  176. sqlite3_close(db);
  177. exit(0);
  178. break;
  179. default :
  180. printf("Invalid data n.\n");
  181. }
  182. }
  183. return 0;
  184. }

五、一个小作业

写了好久,代码不是很完善但是基本功能都实现了,并预留了接口等什么时候不忙了进行二次开发。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sqlite3.h>
  5. #include <time.h>
  6. #define DATABASE "fruit.db"
  7. #define N 128
  8. int do_insert(sqlite3 *db)
  9. {
  10. int id;
  11. char name[32] = {};
  12. float price;
  13. int number;
  14. char sql[N] = {};
  15. char *errmsg;
  16. printf("Input id:");
  17. scanf("%d", &id);
  18. puts("");
  19. printf("Input name:");
  20. scanf("%s", name);
  21. getchar();
  22. puts("");
  23. printf("Input price:");
  24. scanf("%f", &price);
  25. puts("");
  26. printf("Input number:");
  27. scanf("%d", &number);
  28. puts("");
  29. sprintf(sql, "insert into fruit values(%d, '%s', %f, %d)", id, name, price, number);
  30. if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
  31. {
  32. printf("%s\n", errmsg);
  33. }
  34. else
  35. {
  36. printf("stock done.\n");
  37. }
  38. return 0;
  39. }
  40. int do_delete(sqlite3 *db)
  41. {
  42. int id;
  43. char sql[N] = {};
  44. char *errmsg;
  45. char a;
  46. char name[32] = {};
  47. printf("Are you sure you don`t want this fruit!(Y/N)\n");
  48. scanf("%c",&a);
  49. if (a == 'Y' || a == 'y'){
  50. printf("Input fruit:");
  51. scanf("%s", name);
  52. getchar();
  53. puts("");
  54. sprintf(sql, "delete from fruit where name = '%s'", name);
  55. if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
  56. {
  57. printf("%s\n", errmsg);
  58. }
  59. else
  60. {
  61. printf("Delete done.\n");
  62. }
  63. }
  64. return 0;
  65. }
  66. int do_update(sqlite3 *db)
  67. {
  68. int id;
  69. char sql[N] = {};
  70. char name[32] = {};
  71. char *errmsg;
  72. float price;
  73. printf("Input fruit:");
  74. scanf("%s", name);
  75. getchar();
  76. puts("");
  77. printf("Input price");
  78. scanf("%f", &price);
  79. puts("");
  80. sprintf(sql, "update fruit set price=%f where name = '%s'", price,name);
  81. if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
  82. {
  83. printf("%s\n", errmsg);
  84. }
  85. else
  86. {
  87. printf("update done.\n");
  88. }
  89. return 0;
  90. }
  91. int callback(void *arg, int f_num, char ** f_value, char ** f_name)
  92. {
  93. int i = 0;
  94. for(i = 0; i < f_num; i++)
  95. {
  96. // printf("%-8s %s", f_value[i], f_name[i]);
  97. printf("%-8s", f_value[i]);
  98. }
  99. printf("\n++++++++++++++++++++++++++++++++");
  100. putchar(10);
  101. return 0;
  102. }
  103. int do_query(sqlite3 *db)
  104. {
  105. printf("++++++++++++++++++++++++++++++++\n");
  106. char *errmsg;
  107. //char sql[N] = "select count(*) from fruit where name='zhangsan';";
  108. char sql[N] = "select * from fruit;";
  109. if(sqlite3_exec(db, sql, callback,NULL , &errmsg) != SQLITE_OK)
  110. {
  111. printf("%s", errmsg);
  112. }
  113. else
  114. {
  115. printf("select done.\n");
  116. }
  117. }
  118. int do_query1(sqlite3 *db)
  119. {
  120. char *errmsg;
  121. char ** resultp;
  122. int nrow;
  123. int ncolumn;
  124. if(sqlite3_get_table(db, "select * from fruit", &resultp, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
  125. {
  126. printf("%s\n", errmsg);
  127. return -1;
  128. }
  129. else
  130. {
  131. printf("query done.\n");
  132. }
  133. int i = 0;
  134. int j = 0;
  135. int index = ncolumn;
  136. for(j = 0; j < ncolumn; j++)
  137. {
  138. printf("%-10s ", resultp[j]);
  139. }
  140. putchar(10);
  141. for(i = 0; i < nrow; i++)
  142. {
  143. for(j = 0; j < ncolumn; j++)
  144. {
  145. printf("%-10s ", resultp[index++]);
  146. }
  147. putchar(10);
  148. }
  149. return 0;
  150. }
  151. #if 0 //FIXME//cuowu
  152. static int callback1(void *data,int argc,char **argv,char **azColName)
  153. {
  154. int i;
  155. fprintf(stderr,"%s:",(const char *)data);
  156. for(i=0;i<argc;i++)
  157. {
  158. printf("%s = %s\n",azColName[i],argv[i] ? argv[i]:"NULL");
  159. }
  160. printf("\n");
  161. return 0;
  162. }
  163. //查询主函数
  164. int select_db(sqlite3 *db,char *tbname)
  165. {
  166. Stu s1 = {};
  167. char sql[100] = {'\0'};
  168. sprintf(sql,"select * from %s",tbname);
  169. printf("%s\n",sql);
  170. char *errmsg = NULL;
  171. if(sqlite3_exec(db,sql,callback,NULL,&errmsg)!=SQLITE_OK)
  172. {
  173. printf("%s\n",errmsg);
  174. return -1;
  175. }
  176. printf("select ok\n");
  177. return 0;
  178. }
  179. #endif
  180. int do_sell(sqlite3 *db)
  181. {
  182. int id;
  183. char sql[N] = {};
  184. char sql2[N] = {};
  185. char name[32] = {};
  186. char *errmsg;
  187. float price;
  188. int number;
  189. time_t times;
  190. struct tm* ge;
  191. times = time(NULL);
  192. char buf[32];
  193. sprintf(buf,"%4d-%2d-%2d %2d:%2d:%2d",ge->tm_year+1900,ge->tm_mon+1,ge->tm_mday,ge->tm_hour,ge->tm_min,ge->tm_sec);
  194. printf("Input fruit:");
  195. scanf("%s", name);
  196. getchar();
  197. puts("");
  198. printf("Input number");
  199. scanf("%d", &number);
  200. puts("");
  201. sprintf(sql2, "insert into sell values(%d, '%s', %f, %d, '%s')", id, name, price, number, buf);
  202. if(sqlite3_exec(db, sql2, NULL, NULL, &errmsg) != SQLITE_OK)
  203. {
  204. printf("%s\n", errmsg);
  205. }
  206. else
  207. {
  208. printf("stock done.\n");
  209. }
  210. #if 1
  211. char *errmsg1;
  212. char ** resultp;
  213. int nrow;
  214. int ncolumn;
  215. char sql1[N] = {};
  216. sprintf(sql,"select number from fruit where name = '%s';",name);
  217. if(sqlite3_get_table(db, sql1, &resultp, &nrow, &ncolumn, &errmsg1) != SQLITE_OK)
  218. {
  219. printf("%s\n", errmsg1);
  220. return -1;
  221. }
  222. else
  223. {
  224. number = (int)resultp - number;
  225. printf("query done.\n");
  226. }
  227. #endif
  228. sprintf(sql, "update fruit set number='%d' where name='%s'", number, name);
  229. if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
  230. {
  231. printf("%s\n", errmsg);
  232. }
  233. else
  234. {
  235. printf("sell done.\n");
  236. }
  237. return 0;
  238. }
  239. void help()
  240. {
  241. printf("1:stock,Input id,name,price,number\n");
  242. printf("2:Inquire the price and allowance of all fruits\n");
  243. printf("3:Delete fruit that is sold out and never comes in again\n");
  244. //XXX
  245. }
  246. int do_stock_list(db)
  247. {
  248. //TODO
  249. return 0;
  250. }
  251. int do_sell_list(db)
  252. {
  253. //TODO
  254. return 0;
  255. }
  256. int main(int argc, const char *argv[])
  257. {
  258. sqlite3 *db;
  259. char *errmsg;//errmsg是一个二级指针,定义一个一级指针取地址。
  260. int n;
  261. if(sqlite3_open(DATABASE, &db) != SQLITE_OK)//打开数据库
  262. {
  263. printf("%s\n", sqlite3_errmsg(db));
  264. return -1;
  265. }
  266. else
  267. {
  268. printf("open DATABASE success.\n");
  269. }
  270. if(sqlite3_exec(db, "create table if not exists fruit(id int, name char , price float , number int);",NULL, NULL, &errmsg) != SQLITE_OK)//分号可加可不加 商品价格数量表
  271. {
  272. printf("%s\n", errmsg);
  273. }
  274. else
  275. {
  276. printf("Create or open table success.\n");
  277. }
  278. if(sqlite3_exec(db, "create table if not exists sell(id int, name char , price float ,number int, time char);",NULL, NULL, &errmsg) != SQLITE_OK)
  279. {
  280. printf("%s\n", errmsg);
  281. }
  282. else
  283. {
  284. printf("Create or open table success.\n");
  285. }
  286. while(1)
  287. {
  288. printf("*******************************************************************************\n");
  289. printf("1:stock 2:price 3:delete 4:update 5:quit 6:sell 7:stock_list 8:sell_list 9:help\n");
  290. printf("*******************************************************************************\n");
  291. printf("Please select:");
  292. scanf("%d", &n);
  293. switch(n)
  294. {
  295. case 1:
  296. do_insert(db);
  297. break;
  298. case 2:
  299. do_query(db);
  300. // do_query1(db);
  301. break;
  302. case 3:
  303. do_delete(db);
  304. break;
  305. case 4:
  306. do_update(db);
  307. break;
  308. case 5:
  309. printf("main exit.\n");
  310. sqlite3_close(db);
  311. exit(0);
  312. break;
  313. case 6:
  314. do_sell(db);
  315. break;
  316. //XXX//mianqiangnenggongzuo
  317. case 7:
  318. do_stock_list(db);
  319. break;
  320. case 8:
  321. do_sell_list(db);
  322. break;
  323. case 9:
  324. help();
  325. break;
  326. default :
  327. printf("Input error\n");
  328. }
  329. }
  330. return 0;
  331. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/173221
推荐阅读
相关标签
  

闽ICP备14008679号