108.sqllite3(C语言数据库库)详解

  • //创建数据库,插入表,生效
     1 //创建数据库,插入表,生效
     2 void create_database()
     3 {
     4     //数据库指针
     5     sqlite3 *db=0;
     6     //打开数据数据库,初始化指针
     7     int res = sqlite3_open("1.db", &db);
     8     //判断是否打开
     9     if (res!=SQLITE_OK)
    10     {
    11         printf("数据库无法打开");
    12         getchar();
    13         return;
    14     }
    15     else
    16     {
    17         printf("数据库成功");
    18         //创建数据库语句
    19         char *sqlcreatetable = "create table hello(id int primary key,name varchar(128))";
    20         char * error;
    21         //执行数据库语句
    22         int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
    23         //判断表格是否创建成功
    24         if (res != SQLITE_OK)
    25         {
    26             printf("表格插入失败");
    27             getchar();
    28             return;
    29         }
    30         //忽略错误
    31         sqlite3_free(error);
    32         //关闭数据库
    33         sqlite3_close(db);
    34     }
    35     system("pause");
    36 }
  • 创建数据库并实现绑定插入数据
     1 void  create_table_by_bind()
     2 {
     3     //数据库指针
     4     sqlite3 *db = 0;
     5     //打开数据数据库,初始化指针
     6     int res = sqlite3_open("1.db", &db);
     7     if (res != SQLITE_OK)
     8     {
     9         printf("数据库无法打开");
    10         getchar();
    11         return;
    12     }
    13     else
    14     {
    15         printf("数据库成功");
    16         //sql语句,创建数据库库
    17         char *sqlcreatetable = "create table hello(id int primary key,name varchar(128))";
    18         char * error;
    19         //执行sql语句
    20         int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
    21         if (res != SQLITE_OK)
    22         {
    23             printf("表格插入失败");
    24             getchar();
    25             return;
    26         }
    27 
    28         //状态指针
    29         sqlite3_stmt *stmt;
    30         //插入
    31         sqlite3_prepare_v2(db, "insert into hello (id,name)values(?,?)", -1, &stmt, 0);
    32         //插入数据
    33         for (int i = 0; i < 100; i++)
    34         {
    35             char str[256] = { 0 };
    36             sprintf(str, "xiaowang%d", i);
    37             //i与第一个参数绑定
    38             sqlite3_bind_int(stmt, 1, i);
    39             //str与第二个参数绑定
    40             sqlite3_bind_text(stmt, 2, str, strlen(str), NULL);
    41             //使状态生效
    42             sqlite3_step(stmt);
    43             //插入后重置
    44             sqlite3_reset(stmt);
    45         }
    46         //最终生效
    47         sqlite3_finalize(stmt);
    48         //忽略错误
    49         sqlite3_free(error);
    50         sqlite3_close(db);
    51     }
    52     system("pause");
    53 }
  • 插入一条数据
     1 //插入一条数据
     2 void insert()
     3 {
     4     sqlite3 *db = 0;//数据库指针
     5     //打开数据数据库,初始化指针
     6     int res = sqlite3_open("1.db", &db);
     7     //判断数据库是否打开成功
     8     if (res != SQLITE_OK)
     9     {
    10         printf("数据库无法打开");
    11         getchar();
    12         return;
    13     }
    14     else
    15     {
    16         printf("数据库成功");
    17         //数据库语句
    18         char *sqlcreatetable = "insert into hello(id,name)values(1,'xiaowang')";
    19         char * error;
    20         //执行sql语句
    21         int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
    22         //判断语句是否执行成功
    23         if (res != SQLITE_OK)
    24         {
    25             printf("表格插入失败");
    26             getchar();
    27             return;
    28         }
    29         //忽略错误
    30         sqlite3_free(error);
    31         //关闭数据库
    32         sqlite3_close(db);
    33     }
    34     system("pause");
    35 }
  • 插入多条
     1 //插入多条
     2 void insert_mul()
     3 {
     4     //数据库指针
     5     sqlite3 *db = 0;
     6     //打开数据数据库,初始化指针
     7     int res = sqlite3_open("1.db", &db);
     8     //判断数据库是否打开
     9     if (res != SQLITE_OK)
    10     {
    11         printf("数据库无法打开");
    12         getchar();
    13         return;
    14     }
    15     else
    16     {
    17         printf("数据库成功");
    18         char * error;
    19         for (int i = 2; i < 100; i++)
    20         {
    21             char strsql[256] = { 0 };
    22             //格式化执行语句
    23             sprintf(strsql, "insert into hello(id,name)values(%d,'xiaowang%d')", i, i);
    24         
    25             //执行sql语句
    26             int res = sqlite3_exec(db, strsql, NULL, NULL, &error);
    27             //判断sql语句是否执行成功
    28             if (res != SQLITE_OK)
    29             {
    30                 printf("表格插入失败");
    31                 getchar();
    32                 return;
    33             }
    34         }
    35         //忽略错误
    36         sqlite3_free(error);
    37         //关闭数据库
    38         sqlite3_close(db);
    39     }
    40     system("pause");
    41 }
  • 回调函数用于sql语句select使用
     1 //回调函数用于sql语句select使用
     2 int showall(void *params, int n_column, char **column_value, char **column_name)
     3 {
     4     //输出有多少列
     5     printf("n_column=%d
    ", n_column);
     6     for (int i = 0; i < n_column;i++)
     7     {
     8         //输出每一列
     9         printf("	%s", column_value[i]);
    10     }
    11     printf("
    ");
    12     return 0;
    13 
    14 }
  • 遍历所有的数据
     1 //遍历所有的数据
     2 void findall()
     3 {
     4     //数据库指针
     5     sqlite3 *db = 0;
     6     //打开数据库,初始化指针
     7     int res = sqlite3_open("1.db", &db);
     8     //判断数据库是否打开
     9     if (res != SQLITE_OK)
    10     {
    11         printf("数据库无法打开");
    12         getchar();
    13         return;
    14     }
    15     else
    16     {
    17         printf("数据库成功");
    18 
    19        //sql执行语句
    20         char *sqlcreatetable = "select * from hello";
    21         char * error;
    22         //执行sql语句,调用回调函数showall
    23         int res = sqlite3_exec(db, sqlcreatetable,showall, NULL, &error);
    24         if (res != SQLITE_OK)
    25         {
    26             printf("表格查询失败");
    27             getchar();
    28             return;
    29         }
    30         //忽略错误
    31         sqlite3_free(error);
    32         //关闭数据库
    33         sqlite3_close(db);
    34     }
    35     system("pause");
    36 }
  • 不用回调函数遍历所有数据
     1 void findall_direct()
     2 {
     3     //数据库指针
     4     sqlite3 *db = 0;
     5     //打开数据数据库,初始化指针
     6     int res = sqlite3_open("1.db", &db);
     7     //判断是否打开数据库
     8     if (res != SQLITE_OK)
     9     {
    10         printf("数据库无法打开");
    11         getchar();
    12         return;
    13     }
    14     else
    15     {
    16         printf("数据库成功");
    17         
    18         //sql语句
    19         char *sql = "select * from hello";
    20         //result存放读取的数据,error存放错误信息
    21         char ** result,*error;
    22         int nrow, ncolumn;
    23         //获取数据
    24         int  res = sqlite3_get_table(db, sql, &result, &nrow, &ncolumn, &error);
    25         //输出表有多少行多少列
    26         printf("row=%d,column=%d
    ", nrow, ncolumn);
    27 
    28         if (res==SQLITE_OK)
    29         {
    30             //输出每一列的名字信息
    31             //result前几个保存的是列的名字信息
    32             for (int j = 0; j < ncolumn; j++)
    33             {
    34                 printf("%s	", result[j]);
    35             }
    36             printf("
    ");
    37             for (int i = 0; i < nrow;i++)//遍历行
    38             {    
    39                 for (int j = 0; j < ncolumn; j++)
    40                 {
    41                     //显示数据
    42                     printf("%s	", result[(i+1)*ncolumn+j]);
    43                 }
    44                 printf("
    ");
    45             }
    46         }
    47 
    48         //释放表
    49         sqlite3_free_table(result);
    50         //忽略错误
    51         sqlite3_free(error);
    52         //关闭数据库
    53         sqlite3_close(db);
    54     }
    55     system("pause");
    56 
    57 }
  • 删除一条数据
     1 void  delete_data()
     2 {
     3     //数据库指针
     4     sqlite3 *db = 0;
     5     //打开数据数据库,初始化指针
     6     int res = sqlite3_open("1.db", &db);
     7     if (res != SQLITE_OK)
     8     {
     9         printf("数据库无法打开");
    10         getchar();
    11         return;
    12     }
    13     else
    14     {
    15         printf("数据库成功");
    16         //sql语言
    17         //char *sqlcreatetable = "delete from hello  where id<27";
    18         //char *sqlcreatetable = "delete from hello  where name= 'xiaowang47' 
    19         char *sqlcreatetable = "delete from hello  where name= 'haihua47' or id<45 ";
    20         char * error;
    21         //执行sql语句
    22         int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
    23         if (res != SQLITE_OK)
    24         {
    25             printf("表格插入失败");
    26             getchar();
    27             return;
    28         }
    29         //忽略错误
    30         sqlite3_free(error);
    31         //关闭数据库
    32         sqlite3_close(db);
    33     }
    34     system("pause");
    35 }
  • 更新数据库
     1 void  updata_data()
     2 {
     3     //数据库指针
     4     sqlite3 *db = 0;
     5     //打开数据数据库,初始化指针
     6     int res = sqlite3_open("1.db", &db);
     7     if (res != SQLITE_OK)
     8     {
     9         printf("数据库无法打开");
    10         getchar();
    11         return;
    12     }
    13     else
    14     {
    15         printf("数据库成功");
    16 
    17         //sql语句
    18         char *sqlcreatetable = "update hello set name='fangfang' where id=50 ";
    19         char * error;
    20         int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
    21         if (res != SQLITE_OK)
    22         {
    23             printf("表格插入失败");
    24             getchar();
    25             return;
    26         }
    27         //忽略错误
    28         sqlite3_free(error);
    29         //关闭数据库
    30         sqlite3_close(db);
    31     }
    32     system("pause");
    33 }
  • 删除表
     1 void delete_table()
     2 {
     3     //数据库指针
     4     sqlite3 *db = 0;
     5     //打开数据数据库,初始化指针
     6     int res = sqlite3_open("1.db", &db);
     7     if (res != SQLITE_OK)
     8     {
     9         printf("数据库无法打开");
    10         getchar();
    11         return;
    12     }
    13     else
    14     {
    15         printf("数据库成功");
    16         //删除所有表中的数据,并删除表
    17         char *sqlcreatetable = "drop table if exists hello ";
    18         char * error;
    19         int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
    20         if (res != SQLITE_OK)
    21         {
    22             printf("表格插入失败");
    23             getchar();
    24             return;
    25         }
    26         sqlite3_free(error);//忽略错误
    27         sqlite3_close(db);
    28     }
    29     system("pause");
    30 }

完整代码

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include "sqlite3.h"
  5 //  项目属性, C/C++ SDL 选择否,屏蔽安全生命周期检查
  6 
  7 //创建数据库,插入表,生效
  8 void create_database()
  9 {
 10     //数据库指针
 11     sqlite3 *db=0;
 12     //打开数据数据库,初始化指针
 13     int res = sqlite3_open("1.db", &db);
 14     //判断是否打开
 15     if (res!=SQLITE_OK)
 16     {
 17         printf("数据库无法打开");
 18         getchar();
 19         return;
 20     }
 21     else
 22     {
 23         printf("数据库成功");
 24         //创建数据库语句
 25         char *sqlcreatetable = "create table hello(id int primary key,name varchar(128))";
 26         char * error;
 27         //执行数据库语句
 28         int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
 29         //判断表格是否创建成功
 30         if (res != SQLITE_OK)
 31         {
 32             printf("表格插入失败");
 33             getchar();
 34             return;
 35         }
 36         //忽略错误
 37         sqlite3_free(error);
 38         //关闭数据库
 39         sqlite3_close(db);
 40     }
 41     system("pause");
 42 }
 43 
 44 //创建数据库并实现绑定插入数据
 45 void  create_table_by_bind()
 46 {
 47     //数据库指针
 48     sqlite3 *db = 0;
 49     //打开数据数据库,初始化指针
 50     int res = sqlite3_open("1.db", &db);
 51     if (res != SQLITE_OK)
 52     {
 53         printf("数据库无法打开");
 54         getchar();
 55         return;
 56     }
 57     else
 58     {
 59         printf("数据库成功");
 60         //sql语句,创建数据库库
 61         char *sqlcreatetable = "create table hello(id int primary key,name varchar(128))";
 62         char * error;
 63         //执行sql语句
 64         int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
 65         if (res != SQLITE_OK)
 66         {
 67             printf("表格插入失败");
 68             getchar();
 69             return;
 70         }
 71 
 72         //状态指针
 73         sqlite3_stmt *stmt;
 74         //插入
 75         sqlite3_prepare_v2(db, "insert into hello (id,name)values(?,?)", -1, &stmt, 0);
 76         //插入数据
 77         for (int i = 0; i < 100; i++)
 78         {
 79             char str[256] = { 0 };
 80             sprintf(str, "xiaowang%d", i);
 81             //i与第一个参数绑定
 82             sqlite3_bind_int(stmt, 1, i);
 83             //str与第二个参数绑定
 84             sqlite3_bind_text(stmt, 2, str, strlen(str), NULL);
 85             //使状态生效
 86             sqlite3_step(stmt);
 87             //插入后重置
 88             sqlite3_reset(stmt);
 89         }
 90         //最终生效
 91         sqlite3_finalize(stmt);
 92         //忽略错误
 93         sqlite3_free(error);
 94         sqlite3_close(db);
 95     }
 96     system("pause");
 97 }
 98 //插入一条数据
 99 void insert()
100 {
101     sqlite3 *db = 0;//数据库指针
102     //打开数据数据库,初始化指针
103     int res = sqlite3_open("1.db", &db);
104     //判断数据库是否打开成功
105     if (res != SQLITE_OK)
106     {
107         printf("数据库无法打开");
108         getchar();
109         return;
110     }
111     else
112     {
113         printf("数据库成功");
114         //数据库语句
115         char *sqlcreatetable = "insert into hello(id,name)values(1,'xiaowang')";
116         char * error;
117         //执行sql语句
118         int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
119         //判断语句是否执行成功
120         if (res != SQLITE_OK)
121         {
122             printf("表格插入失败");
123             getchar();
124             return;
125         }
126         //忽略错误
127         sqlite3_free(error);
128         //关闭数据库
129         sqlite3_close(db);
130     }
131     system("pause");
132 }
133 
134 //插入多条
135 void insert_mul()
136 {
137     //数据库指针
138     sqlite3 *db = 0;
139     //打开数据数据库,初始化指针
140     int res = sqlite3_open("1.db", &db);
141     //判断数据库是否打开
142     if (res != SQLITE_OK)
143     {
144         printf("数据库无法打开");
145         getchar();
146         return;
147     }
148     else
149     {
150         printf("数据库成功");
151         char * error;
152         for (int i = 2; i < 100; i++)
153         {
154             char strsql[256] = { 0 };
155             //格式化执行语句
156             sprintf(strsql, "insert into hello(id,name)values(%d,'xiaowang%d')", i, i);
157         
158             //执行sql语句
159             int res = sqlite3_exec(db, strsql, NULL, NULL, &error);
160             //判断sql语句是否执行成功
161             if (res != SQLITE_OK)
162             {
163                 printf("表格插入失败");
164                 getchar();
165                 return;
166             }
167         }
168         //忽略错误
169         sqlite3_free(error);
170         //关闭数据库
171         sqlite3_close(db);
172     }
173     system("pause");
174 }
175 
176 //回调函数用于sql语句select使用
177 int showall(void *params, int n_column, char **column_value, char **column_name)
178 {
179     //输出有多少列
180     printf("n_column=%d
", n_column);
181     for (int i = 0; i < n_column;i++)
182     {
183         //输出每一列
184         printf("	%s", column_value[i]);
185     }
186     printf("
");
187     return 0;
188 
189 }
190 
191 //遍历所有的数据
192 void findall()
193 {
194     //数据库指针
195     sqlite3 *db = 0;
196     //打开数据库,初始化指针
197     int res = sqlite3_open("1.db", &db);
198     //判断数据库是否打开
199     if (res != SQLITE_OK)
200     {
201         printf("数据库无法打开");
202         getchar();
203         return;
204     }
205     else
206     {
207         printf("数据库成功");
208 
209        //sql执行语句
210         char *sqlcreatetable = "select * from hello";
211         char * error;
212         //执行sql语句,调用回调函数showall
213         int res = sqlite3_exec(db, sqlcreatetable,showall, NULL, &error);
214         if (res != SQLITE_OK)
215         {
216             printf("表格查询失败");
217             getchar();
218             return;
219         }
220         //忽略错误
221         sqlite3_free(error);
222         //关闭数据库
223         sqlite3_close(db);
224     }
225     system("pause");
226 }
227 
228 //不用回调函数遍历所有数据
229 void findall_direct()
230 {
231     //数据库指针
232     sqlite3 *db = 0;
233     //打开数据数据库,初始化指针
234     int res = sqlite3_open("1.db", &db);
235     //判断是否打开数据库
236     if (res != SQLITE_OK)
237     {
238         printf("数据库无法打开");
239         getchar();
240         return;
241     }
242     else
243     {
244         printf("数据库成功");
245         
246         //sql语句
247         char *sql = "select * from hello";
248         //result存放读取的数据,error存放错误信息
249         char ** result,*error;
250         int nrow, ncolumn;
251         //获取数据
252         int  res = sqlite3_get_table(db, sql, &result, &nrow, &ncolumn, &error);
253         //输出表有多少行多少列
254         printf("row=%d,column=%d
", nrow, ncolumn);
255 
256         if (res==SQLITE_OK)
257         {
258             //输出每一列的名字信息
259             //result前几个保存的是列的名字信息
260             for (int j = 0; j < ncolumn; j++)
261             {
262                 printf("%s	", result[j]);
263             }
264             printf("
");
265             for (int i = 0; i < nrow;i++)//遍历行
266             {    
267                 for (int j = 0; j < ncolumn; j++)
268                 {
269                     //显示数据
270                     printf("%s	", result[(i+1)*ncolumn+j]);
271                 }
272                 printf("
");
273             }
274         }
275 
276         //释放表
277         sqlite3_free_table(result);
278         //忽略错误
279         sqlite3_free(error);
280         //关闭数据库
281         sqlite3_close(db);
282     }
283     system("pause");
284 
285 }
286 
287 //删除一条数据
288 void  delete_data()
289 {
290     //数据库指针
291     sqlite3 *db = 0;
292     //打开数据数据库,初始化指针
293     int res = sqlite3_open("1.db", &db);
294     if (res != SQLITE_OK)
295     {
296         printf("数据库无法打开");
297         getchar();
298         return;
299     }
300     else
301     {
302         printf("数据库成功");
303         //sql语言
304         //char *sqlcreatetable = "delete from hello  where id<27";
305         //char *sqlcreatetable = "delete from hello  where name= 'xiaowang47' 
306         char *sqlcreatetable = "delete from hello  where name= 'haihua47' or id<45 ";
307         char * error;
308         //执行sql语句
309         int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
310         if (res != SQLITE_OK)
311         {
312             printf("表格插入失败");
313             getchar();
314             return;
315         }
316         //忽略错误
317         sqlite3_free(error);
318         //关闭数据库
319         sqlite3_close(db);
320     }
321     system("pause");
322 }
323 
324 //更新数据库
325 void  updata_data()
326 {
327     //数据库指针
328     sqlite3 *db = 0;
329     //打开数据数据库,初始化指针
330     int res = sqlite3_open("1.db", &db);
331     if (res != SQLITE_OK)
332     {
333         printf("数据库无法打开");
334         getchar();
335         return;
336     }
337     else
338     {
339         printf("数据库成功");
340 
341         //sql语句
342         char *sqlcreatetable = "update hello set name='fangfang' where id=50 ";
343         char * error;
344         int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
345         if (res != SQLITE_OK)
346         {
347             printf("表格插入失败");
348             getchar();
349             return;
350         }
351         //忽略错误
352         sqlite3_free(error);
353         //关闭数据库
354         sqlite3_close(db);
355     }
356     system("pause");
357 }
358 
359 //删除表
360 void delete_table()
361 {
362     //数据库指针
363     sqlite3 *db = 0;
364     //打开数据数据库,初始化指针
365     int res = sqlite3_open("1.db", &db);
366     if (res != SQLITE_OK)
367     {
368         printf("数据库无法打开");
369         getchar();
370         return;
371     }
372     else
373     {
374         printf("数据库成功");
375         //删除所有表中的数据,并删除表
376         char *sqlcreatetable = "drop table if exists hello ";
377         char * error;
378         int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
379         if (res != SQLITE_OK)
380         {
381             printf("表格插入失败");
382             getchar();
383             return;
384         }
385         sqlite3_free(error);//忽略错误
386         sqlite3_close(db);
387     }
388     system("pause");
389 }
原文地址:https://www.cnblogs.com/xiaochi/p/8504030.html