linux下sqlite3的应用


[cpp]  view plain  co
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 1.sqlite3.c 
  4. * Description : 1)数据插入,删除,清空   
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char *errmsg = NULL;  
  16.   
  17. int main()  
  18. {  
  19.     int ret;  
  20.     int insert;  
  21.       
  22.     ret = sqlite3_open("test.db", &db);  
  23.     if(ret)  
  24.     {  
  25.         printf("can not open database.\n");  
  26.     }  
  27.     else  
  28.     {  
  29.         printf("open database succsee.\n");  
  30.     }  
  31.     insert =  sqlite3_exec(db, "insert into test_table values('wujibing',111,222)", 0, 0, &errmsg);//插入  
  32.     //insert = sqlite3_exec(db, "delete from test_table where name='xu'", 0, 0, &errmsg);//删除  
  33.     //insert = sqlite3_exec(db, "delete from test_table", 0, 0, &errmsg);//清空  
  34.     printf("exec_ret: %d\n", insert);  
  35.     printf("errMsg: %d\n", errmsg);  
  36.     sqlite3_close(db);  
  37.   
  38.     return 0;  
  39. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 2.sqlite3_display.c 
  4. * Description : 1) 显示数据  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 16时30分17秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char **Result = NULL;  
  16. static char *errmsg = NULL;  
  17.   
  18. int main()  
  19. {  
  20.     int ret;  
  21.     int i;  
  22.     int j;  
  23.     int nrow;  
  24.     int ncolumn;  
  25.   
  26.     ret = sqlite3_open("test.db", &db);  
  27. //    sqlite3_exec(db, ".head on", 0, 0, &errmsg);  
  28.     ret = sqlite3_get_table(db, "select * from test_table", &Result, &nrow, &ncolumn, &errmsg);  
  29.     if(ret)  
  30.     {  
  31.         printf("can not open database.\n");  
  32.     }  
  33.     else  
  34.     {  
  35.         printf("open database success.\n");  
  36.         for(i = 0; i <= nrow; i++)  
  37.         {  
  38.             for(j = 0; j < ncolumn; j++)  
  39.             {  
  40.                 printf("%s|", Result[i * ncolumn + j]);  
  41.             }  
  42.             printf("\n");  
  43.         }  
  44.     }  
  45.     sqlite3_free_table(Result);  
  46.     sqlite3_close(db);  
  47.   
  48.     return 0;  
  49. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 3.sqlite3_callback.c 
  4. * Description : 1) 回调函数完成数据库功能  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 17时57分53秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13. #include <stdlib.h>  
  14.   
  15. static sqlite3 *db;  
  16.   
  17. void create_table(sqlite3 *db)  
  18. {  
  19.     char *sql = NULL;  
  20.     char *errmsg = NULL;  
  21.     int ret;  
  22.   
  23.     sql = "create table if not exists mytable (id integer primary key, name text)";  
  24.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  25.     if(ret != SQLITE_OK)  
  26.     {  
  27.          printf("create table error: %s\n", errmsg);  
  28.     }  
  29.   
  30. }  
  31.   
  32. void insert_record(sqlite3 *db)  
  33. {  
  34.     char sql[100];  
  35.     char *errmsg = NULL;  
  36. //    char name[20];  
  37.     int name;  
  38.       
  39.     int ret;  
  40.     int id;  
  41.       
  42.     printf("Please input phone1 and phone2:\n");  
  43.     printf("phone1: ");  
  44.     scanf("%d", &id);  
  45.     getchar();  
  46.     printf("name: ");  
  47.     scanf("%d", &name);  
  48.     getchar();  
  49.   
  50.     sprintf(sql, "insert into test_table values(%d, %d)", id, name);  
  51.   
  52.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  53.     if(ret != SQLITE_OK)  
  54.     {  
  55.         printf("insert table error: %s\n", errmsg);  
  56.     }  
  57. }  
  58.   
  59. int displaycb(void *para, int ncolumn, char ** columnvalue, char *columnname[])  
  60. {  
  61.     int i;  
  62.   
  63.     printf("total column is %d\n", ncolumn);  
  64.   
  65.     for(i = 0; i < ncolumn; i++)  
  66.     {  
  67.         printf("col_name: %s-----> col_value: %s\n", columnname[i], columnvalue[i]);  
  68.         printf("============================\n");  
  69.   
  70.     }  
  71.     return 0;  
  72. }  
  73.   
  74. void inquire_usecb(sqlite3 *db)  
  75. {  
  76.     char *sql = NULL;  
  77.     char *errmsg = NULL;  
  78.     int ret;  
  79.   
  80.     sql = "select * from mytable";  
  81.     ret = sqlite3_exec(db, sql, displaycb, NULL, &errmsg);  
  82.     if(ret != SQLITE_OK)  
  83.     {  
  84.         printf("select is error: %s\n", errmsg);  
  85.     }  
  86. }  
  87.   
  88. int main()  
  89. {  
  90.     int ret;  
  91.   
  92.     ret = sqlite3_open("test.db", &db);  
  93.   
  94.     sqlite3_exec(db,"select * from test_table",displaycb, NULL, NULL);  
  95.     insert_record(db);  
  96.     sqlite3_close(db);  
  97.   
  98.     return 0;  
  99. }  

http://blog.csdn.net/qq_30594349/article/details/53015708?locationNum=7&fps=1


下面是Linux下的sqlite3的c语言应用实例:

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 1.sqlite3.c 
  4. * Description : 1)数据插入,删除,清空   
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char *errmsg = NULL;  
  16.   
  17. int main()  
  18. {  
  19.     int ret;  
  20.     int insert;  
  21.       
  22.     ret = sqlite3_open("test.db", &db);  
  23.     if(ret)  
  24.     {  
  25.         printf("can not open database.\n");  
  26.     }  
  27.     else  
  28.     {  
  29.         printf("open database succsee.\n");  
  30.     }  
  31.     insert =  sqlite3_exec(db, "insert into test_table values('wujibing',111,222)", 0, 0, &errmsg);//插入  
  32.     //insert = sqlite3_exec(db, "delete from test_table where name='xu'", 0, 0, &errmsg);//删除  
  33.     //insert = sqlite3_exec(db, "delete from test_table", 0, 0, &errmsg);//清空  
  34.     printf("exec_ret: %d\n", insert);  
  35.     printf("errMsg: %d\n", errmsg);  
  36.     sqlite3_close(db);  
  37.   
  38.     return 0;  
  39. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 2.sqlite3_display.c 
  4. * Description : 1) 显示数据  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 16时30分17秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char **Result = NULL;  
  16. static char *errmsg = NULL;  
  17.   
  18. int main()  
  19. {  
  20.     int ret;  
  21.     int i;  
  22.     int j;  
  23.     int nrow;  
  24.     int ncolumn;  
  25.   
  26.     ret = sqlite3_open("test.db", &db);  
  27. //    sqlite3_exec(db, ".head on", 0, 0, &errmsg);  
  28.     ret = sqlite3_get_table(db, "select * from test_table", &Result, &nrow, &ncolumn, &errmsg);  
  29.     if(ret)  
  30.     {  
  31.         printf("can not open database.\n");  
  32.     }  
  33.     else  
  34.     {  
  35.         printf("open database success.\n");  
  36.         for(i = 0; i <= nrow; i++)  
  37.         {  
  38.             for(j = 0; j < ncolumn; j++)  
  39.             {  
  40.                 printf("%s|", Result[i * ncolumn + j]);  
  41.             }  
  42.             printf("\n");  
  43.         }  
  44.     }  
  45.     sqlite3_free_table(Result);  
  46.     sqlite3_close(db);  
  47.   
  48.     return 0;  
  49. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 3.sqlite3_callback.c 
  4. * Description : 1) 回调函数完成数据库功能  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 17时57分53秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13. #include <stdlib.h>  
  14.   
  15. static sqlite3 *db;  
  16.   
  17. void create_table(sqlite3 *db)  
  18. {  
  19.     char *sql = NULL;  
  20.     char *errmsg = NULL;  
  21.     int ret;  
  22.   
  23.     sql = "create table if not exists mytable (id integer primary key, name text)";  
  24.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  25.     if(ret != SQLITE_OK)  
  26.     {  
  27.          printf("create table error: %s\n", errmsg);  
  28.     }  
  29.   
  30. }  
  31.   
  32. void insert_record(sqlite3 *db)  
  33. {  
  34.     char sql[100];  
  35.     char *errmsg = NULL;  
  36. //    char name[20];  
  37.     int name;  
  38.       
  39.     int ret;  
  40.     int id;  
  41.       
  42.     printf("Please input phone1 and phone2:\n");  
  43.     printf("phone1: ");  
  44.     scanf("%d", &id);  
  45.     getchar();  
  46.     printf("name: ");  
  47.     scanf("%d", &name);  
  48.     getchar();  
  49.   
  50.     sprintf(sql, "insert into test_table values(%d, %d)", id, name);  
  51.   
  52.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  53.     if(ret != SQLITE_OK)  
  54.     {  
  55.         printf("insert table error: %s\n", errmsg);  
  56.     }  
  57. }  
  58.   
  59. int displaycb(void *para, int ncolumn, char ** columnvalue, char *columnname[])  
  60. {  
  61.     int i;  
  62.   
  63.     printf("total column is %d\n", ncolumn);  
  64.   
  65.     for(i = 0; i < ncolumn; i++)  
  66.     {  
  67.         printf("col_name: %s-----> col_value: %s\n", columnname[i], columnvalue[i]);  
  68.         printf("============================\n");  
  69.   
  70.     }  
  71.     return 0;  
  72. }  
  73.   
  74. void inquire_usecb(sqlite3 *db)  
  75. {  
  76.     char *sql = NULL;  
  77.     char *errmsg = NULL;  
  78.     int ret;  
  79.   
  80.     sql = "select * from mytable";  
  81.     ret = sqlite3_exec(db, sql, displaycb, NULL, &errmsg);  
  82.     if(ret != SQLITE_OK)  
  83.     {  
  84.         printf("select is error: %s\n", errmsg);  
  85.     }  
  86. }  
  87.   
  88. int main()  
  89. {  
  90.     int ret;  
  91.   
  92.     ret = sqlite3_open("test.db", &db);  
  93.   
  94.     sqlite3_exec(db,"select * from test_table",displaycb, NULL, NULL);  
  95.     insert_record(db);  
  96.     sqlite3_close(db);  
  97.   
  98.     return 0;  
  99. }  

http://blog.csdn.net/qq_30594349/article/details/53015708?locationNum=7&fps=1




[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 1.sqlite3.c 
  4. * Description : 1)数据插入,删除,清空   
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char *errmsg = NULL;  
  16.   
  17. int main()  
  18. {  
  19.     int ret;  
  20.     int insert;  
  21.       
  22.     ret = sqlite3_open("test.db", &db);  
  23.     if(ret)  
  24.     {  
  25.         printf("can not open database.\n");  
  26.     }  
  27.     else  
  28.     {  
  29.         printf("open database succsee.\n");  
  30.     }  
  31.     insert =  sqlite3_exec(db, "insert into test_table values('wujibing',111,222)", 0, 0, &errmsg);//插入  
  32.     //insert = sqlite3_exec(db, "delete from test_table where name='xu'", 0, 0, &errmsg);//删除  
  33.     //insert = sqlite3_exec(db, "delete from test_table", 0, 0, &errmsg);//清空  
  34.     printf("exec_ret: %d\n", insert);  
  35.     printf("errMsg: %d\n", errmsg);  
  36.     sqlite3_close(db);  
  37.   
  38.     return 0;  
  39. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 2.sqlite3_display.c 
  4. * Description : 1) 显示数据  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 16时30分17秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char **Result = NULL;  
  16. static char *errmsg = NULL;  
  17.   
  18. int main()  
  19. {  
  20.     int ret;  
  21.     int i;  
  22.     int j;  
  23.     int nrow;  
  24.     int ncolumn;  
  25.   
  26.     ret = sqlite3_open("test.db", &db);  
  27. //    sqlite3_exec(db, ".head on", 0, 0, &errmsg);  
  28.     ret = sqlite3_get_table(db, "select * from test_table", &Result, &nrow, &ncolumn, &errmsg);  
  29.     if(ret)  
  30.     {  
  31.         printf("can not open database.\n");  
  32.     }  
  33.     else  
  34.     {  
  35.         printf("open database success.\n");  
  36.         for(i = 0; i <= nrow; i++)  
  37.         {  
  38.             for(j = 0; j < ncolumn; j++)  
  39.             {  
  40.                 printf("%s|", Result[i * ncolumn + j]);  
  41.             }  
  42.             printf("\n");  
  43.         }  
  44.     }  
  45.     sqlite3_free_table(Result);  
  46.     sqlite3_close(db);  
  47.   
  48.     return 0;  
  49. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 3.sqlite3_callback.c 
  4. * Description : 1) 回调函数完成数据库功能  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 17时57分53秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13. #include <stdlib.h>  
  14.   
  15. static sqlite3 *db;  
  16.   
  17. void create_table(sqlite3 *db)  
  18. {  
  19.     char *sql = NULL;  
  20.     char *errmsg = NULL;  
  21.     int ret;  
  22.   
  23.     sql = "create table if not exists mytable (id integer primary key, name text)";  
  24.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  25.     if(ret != SQLITE_OK)  
  26.     {  
  27.          printf("create table error: %s\n", errmsg);  
  28.     }  
  29.   
  30. }  
  31.   
  32. void insert_record(sqlite3 *db)  
  33. {  
  34.     char sql[100];  
  35.     char *errmsg = NULL;  
  36. //    char name[20];  
  37.     int name;  
  38.       
  39.     int ret;  
  40.     int id;  
  41.       
  42.     printf("Please input phone1 and phone2:\n");  
  43.     printf("phone1: ");  
  44.     scanf("%d", &id);  
  45.     getchar();  
  46.     printf("name: ");  
  47.     scanf("%d", &name);  
  48.     getchar();  
  49.   
  50.     sprintf(sql, "insert into test_table values(%d, %d)", id, name);  
  51.   
  52.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  53.     if(ret != SQLITE_OK)  
  54.     {  
  55.         printf("insert table error: %s\n", errmsg);  
  56.     }  
  57. }  
  58.   
  59. int displaycb(void *para, int ncolumn, char ** columnvalue, char *columnname[])  
  60. {  
  61.     int i;  
  62.   
  63.     printf("total column is %d\n", ncolumn);  
  64.   
  65.     for(i = 0; i < ncolumn; i++)  
  66.     {  
  67.         printf("col_name: %s-----> col_value: %s\n", columnname[i], columnvalue[i]);  
  68.         printf("============================\n");  
  69.   
  70.     }  
  71.     return 0;  
  72. }  
  73.   
  74. void inquire_usecb(sqlite3 *db)  
  75. {  
  76.     char *sql = NULL;  
  77.     char *errmsg = NULL;  
  78.     int ret;  
  79.   
  80.     sql = "select * from mytable";  
  81.     ret = sqlite3_exec(db, sql, displaycb, NULL, &errmsg);  
  82.     if(ret != SQLITE_OK)  
  83.     {  
  84.         printf("select is error: %s\n", errmsg);  
  85.     }  
  86. }  
  87.   
  88. int main()  
  89. {  
  90.     int ret;  
  91.   
  92.     ret = sqlite3_open("test.db", &db);  
  93.   
  94.     sqlite3_exec(db,"select * from test_table",displaycb, NULL, NULL);  
  95.     insert_record(db);  
  96.     sqlite3_close(db);  
  97.   
  98.     return 0;  
  99. }  

http://blog.csdn.net/qq_30594349/article/details/53015708?locationNum=7&fps=1


[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 1.sqlite3.c 
  4. * Description : 1)数据插入,删除,清空   
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char *errmsg = NULL;  
  16.   
  17. int main()  
  18. {  
  19.     int ret;  
  20.     int insert;  
  21.       
  22.     ret = sqlite3_open("test.db", &db);  
  23.     if(ret)  
  24.     {  
  25.         printf("can not open database.\n");  
  26.     }  
  27.     else  
  28.     {  
  29.         printf("open database succsee.\n");  
  30.     }  
  31.     insert =  sqlite3_exec(db, "insert into test_table values('wujibing',111,222)", 0, 0, &errmsg);//插入  
  32.     //insert = sqlite3_exec(db, "delete from test_table where name='xu'", 0, 0, &errmsg);//删除  
  33.     //insert = sqlite3_exec(db, "delete from test_table", 0, 0, &errmsg);//清空  
  34.     printf("exec_ret: %d\n", insert);  
  35.     printf("errMsg: %d\n", errmsg);  
  36.     sqlite3_close(db);  
  37.   
  38.     return 0;  
  39. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 2.sqlite3_display.c 
  4. * Description : 1) 显示数据  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 16时30分17秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char **Result = NULL;  
  16. static char *errmsg = NULL;  
  17.   
  18. int main()  
  19. {  
  20.     int ret;  
  21.     int i;  
  22.     int j;  
  23.     int nrow;  
  24.     int ncolumn;  
  25.   
  26.     ret = sqlite3_open("test.db", &db);  
  27. //    sqlite3_exec(db, ".head on", 0, 0, &errmsg);  
  28.     ret = sqlite3_get_table(db, "select * from test_table", &Result, &nrow, &ncolumn, &errmsg);  
  29.     if(ret)  
  30.     {  
  31.         printf("can not open database.\n");  
  32.     }  
  33.     else  
  34.     {  
  35.         printf("open database success.\n");  
  36.         for(i = 0; i <= nrow; i++)  
  37.         {  
  38.             for(j = 0; j < ncolumn; j++)  
  39.             {  
  40.                 printf("%s|", Result[i * ncolumn + j]);  
  41.             }  
  42.             printf("\n");  
  43.         }  
  44.     }  
  45.     sqlite3_free_table(Result);  
  46.     sqlite3_close(db);  
  47.   
  48.     return 0;  
  49. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 3.sqlite3_callback.c 
  4. * Description : 1) 回调函数完成数据库功能  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 17时57分53秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13. #include <stdlib.h>  
  14.   
  15. static sqlite3 *db;  
  16.   
  17. void create_table(sqlite3 *db)  
  18. {  
  19.     char *sql = NULL;  
  20.     char *errmsg = NULL;  
  21.     int ret;  
  22.   
  23.     sql = "create table if not exists mytable (id integer primary key, name text)";  
  24.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  25.     if(ret != SQLITE_OK)  
  26.     {  
  27.          printf("create table error: %s\n", errmsg);  
  28.     }  
  29.   
  30. }  
  31.   
  32. void insert_record(sqlite3 *db)  
  33. {  
  34.     char sql[100];  
  35.     char *errmsg = NULL;  
  36. //    char name[20];  
  37.     int name;  
  38.       
  39.     int ret;  
  40.     int id;  
  41.       
  42.     printf("Please input phone1 and phone2:\n");  
  43.     printf("phone1: ");  
  44.     scanf("%d", &id);  
  45.     getchar();  
  46.     printf("name: ");  
  47.     scanf("%d", &name);  
  48.     getchar();  
  49.   
  50.     sprintf(sql, "insert into test_table values(%d, %d)", id, name);  
  51.   
  52.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  53.     if(ret != SQLITE_OK)  
  54.     {  
  55.         printf("insert table error: %s\n", errmsg);  
  56.     }  
  57. }  
  58.   
  59. int displaycb(void *para, int ncolumn, char ** columnvalue, char *columnname[])  
  60. {  
  61.     int i;  
  62.   
  63.     printf("total column is %d\n", ncolumn);  
  64.   
  65.     for(i = 0; i < ncolumn; i++)  
  66.     {  
  67.         printf("col_name: %s-----> col_value: %s\n", columnname[i], columnvalue[i]);  
  68.         printf("============================\n");  
  69.   
  70.     }  
  71.     return 0;  
  72. }  
  73.   
  74. void inquire_usecb(sqlite3 *db)  
  75. {  
  76.     char *sql = NULL;  
  77.     char *errmsg = NULL;  
  78.     int ret;  
  79.   
  80.     sql = "select * from mytable";  
  81.     ret = sqlite3_exec(db, sql, displaycb, NULL, &errmsg);  
  82.     if(ret != SQLITE_OK)  
  83.     {  
  84.         printf("select is error: %s\n", errmsg);  
  85.     }  
  86. }  
  87.   
  88. int main()  
  89. {  
  90.     int ret;  
  91.   
  92.     ret = sqlite3_open("test.db", &db);  
  93.   
  94.     sqlite3_exec(db,"select * from test_table",displaycb, NULL, NULL);  
  95.     insert_record(db);  
  96.     sqlite3_close(db);  
  97.   
  98.     return 0;  
  99. }  

http://blog.csdn.net/qq_30594349/article/details/53015708?locationNum=7&fps=1
[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 1.sqlite3.c 
  4. * Description : 1)数据插入,删除,清空   
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char *errmsg = NULL;  
  16.   
  17. int main()  
  18. {  
  19.     int ret;  
  20.     int insert;  
  21.       
  22.     ret = sqlite3_open("test.db", &db);  
  23.     if(ret)  
  24.     {  
  25.         printf("can not open database.\n");  
  26.     }  
  27.     else  
  28.     {  
  29.         printf("open database succsee.\n");  
  30.     }  
  31.     insert =  sqlite3_exec(db, "insert into test_table values('wujibing',111,222)", 0, 0, &errmsg);//插入  
  32.     //insert = sqlite3_exec(db, "delete from test_table where name='xu'", 0, 0, &errmsg);//删除  
  33.     //insert = sqlite3_exec(db, "delete from test_table", 0, 0, &errmsg);//清空  
  34.     printf("exec_ret: %d\n", insert);  
  35.     printf("errMsg: %d\n", errmsg);  
  36.     sqlite3_close(db);  
  37.   
  38.     return 0;  
  39. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 2.sqlite3_display.c 
  4. * Description : 1) 显示数据  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 16时30分17秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char **Result = NULL;  
  16. static char *errmsg = NULL;  
  17.   
  18. int main()  
  19. {  
  20.     int ret;  
  21.     int i;  
  22.     int j;  
  23.     int nrow;  
  24.     int ncolumn;  
  25.   
  26.     ret = sqlite3_open("test.db", &db);  
  27. //    sqlite3_exec(db, ".head on", 0, 0, &errmsg);  
  28.     ret = sqlite3_get_table(db, "select * from test_table", &Result, &nrow, &ncolumn, &errmsg);  
  29.     if(ret)  
  30.     {  
  31.         printf("can not open database.\n");  
  32.     }  
  33.     else  
  34.     {  
  35.         printf("open database success.\n");  
  36.         for(i = 0; i <= nrow; i++)  
  37.         {  
  38.             for(j = 0; j < ncolumn; j++)  
  39.             {  
  40.                 printf("%s|", Result[i * ncolumn + j]);  
  41.             }  
  42.             printf("\n");  
  43.         }  
  44.     }  
  45.     sqlite3_free_table(Result);  
  46.     sqlite3_close(db);  
  47.   
  48.     return 0;  
  49. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 3.sqlite3_callback.c 
  4. * Description : 1) 回调函数完成数据库功能  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 17时57分53秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13. #include <stdlib.h>  
  14.   
  15. static sqlite3 *db;  
  16.   
  17. void create_table(sqlite3 *db)  
  18. {  
  19.     char *sql = NULL;  
  20.     char *errmsg = NULL;  
  21.     int ret;  
  22.   
  23.     sql = "create table if not exists mytable (id integer primary key, name text)";  
  24.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  25.     if(ret != SQLITE_OK)  
  26.     {  
  27.          printf("create table error: %s\n", errmsg);  
  28.     }  
  29.   
  30. }  
  31.   
  32. void insert_record(sqlite3 *db)  
  33. {  
  34.     char sql[100];  
  35.     char *errmsg = NULL;  
  36. //    char name[20];  
  37.     int name;  
  38.       
  39.     int ret;  
  40.     int id;  
  41.       
  42.     printf("Please input phone1 and phone2:\n");  
  43.     printf("phone1: ");  
  44.     scanf("%d", &id);  
  45.     getchar();  
  46.     printf("name: ");  
  47.     scanf("%d", &name);  
  48.     getchar();  
  49.   
  50.     sprintf(sql, "insert into test_table values(%d, %d)", id, name);  
  51.   
  52.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  53.     if(ret != SQLITE_OK)  
  54.     {  
  55.         printf("insert table error: %s\n", errmsg);  
  56.     }  
  57. }  
  58.   
  59. int displaycb(void *para, int ncolumn, char ** columnvalue, char *columnname[])  
  60. {  
  61.     int i;  
  62.   
  63.     printf("total column is %d\n", ncolumn);  
  64.   
  65.     for(i = 0; i < ncolumn; i++)  
  66.     {  
  67.         printf("col_name: %s-----> col_value: %s\n", columnname[i], columnvalue[i]);  
  68.         printf("============================\n");  
  69.   
  70.     }  
  71.     return 0;  
  72. }  
  73.   
  74. void inquire_usecb(sqlite3 *db)  
  75. {  
  76.     char *sql = NULL;  
  77.     char *errmsg = NULL;  
  78.     int ret;  
  79.   
  80.     sql = "select * from mytable";  
  81.     ret = sqlite3_exec(db, sql, displaycb, NULL, &errmsg);  
  82.     if(ret != SQLITE_OK)  
  83.     {  
  84.         printf("select is error: %s\n", errmsg);  
  85.     }  
  86. }  
  87.   
  88. int main()  
  89. {  
  90.     int ret;  
  91.   
  92.     ret = sqlite3_open("test.db", &db);  
  93.   
  94.     sqlite3_exec(db,"select * from test_table",displaycb, NULL, NULL);  
  95.     insert_record(db);  
  96.     sqlite3_close(db);  
  97.   
  98.     return 0;  
  99. }  

http://blog.csdn.net/qq_30594349/article/details/53015708?locationNum=7&fps=1
[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 1.sqlite3.c 
  4. * Description : 1)数据插入,删除,清空   
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char *errmsg = NULL;  
  16.   
  17. int main()  
  18. {  
  19.     int ret;  
  20.     int insert;  
  21.       
  22.     ret = sqlite3_open("test.db", &db);  
  23.     if(ret)  
  24.     {  
  25.         printf("can not open database.\n");  
  26.     }  
  27.     else  
  28.     {  
  29.         printf("open database succsee.\n");  
  30.     }  
  31.     insert =  sqlite3_exec(db, "insert into test_table values('wujibing',111,222)", 0, 0, &errmsg);//插入  
  32.     //insert = sqlite3_exec(db, "delete from test_table where name='xu'", 0, 0, &errmsg);//删除  
  33.     //insert = sqlite3_exec(db, "delete from test_table", 0, 0, &errmsg);//清空  
  34.     printf("exec_ret: %d\n", insert);  
  35.     printf("errMsg: %d\n", errmsg);  
  36.     sqlite3_close(db);  
  37.   
  38.     return 0;  
  39. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 2.sqlite3_display.c 
  4. * Description : 1) 显示数据  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 16时30分17秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char **Result = NULL;  
  16. static char *errmsg = NULL;  
  17.   
  18. int main()  
  19. {  
  20.     int ret;  
  21.     int i;  
  22.     int j;  
  23.     int nrow;  
  24.     int ncolumn;  
  25.   
  26.     ret = sqlite3_open("test.db", &db);  
  27. //    sqlite3_exec(db, ".head on", 0, 0, &errmsg);  
  28.     ret = sqlite3_get_table(db, "select * from test_table", &Result, &nrow, &ncolumn, &errmsg);  
  29.     if(ret)  
  30.     {  
  31.         printf("can not open database.\n");  
  32.     }  
  33.     else  
  34.     {  
  35.         printf("open database success.\n");  
  36.         for(i = 0; i <= nrow; i++)  
  37.         {  
  38.             for(j = 0; j < ncolumn; j++)  
  39.             {  
  40.                 printf("%s|", Result[i * ncolumn + j]);  
  41.             }  
  42.             printf("\n");  
  43.         }  
  44.     }  
  45.     sqlite3_free_table(Result);  
  46.     sqlite3_close(db);  
  47.   
  48.     return 0;  
  49. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 3.sqlite3_callback.c 
  4. * Description : 1) 回调函数完成数据库功能  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 17时57分53秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13. #include <stdlib.h>  
  14.   
  15. static sqlite3 *db;  
  16.   
  17. void create_table(sqlite3 *db)  
  18. {  
  19.     char *sql = NULL;  
  20.     char *errmsg = NULL;  
  21.     int ret;  
  22.   
  23.     sql = "create table if not exists mytable (id integer primary key, name text)";  
  24.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  25.     if(ret != SQLITE_OK)  
  26.     {  
  27.          printf("create table error: %s\n", errmsg);  
  28.     }  
  29.   
  30. }  
  31.   
  32. void insert_record(sqlite3 *db)  
  33. {  
  34.     char sql[100];  
  35.     char *errmsg = NULL;  
  36. //    char name[20];  
  37.     int name;  
  38.       
  39.     int ret;  
  40.     int id;  
  41.       
  42.     printf("Please input phone1 and phone2:\n");  
  43.     printf("phone1: ");  
  44.     scanf("%d", &id);  
  45.     getchar();  
  46.     printf("name: ");  
  47.     scanf("%d", &name);  
  48.     getchar();  
  49.   
  50.     sprintf(sql, "insert into test_table values(%d, %d)", id, name);  
  51.   
  52.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  53.     if(ret != SQLITE_OK)  
  54.     {  
  55.         printf("insert table error: %s\n", errmsg);  
  56.     }  
  57. }  
  58.   
  59. int displaycb(void *para, int ncolumn, char ** columnvalue, char *columnname[])  
  60. {  
  61.     int i;  
  62.   
  63.     printf("total column is %d\n", ncolumn);  
  64.   
  65.     for(i = 0; i < ncolumn; i++)  
  66.     {  
  67.         printf("col_name: %s-----> col_value: %s\n", columnname[i], columnvalue[i]);  
  68.         printf("============================\n");  
  69.   
  70.     }  
  71.     return 0;  
  72. }  
  73.   
  74. void inquire_usecb(sqlite3 *db)  
  75. {  
  76.     char *sql = NULL;  
  77.     char *errmsg = NULL;  
  78.     int ret;  
  79.   
  80.     sql = "select * from mytable";  
  81.     ret = sqlite3_exec(db, sql, displaycb, NULL, &errmsg);  
  82.     if(ret != SQLITE_OK)  
  83.     {  
  84.         printf("select is error: %s\n", errmsg);  
  85.     }  
  86. }  
  87.   
  88. int main()  
  89. {  
  90.     int ret;  
  91.   
  92.     ret = sqlite3_open("test.db", &db);  
  93.   
  94.     sqlite3_exec(db,"select * from test_table",displaycb, NULL, NULL);  
  95.     insert_record(db);  
  96.     sqlite3_close(db);  
  97.   
  98.     return 0;  
  99. }  

http://blog.csdn.net/qq_30594349/article/details/53015708?locationNum=7&fps=1
[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 1.sqlite3.c 
  4. * Description : 1)数据插入,删除,清空   
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char *errmsg = NULL;  
  16.   
  17. int main()  
  18. {  
  19.     int ret;  
  20.     int insert;  
  21.       
  22.     ret = sqlite3_open("test.db", &db);  
  23.     if(ret)  
  24.     {  
  25.         printf("can not open database.\n");  
  26.     }  
  27.     else  
  28.     {  
  29.         printf("open database succsee.\n");  
  30.     }  
  31.     insert =  sqlite3_exec(db, "insert into test_table values('wujibing',111,222)", 0, 0, &errmsg);//插入  
  32.     //insert = sqlite3_exec(db, "delete from test_table where name='xu'", 0, 0, &errmsg);//删除  
  33.     //insert = sqlite3_exec(db, "delete from test_table", 0, 0, &errmsg);//清空  
  34.     printf("exec_ret: %d\n", insert);  
  35.     printf("errMsg: %d\n", errmsg);  
  36.     sqlite3_close(db);  
  37.   
  38.     return 0;  
  39. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 2.sqlite3_display.c 
  4. * Description : 1) 显示数据  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 16时30分17秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char **Result = NULL;  
  16. static char *errmsg = NULL;  
  17.   
  18. int main()  
  19. {  
  20.     int ret;  
  21.     int i;  
  22.     int j;  
  23.     int nrow;  
  24.     int ncolumn;  
  25.   
  26.     ret = sqlite3_open("test.db", &db);  
  27. //    sqlite3_exec(db, ".head on", 0, 0, &errmsg);  
  28.     ret = sqlite3_get_table(db, "select * from test_table", &Result, &nrow, &ncolumn, &errmsg);  
  29.     if(ret)  
  30.     {  
  31.         printf("can not open database.\n");  
  32.     }  
  33.     else  
  34.     {  
  35.         printf("open database success.\n");  
  36.         for(i = 0; i <= nrow; i++)  
  37.         {  
  38.             for(j = 0; j < ncolumn; j++)  
  39.             {  
  40.                 printf("%s|", Result[i * ncolumn + j]);  
  41.             }  
  42.             printf("\n");  
  43.         }  
  44.     }  
  45.     sqlite3_free_table(Result);  
  46.     sqlite3_close(db);  
  47.   
  48.     return 0;  
  49. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 3.sqlite3_callback.c 
  4. * Description : 1) 回调函数完成数据库功能  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 17时57分53秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13. #include <stdlib.h>  
  14.   
  15. static sqlite3 *db;  
  16.   
  17. void create_table(sqlite3 *db)  
  18. {  
  19.     char *sql = NULL;  
  20.     char *errmsg = NULL;  
  21.     int ret;  
  22.   
  23.     sql = "create table if not exists mytable (id integer primary key, name text)";  
  24.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  25.     if(ret != SQLITE_OK)  
  26.     {  
  27.          printf("create table error: %s\n", errmsg);  
  28.     }  
  29.   
  30. }  
  31.   
  32. void insert_record(sqlite3 *db)  
  33. {  
  34.     char sql[100];  
  35.     char *errmsg = NULL;  
  36. //    char name[20];  
  37.     int name;  
  38.       
  39.     int ret;  
  40.     int id;  
  41.       
  42.     printf("Please input phone1 and phone2:\n");  
  43.     printf("phone1: ");  
  44.     scanf("%d", &id);  
  45.     getchar();  
  46.     printf("name: ");  
  47.     scanf("%d", &name);  
  48.     getchar();  
  49.   
  50.     sprintf(sql, "insert into test_table values(%d, %d)", id, name);  
  51.   
  52.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  53.     if(ret != SQLITE_OK)  
  54.     {  
  55.         printf("insert table error: %s\n", errmsg);  
  56.     }  
  57. }  
  58.   
  59. int displaycb(void *para, int ncolumn, char ** columnvalue, char *columnname[])  
  60. {  
  61.     int i;  
  62.   
  63.     printf("total column is %d\n", ncolumn);  
  64.   
  65.     for(i = 0; i < ncolumn; i++)  
  66.     {  
  67.         printf("col_name: %s-----> col_value: %s\n", columnname[i], columnvalue[i]);  
  68.         printf("============================\n");  
  69.   
  70.     }  
  71.     return 0;  
  72. }  
  73.   
  74. void inquire_usecb(sqlite3 *db)  
  75. {  
  76.     char *sql = NULL;  
  77.     char *errmsg = NULL;  
  78.     int ret;  
  79.   
  80.     sql = "select * from mytable";  
  81.     ret = sqlite3_exec(db, sql, displaycb, NULL, &errmsg);  
  82.     if(ret != SQLITE_OK)  
  83.     {  
  84.         printf("select is error: %s\n", errmsg);  
  85.     }  
  86. }  
  87.   
  88. int main()  
  89. {  
  90.     int ret;  
  91.   
  92.     ret = sqlite3_open("test.db", &db);  
  93.   
  94.     sqlite3_exec(db,"select * from test_table",displaycb, NULL, NULL);  
  95.     insert_record(db);  
  96.     sqlite3_close(db);  
  97.   
  98.     return 0;  
  99. }  

http://blog.csdn.net/qq_30594349/article/details/53015708?locationNum=7&fps=1
[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 1.sqlite3.c 
  4. * Description : 1)数据插入,删除,清空   
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char *errmsg = NULL;  
  16.   
  17. int main()  
  18. {  
  19.     int ret;  
  20.     int insert;  
  21.       
  22.     ret = sqlite3_open("test.db", &db);  
  23.     if(ret)  
  24.     {  
  25.         printf("can not open database.\n");  
  26.     }  
  27.     else  
  28.     {  
  29.         printf("open database succsee.\n");  
  30.     }  
  31.     insert =  sqlite3_exec(db, "insert into test_table values('wujibing',111,222)", 0, 0, &errmsg);//插入  
  32.     //insert = sqlite3_exec(db, "delete from test_table where name='xu'", 0, 0, &errmsg);//删除  
  33.     //insert = sqlite3_exec(db, "delete from test_table", 0, 0, &errmsg);//清空  
  34.     printf("exec_ret: %d\n", insert);  
  35.     printf("errMsg: %d\n", errmsg);  
  36.     sqlite3_close(db);  
  37.   
  38.     return 0;  
  39. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 2.sqlite3_display.c 
  4. * Description : 1) 显示数据  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 16时30分17秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char **Result = NULL;  
  16. static char *errmsg = NULL;  
  17.   
  18. int main()  
  19. {  
  20.     int ret;  
  21.     int i;  
  22.     int j;  
  23.     int nrow;  
  24.     int ncolumn;  
  25.   
  26.     ret = sqlite3_open("test.db", &db);  
  27. //    sqlite3_exec(db, ".head on", 0, 0, &errmsg);  
  28.     ret = sqlite3_get_table(db, "select * from test_table", &Result, &nrow, &ncolumn, &errmsg);  
  29.     if(ret)  
  30.     {  
  31.         printf("can not open database.\n");  
  32.     }  
  33.     else  
  34.     {  
  35.         printf("open database success.\n");  
  36.         for(i = 0; i <= nrow; i++)  
  37.         {  
  38.             for(j = 0; j < ncolumn; j++)  
  39.             {  
  40.                 printf("%s|", Result[i * ncolumn + j]);  
  41.             }  
  42.             printf("\n");  
  43.         }  
  44.     }  
  45.     sqlite3_free_table(Result);  
  46.     sqlite3_close(db);  
  47.   
  48.     return 0;  
  49. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 3.sqlite3_callback.c 
  4. * Description : 1) 回调函数完成数据库功能  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 17时57分53秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13. #include <stdlib.h>  
  14.   
  15. static sqlite3 *db;  
  16.   
  17. void create_table(sqlite3 *db)  
  18. {  
  19.     char *sql = NULL;  
  20.     char *errmsg = NULL;  
  21.     int ret;  
  22.   
  23.     sql = "create table if not exists mytable (id integer primary key, name text)";  
  24.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  25.     if(ret != SQLITE_OK)  
  26.     {  
  27.          printf("create table error: %s\n", errmsg);  
  28.     }  
  29.   
  30. }  
  31.   
  32. void insert_record(sqlite3 *db)  
  33. {  
  34.     char sql[100];  
  35.     char *errmsg = NULL;  
  36. //    char name[20];  
  37.     int name;  
  38.       
  39.     int ret;  
  40.     int id;  
  41.       
  42.     printf("Please input phone1 and phone2:\n");  
  43.     printf("phone1: ");  
  44.     scanf("%d", &id);  
  45.     getchar();  
  46.     printf("name: ");  
  47.     scanf("%d", &name);  
  48.     getchar();  
  49.   
  50.     sprintf(sql, "insert into test_table values(%d, %d)", id, name);  
  51.   
  52.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  53.     if(ret != SQLITE_OK)  
  54.     {  
  55.         printf("insert table error: %s\n", errmsg);  
  56.     }  
  57. }  
  58.   
  59. int displaycb(void *para, int ncolumn, char ** columnvalue, char *columnname[])  
  60. {  
  61.     int i;  
  62.   
  63.     printf("total column is %d\n", ncolumn);  
  64.   
  65.     for(i = 0; i < ncolumn; i++)  
  66.     {  
  67.         printf("col_name: %s-----> col_value: %s\n", columnname[i], columnvalue[i]);  
  68.         printf("============================\n");  
  69.   
  70.     }  
  71.     return 0;  
  72. }  
  73.   
  74. void inquire_usecb(sqlite3 *db)  
  75. {  
  76.     char *sql = NULL;  
  77.     char *errmsg = NULL;  
  78.     int ret;  
  79.   
  80.     sql = "select * from mytable";  
  81.     ret = sqlite3_exec(db, sql, displaycb, NULL, &errmsg);  
  82.     if(ret != SQLITE_OK)  
  83.     {  
  84.         printf("select is error: %s\n", errmsg);  
  85.     }  
  86. }  
  87.   
  88. int main()  
  89. {  
  90.     int ret;  
  91.   
  92.     ret = sqlite3_open("test.db", &db);  
  93.   
  94.     sqlite3_exec(db,"select * from test_table",displaycb, NULL, NULL);  
  95.     insert_record(db);  
  96.     sqlite3_close(db);  
  97.   
  98.     return 0;  
  99. }  

http://blog.csdn.net/qq_30594349/article/details/53015708?locationNum=7&fps=1
[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 1.sqlite3.c 
  4. * Description : 1)数据插入,删除,清空   
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char *errmsg = NULL;  
  16.   
  17. int main()  
  18. {  
  19.     int ret;  
  20.     int insert;  
  21.       
  22.     ret = sqlite3_open("test.db", &db);  
  23.     if(ret)  
  24.     {  
  25.         printf("can not open database.\n");  
  26.     }  
  27.     else  
  28.     {  
  29.         printf("open database succsee.\n");  
  30.     }  
  31.     insert =  sqlite3_exec(db, "insert into test_table values('wujibing',111,222)", 0, 0, &errmsg);//插入  
  32.     //insert = sqlite3_exec(db, "delete from test_table where name='xu'", 0, 0, &errmsg);//删除  
  33.     //insert = sqlite3_exec(db, "delete from test_table", 0, 0, &errmsg);//清空  
  34.     printf("exec_ret: %d\n", insert);  
  35.     printf("errMsg: %d\n", errmsg);  
  36.     sqlite3_close(db);  
  37.   
  38.     return 0;  
  39. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 2.sqlite3_display.c 
  4. * Description : 1) 显示数据  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 16时30分17秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char **Result = NULL;  
  16. static char *errmsg = NULL;  
  17.   
  18. int main()  
  19. {  
  20.     int ret;  
  21.     int i;  
  22.     int j;  
  23.     int nrow;  
  24.     int ncolumn;  
  25.   
  26.     ret = sqlite3_open("test.db", &db);  
  27. //    sqlite3_exec(db, ".head on", 0, 0, &errmsg);  
  28.     ret = sqlite3_get_table(db, "select * from test_table", &Result, &nrow, &ncolumn, &errmsg);  
  29.     if(ret)  
  30.     {  
  31.         printf("can not open database.\n");  
  32.     }  
  33.     else  
  34.     {  
  35.         printf("open database success.\n");  
  36.         for(i = 0; i <= nrow; i++)  
  37.         {  
  38.             for(j = 0; j < ncolumn; j++)  
  39.             {  
  40.                 printf("%s|", Result[i * ncolumn + j]);  
  41.             }  
  42.             printf("\n");  
  43.         }  
  44.     }  
  45.     sqlite3_free_table(Result);  
  46.     sqlite3_close(db);  
  47.   
  48.     return 0;  
  49. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 3.sqlite3_callback.c 
  4. * Description : 1) 回调函数完成数据库功能  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 17时57分53秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13. #include <stdlib.h>  
  14.   
  15. static sqlite3 *db;  
  16.   
  17. void create_table(sqlite3 *db)  
  18. {  
  19.     char *sql = NULL;  
  20.     char *errmsg = NULL;  
  21.     int ret;  
  22.   
  23.     sql = "create table if not exists mytable (id integer primary key, name text)";  
  24.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  25.     if(ret != SQLITE_OK)  
  26.     {  
  27.          printf("create table error: %s\n", errmsg);  
  28.     }  
  29.   
  30. }  
  31.   
  32. void insert_record(sqlite3 *db)  
  33. {  
  34.     char sql[100];  
  35.     char *errmsg = NULL;  
  36. //    char name[20];  
  37.     int name;  
  38.       
  39.     int ret;  
  40.     int id;  
  41.       
  42.     printf("Please input phone1 and phone2:\n");  
  43.     printf("phone1: ");  
  44.     scanf("%d", &id);  
  45.     getchar();  
  46.     printf("name: ");  
  47.     scanf("%d", &name);  
  48.     getchar();  
  49.   
  50.     sprintf(sql, "insert into test_table values(%d, %d)", id, name);  
  51.   
  52.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  53.     if(ret != SQLITE_OK)  
  54.     {  
  55.         printf("insert table error: %s\n", errmsg);  
  56.     }  
  57. }  
  58.   
  59. int displaycb(void *para, int ncolumn, char ** columnvalue, char *columnname[])  
  60. {  
  61.     int i;  
  62.   
  63.     printf("total column is %d\n", ncolumn);  
  64.   
  65.     for(i = 0; i < ncolumn; i++)  
  66.     {  
  67.         printf("col_name: %s-----> col_value: %s\n", columnname[i], columnvalue[i]);  
  68.         printf("============================\n");  
  69.   
  70.     }  
  71.     return 0;  
  72. }  
  73.   
  74. void inquire_usecb(sqlite3 *db)  
  75. {  
  76.     char *sql = NULL;  
  77.     char *errmsg = NULL;  
  78.     int ret;  
  79.   
  80.     sql = "select * from mytable";  
  81.     ret = sqlite3_exec(db, sql, displaycb, NULL, &errmsg);  
  82.     if(ret != SQLITE_OK)  
  83.     {  
  84.         printf("select is error: %s\n", errmsg);  
  85.     }  
  86. }  
  87.   
  88. int main()  
  89. {  
  90.     int ret;  
  91.   
  92.     ret = sqlite3_open("test.db", &db);  
  93.   
  94.     sqlite3_exec(db,"select * from test_table",displaycb, NULL, NULL);  
  95.     insert_record(db);  
  96.     sqlite3_close(db);  
  97.   
  98.     return 0;  
  99. }  

http://blog.csdn.net/qq_30594349/article/details/53015708?locationNum=7&fps=1
[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 1.sqlite3.c 
  4. * Description : 1)数据插入,删除,清空   
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char *errmsg = NULL;  
  16.   
  17. int main()  
  18. {  
  19.     int ret;  
  20.     int insert;  
  21.       
  22.     ret = sqlite3_open("test.db", &db);  
  23.     if(ret)  
  24.     {  
  25.         printf("can not open database.\n");  
  26.     }  
  27.     else  
  28.     {  
  29.         printf("open database succsee.\n");  
  30.     }  
  31.     insert =  sqlite3_exec(db, "insert into test_table values('wujibing',111,222)", 0, 0, &errmsg);//插入  
  32.     //insert = sqlite3_exec(db, "delete from test_table where name='xu'", 0, 0, &errmsg);//删除  
  33.     //insert = sqlite3_exec(db, "delete from test_table", 0, 0, &errmsg);//清空  
  34.     printf("exec_ret: %d\n", insert);  
  35.     printf("errMsg: %d\n", errmsg);  
  36.     sqlite3_close(db);  
  37.   
  38.     return 0;  
  39. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 2.sqlite3_display.c 
  4. * Description : 1) 显示数据  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 16时30分17秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char **Result = NULL;  
  16. static char *errmsg = NULL;  
  17.   
  18. int main()  
  19. {  
  20.     int ret;  
  21.     int i;  
  22.     int j;  
  23.     int nrow;  
  24.     int ncolumn;  
  25.   
  26.     ret = sqlite3_open("test.db", &db);  
  27. //    sqlite3_exec(db, ".head on", 0, 0, &errmsg);  
  28.     ret = sqlite3_get_table(db, "select * from test_table", &Result, &nrow, &ncolumn, &errmsg);  
  29.     if(ret)  
  30.     {  
  31.         printf("can not open database.\n");  
  32.     }  
  33.     else  
  34.     {  
  35.         printf("open database success.\n");  
  36.         for(i = 0; i <= nrow; i++)  
  37.         {  
  38.             for(j = 0; j < ncolumn; j++)  
  39.             {  
  40.                 printf("%s|", Result[i * ncolumn + j]);  
  41.             }  
  42.             printf("\n");  
  43.         }  
  44.     }  
  45.     sqlite3_free_table(Result);  
  46.     sqlite3_close(db);  
  47.   
  48.     return 0;  
  49. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 3.sqlite3_callback.c 
  4. * Description : 1) 回调函数完成数据库功能  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 17时57分53秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13. #include <stdlib.h>  
  14.   
  15. static sqlite3 *db;  
  16.   
  17. void create_table(sqlite3 *db)  
  18. {  
  19.     char *sql = NULL;  
  20.     char *errmsg = NULL;  
  21.     int ret;  
  22.   
  23.     sql = "create table if not exists mytable (id integer primary key, name text)";  
  24.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  25.     if(ret != SQLITE_OK)  
  26.     {  
  27.          printf("create table error: %s\n", errmsg);  
  28.     }  
  29.   
  30. }  
  31.   
  32. void insert_record(sqlite3 *db)  
  33. {  
  34.     char sql[100];  
  35.     char *errmsg = NULL;  
  36. //    char name[20];  
  37.     int name;  
  38.       
  39.     int ret;  
  40.     int id;  
  41.       
  42.     printf("Please input phone1 and phone2:\n");  
  43.     printf("phone1: ");  
  44.     scanf("%d", &id);  
  45.     getchar();  
  46.     printf("name: ");  
  47.     scanf("%d", &name);  
  48.     getchar();  
  49.   
  50.     sprintf(sql, "insert into test_table values(%d, %d)", id, name);  
  51.   
  52.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  53.     if(ret != SQLITE_OK)  
  54.     {  
  55.         printf("insert table error: %s\n", errmsg);  
  56.     }  
  57. }  
  58.   
  59. int displaycb(void *para, int ncolumn, char ** columnvalue, char *columnname[])  
  60. {  
  61.     int i;  
  62.   
  63.     printf("total column is %d\n", ncolumn);  
  64.   
  65.     for(i = 0; i < ncolumn; i++)  
  66.     {  
  67.         printf("col_name: %s-----> col_value: %s\n", columnname[i], columnvalue[i]);  
  68.         printf("============================\n");  
  69.   
  70.     }  
  71.     return 0;  
  72. }  
  73.   
  74. void inquire_usecb(sqlite3 *db)  
  75. {  
  76.     char *sql = NULL;  
  77.     char *errmsg = NULL;  
  78.     int ret;  
  79.   
  80.     sql = "select * from mytable";  
  81.     ret = sqlite3_exec(db, sql, displaycb, NULL, &errmsg);  
  82.     if(ret != SQLITE_OK)  
  83.     {  
  84.         printf("select is error: %s\n", errmsg);  
  85.     }  
  86. }  
  87.   
  88. int main()  
  89. {  
  90.     int ret;  
  91.   
  92.     ret = sqlite3_open("test.db", &db);  
  93.   
  94.     sqlite3_exec(db,"select * from test_table",displaycb, NULL, NULL);  
  95.     insert_record(db);  
  96.     sqlite3_close(db);  
  97.   
  98.     return 0;  
  99. }  

http://blog.csdn.net/qq_30594349/article/details/53015708?locationNum=7&fps=1
[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 1.sqlite3.c 
  4. * Description : 1)数据插入,删除,清空   
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char *errmsg = NULL;  
  16.   
  17. int main()  
  18. {  
  19.     int ret;  
  20.     int insert;  
  21.       
  22.     ret = sqlite3_open("test.db", &db);  
  23.     if(ret)  
  24.     {  
  25.         printf("can not open database.\n");  
  26.     }  
  27.     else  
  28.     {  
  29.         printf("open database succsee.\n");  
  30.     }  
  31.     insert =  sqlite3_exec(db, "insert into test_table values('wujibing',111,222)", 0, 0, &errmsg);//插入  
  32.     //insert = sqlite3_exec(db, "delete from test_table where name='xu'", 0, 0, &errmsg);//删除  
  33.     //insert = sqlite3_exec(db, "delete from test_table", 0, 0, &errmsg);//清空  
  34.     printf("exec_ret: %d\n", insert);  
  35.     printf("errMsg: %d\n", errmsg);  
  36.     sqlite3_close(db);  
  37.   
  38.     return 0;  
  39. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 2.sqlite3_display.c 
  4. * Description : 1) 显示数据  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 16时30分17秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char **Result = NULL;  
  16. static char *errmsg = NULL;  
  17.   
  18. int main()  
  19. {  
  20.     int ret;  
  21.     int i;  
  22.     int j;  
  23.     int nrow;  
  24.     int ncolumn;  
  25.   
  26.     ret = sqlite3_open("test.db", &db);  
  27. //    sqlite3_exec(db, ".head on", 0, 0, &errmsg);  
  28.     ret = sqlite3_get_table(db, "select * from test_table", &Result, &nrow, &ncolumn, &errmsg);  
  29.     if(ret)  
  30.     {  
  31.         printf("can not open database.\n");  
  32.     }  
  33.     else  
  34.     {  
  35.         printf("open database success.\n");  
  36.         for(i = 0; i <= nrow; i++)  
  37.         {  
  38.             for(j = 0; j < ncolumn; j++)  
  39.             {  
  40.                 printf("%s|", Result[i * ncolumn + j]);  
  41.             }  
  42.             printf("\n");  
  43.         }  
  44.     }  
  45.     sqlite3_free_table(Result);  
  46.     sqlite3_close(db);  
  47.   
  48.     return 0;  
  49. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 3.sqlite3_callback.c 
  4. * Description : 1) 回调函数完成数据库功能  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 17时57分53秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13. #include <stdlib.h>  
  14.   
  15. static sqlite3 *db;  
  16.   
  17. void create_table(sqlite3 *db)  
  18. {  
  19.     char *sql = NULL;  
  20.     char *errmsg = NULL;  
  21.     int ret;  
  22.   
  23.     sql = "create table if not exists mytable (id integer primary key, name text)";  
  24.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  25.     if(ret != SQLITE_OK)  
  26.     {  
  27.          printf("create table error: %s\n", errmsg);  
  28.     }  
  29.   
  30. }  
  31.   
  32. void insert_record(sqlite3 *db)  
  33. {  
  34.     char sql[100];  
  35.     char *errmsg = NULL;  
  36. //    char name[20];  
  37.     int name;  
  38.       
  39.     int ret;  
  40.     int id;  
  41.       
  42.     printf("Please input phone1 and phone2:\n");  
  43.     printf("phone1: ");  
  44.     scanf("%d", &id);  
  45.     getchar();  
  46.     printf("name: ");  
  47.     scanf("%d", &name);  
  48.     getchar();  
  49.   
  50.     sprintf(sql, "insert into test_table values(%d, %d)", id, name);  
  51.   
  52.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  53.     if(ret != SQLITE_OK)  
  54.     {  
  55.         printf("insert table error: %s\n", errmsg);  
  56.     }  
  57. }  
  58.   
  59. int displaycb(void *para, int ncolumn, char ** columnvalue, char *columnname[])  
  60. {  
  61.     int i;  
  62.   
  63.     printf("total column is %d\n", ncolumn);  
  64.   
  65.     for(i = 0; i < ncolumn; i++)  
  66.     {  
  67.         printf("col_name: %s-----> col_value: %s\n", columnname[i], columnvalue[i]);  
  68.         printf("============================\n");  
  69.   
  70.     }  
  71.     return 0;  
  72. }  
  73.   
  74. void inquire_usecb(sqlite3 *db)  
  75. {  
  76.     char *sql = NULL;  
  77.     char *errmsg = NULL;  
  78.     int ret;  
  79.   
  80.     sql = "select * from mytable";  
  81.     ret = sqlite3_exec(db, sql, displaycb, NULL, &errmsg);  
  82.     if(ret != SQLITE_OK)  
  83.     {  
  84.         printf("select is error: %s\n", errmsg);  
  85.     }  
  86. }  
  87.   
  88. int main()  
  89. {  
  90.     int ret;  
  91.   
  92.     ret = sqlite3_open("test.db", &db);  
  93.   
  94.     sqlite3_exec(db,"select * from test_table",displaycb, NULL, NULL);  
  95.     insert_record(db);  
  96.     sqlite3_close(db);  
  97.   
  98.     return 0;  
  99. }  

http://blog.csdn.net/qq_30594349/article/details/53015708?locationNum=7&fps=1



[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 1.sqlite3.c 
  4. * Description : 1)数据插入,删除,清空   
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char *errmsg = NULL;  
  16.   
  17. int main()  
  18. {  
  19.     int ret;  
  20.     int insert;  
  21.       
  22.     ret = sqlite3_open("test.db", &db);  
  23.     if(ret)  
  24.     {  
  25.         printf("can not open database.\n");  
  26.     }  
  27.     else  
  28.     {  
  29.         printf("open database succsee.\n");  
  30.     }  
  31.     insert =  sqlite3_exec(db, "insert into test_table values('wujibing',111,222)", 0, 0, &errmsg);//插入  
  32.     //insert = sqlite3_exec(db, "delete from test_table where name='xu'", 0, 0, &errmsg);//删除  
  33.     //insert = sqlite3_exec(db, "delete from test_table", 0, 0, &errmsg);//清空  
  34.     printf("exec_ret: %d\n", insert);  
  35.     printf("errMsg: %d\n", errmsg);  
  36.     sqlite3_close(db);  
  37.   
  38.     return 0;  
  39. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 2.sqlite3_display.c 
  4. * Description : 1) 显示数据  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 16时30分17秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char **Result = NULL;  
  16. static char *errmsg = NULL;  
  17.   
  18. int main()  
  19. {  
  20.     int ret;  
  21.     int i;  
  22.     int j;  
  23.     int nrow;  
  24.     int ncolumn;  
  25.   
  26.     ret = sqlite3_open("test.db", &db);  
  27. //    sqlite3_exec(db, ".head on", 0, 0, &errmsg);  
  28.     ret = sqlite3_get_table(db, "select * from test_table", &Result, &nrow, &ncolumn, &errmsg);  
  29.     if(ret)  
  30.     {  
  31.         printf("can not open database.\n");  
  32.     }  
  33.     else  
  34.     {  
  35.         printf("open database success.\n");  
  36.         for(i = 0; i <= nrow; i++)  
  37.         {  
  38.             for(j = 0; j < ncolumn; j++)  
  39.             {  
  40.                 printf("%s|", Result[i * ncolumn + j]);  
  41.             }  
  42.             printf("\n");  
  43.         }  
  44.     }  
  45.     sqlite3_free_table(Result);  
  46.     sqlite3_close(db);  
  47.   
  48.     return 0;  
  49. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 3.sqlite3_callback.c 
  4. * Description : 1) 回调函数完成数据库功能  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 17时57分53秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13. #include <stdlib.h>  
  14.   
  15. static sqlite3 *db;  
  16.   
  17. void create_table(sqlite3 *db)  
  18. {  
  19.     char *sql = NULL;  
  20.     char *errmsg = NULL;  
  21.     int ret;  
  22.   
  23.     sql = "create table if not exists mytable (id integer primary key, name text)";  
  24.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  25.     if(ret != SQLITE_OK)  
  26.     {  
  27.          printf("create table error: %s\n", errmsg);  
  28.     }  
  29.   
  30. }  
  31.   
  32. void insert_record(sqlite3 *db)  
  33. {  
  34.     char sql[100];  
  35.     char *errmsg = NULL;  
  36. //    char name[20];  
  37.     int name;  
  38.       
  39.     int ret;  
  40.     int id;  
  41.       
  42.     printf("Please input phone1 and phone2:\n");  
  43.     printf("phone1: ");  
  44.     scanf("%d", &id);  
  45.     getchar();  
  46.     printf("name: ");  
  47.     scanf("%d", &name);  
  48.     getchar();  
  49.   
  50.     sprintf(sql, "insert into test_table values(%d, %d)", id, name);  
  51.   
  52.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  53.     if(ret != SQLITE_OK)  
  54.     {  
  55.         printf("insert table error: %s\n", errmsg);  
  56.     }  
  57. }  
  58.   
  59. int displaycb(void *para, int ncolumn, char ** columnvalue, char *columnname[])  
  60. {  
  61.     int i;  
  62.   
  63.     printf("total column is %d\n", ncolumn);  
  64.   
  65.     for(i = 0; i < ncolumn; i++)  
  66.     {  
  67.         printf("col_name: %s-----> col_value: %s\n", columnname[i], columnvalue[i]);  
  68.         printf("============================\n");  
  69.   
  70.     }  
  71.     return 0;  
  72. }  
  73.   
  74. void inquire_usecb(sqlite3 *db)  
  75. {  
  76.     char *sql = NULL;  
  77.     char *errmsg = NULL;  
  78.     int ret;  
  79.   
  80.     sql = "select * from mytable";  
  81.     ret = sqlite3_exec(db, sql, displaycb, NULL, &errmsg);  
  82.     if(ret != SQLITE_OK)  
  83.     {  
  84.         printf("select is error: %s\n", errmsg);  
  85.     }  
  86. }  
  87.   
  88. int main()  
  89. {  
  90.     int ret;  
  91.   
  92.     ret = sqlite3_open("test.db", &db);  
  93.   
  94.     sqlite3_exec(db,"select * from test_table",displaycb, NULL, NULL);  
  95.     insert_record(db);  
  96.     sqlite3_close(db);  
  97.   
  98.     return 0;  
  99. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 1.sqlite3.c 
  4. * Description : 1)数据插入,删除,清空   
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char *errmsg = NULL;  
  16.   
  17. int main()  
  18. {  
  19.     int ret;  
  20.     int insert;  
  21.       
  22.     ret = sqlite3_open("test.db", &db);  
  23.     if(ret)  
  24.     {  
  25.         printf("can not open database.\n");  
  26.     }  
  27.     else  
  28.     {  
  29.         printf("open database succsee.\n");  
  30.     }  
  31.     insert =  sqlite3_exec(db, "insert into test_table values('wujibing',111,222)", 0, 0, &errmsg);//插入  
  32.     //insert = sqlite3_exec(db, "delete from test_table where name='xu'", 0, 0, &errmsg);//删除  
  33.     //insert = sqlite3_exec(db, "delete from test_table", 0, 0, &errmsg);//清空  
  34.     printf("exec_ret: %d\n", insert);  
  35.     printf("errMsg: %d\n", errmsg);  
  36.     sqlite3_close(db);  
  37.   
  38.     return 0;  
  39. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 2.sqlite3_display.c 
  4. * Description : 1) 显示数据  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 16时30分17秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13.   
  14. static sqlite3 *db = NULL;  
  15. static char **Result = NULL;  
  16. static char *errmsg = NULL;  
  17.   
  18. int main()  
  19. {  
  20.     int ret;  
  21.     int i;  
  22.     int j;  
  23.     int nrow;  
  24.     int ncolumn;  
  25.   
  26.     ret = sqlite3_open("test.db", &db);  
  27. //    sqlite3_exec(db, ".head on", 0, 0, &errmsg);  
  28.     ret = sqlite3_get_table(db, "select * from test_table", &Result, &nrow, &ncolumn, &errmsg);  
  29.     if(ret)  
  30.     {  
  31.         printf("can not open database.\n");  
  32.     }  
  33.     else  
  34.     {  
  35.         printf("open database success.\n");  
  36.         for(i = 0; i <= nrow; i++)  
  37.         {  
  38.             for(j = 0; j < ncolumn; j++)  
  39.             {  
  40.                 printf("%s|", Result[i * ncolumn + j]);  
  41.             }  
  42.             printf("\n");  
  43.         }  
  44.     }  
  45.     sqlite3_free_table(Result);  
  46.     sqlite3_close(db);  
  47.   
  48.     return 0;  
  49. }  

[cpp]  view plain  copy
  1. /* 
  2. ************************************************************************** 
  3. * File Name: 3.sqlite3_callback.c 
  4. * Description : 1) 回调函数完成数据库功能  
  5. *               2)  
  6. * Author   : Xubing  
  7. * Created Time: 2016年10月30日 星期日 17时57分53秒 
  8. ************************************************************************** 
  9. */  
  10.   
  11. #include <stdio.h>  
  12. #include <sqlite3.h>  
  13. #include <stdlib.h>  
  14.   
  15. static sqlite3 *db;  
  16.   
  17. void create_table(sqlite3 *db)  
  18. {  
  19.     char *sql = NULL;  
  20.     char *errmsg = NULL;  
  21.     int ret;  
  22.   
  23.     sql = "create table if not exists mytable (id integer primary key, name text)";  
  24.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  25.     if(ret != SQLITE_OK)  
  26.     {  
  27.          printf("create table error: %s\n", errmsg);  
  28.     }  
  29.   
  30. }  
  31.   
  32. void insert_record(sqlite3 *db)  
  33. {  
  34.     char sql[100];  
  35.     char *errmsg = NULL;  
  36. //    char name[20];  
  37.     int name;  
  38.       
  39.     int ret;  
  40.     int id;  
  41.       
  42.     printf("Please input phone1 and phone2:\n");  
  43.     printf("phone1: ");  
  44.     scanf("%d", &id);  
  45.     getchar();  
  46.     printf("name: ");  
  47.     scanf("%d", &name);  
  48.     getchar();  
  49.   
  50.     sprintf(sql, "insert into test_table values(%d, %d)", id, name);  
  51.   
  52.     ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);  
  53.     if(ret != SQLITE_OK)  
  54.     {  
  55.         printf("insert table error: %s\n", errmsg);  
  56.     }  
  57. }  
  58.   
  59. int displaycb(void *para, int ncolumn, char ** columnvalue, char *columnname[])  
  60. {  
  61.     int i;  
  62.   
  63.     printf("total column is %d\n", ncolumn);  
  64.   
  65.     for(i = 0; i < ncolumn; i++)  
  66.     {  
  67.         printf("col_name: %s-----> col_value: %s\n", columnname[i], columnvalue[i]);  
  68.         printf("============================\n");  
  69.   
  70.     }  
  71.     return 0;  
  72. }  
  73.   
  74. void inquire_usecb(sqlite3 *db)  
  75. {  
  76.     char *sql = NULL;  
  77.     char *errmsg = NULL;  
  78.     int ret;  
  79.   
  80.     sql = "select * from mytable";  
  81.     ret = sqlite3_exec(db, sql, displaycb, NULL, &errmsg);  
  82.     if(ret != SQLITE_OK)  
  83.     {  
  84.         printf("select is error: %s\n", errmsg);  
  85.     }  
  86. }  
  87.   
  88. int main()  
  89. {  
  90.     int ret;  
  91.   
  92.     ret = sqlite3_open("test.db", &db);  
  93.   
  94.     sqlite3_exec(db,"select * from test_table",displaycb, NULL, NULL);  
  95.     insert_record(db);  
  96.     sqlite3_close(db);  
  97.   
  98.     return 0;  
  99. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值