当前位置:首页 > 汽车 >

sqlite3参数设置(sqlite3操作实例)

来源:原点资讯(www.yd166.com)时间:2023-04-22 14:09:17作者:YD166手机阅读>>

这里如果数据库存在的话,就选择这个数据库;如果数据库不存在的话,系统会创建一个数据库test.db,然后选中这个数据库。

8.创建表(需要先选中数据库),和mysql中差不多

CREATE TABLE Student(

ID INT PRIMARY KEY NOT NULL,

NAME VARCHAR(20),

AGE INT);

sqlite3参数设置,sqlite3操作实例(5)

9.查看数据库中有哪些表:.tables

10.查看表的字段信息:.schema 表名

11.删除指定的表:drop table 表名

12.其他增删查改操作和mysql中一致。例如查找:

select * from student where ID=1;

sqlite3参数设置,sqlite3操作实例(6)

13.调整显示

sqlite3参数设置,sqlite3操作实例(7)

四、Linux下C语言操作Sqlite

https://blog.csdn.net/zouleideboke/article/details/73649886

http://blog.sina.com.cn/s/blog_4c7c21a9010009bm.html

https://blog.csdn.net/qq_43684922/article/details/90107557

https://bbs.csdn.net/topics/390088709?list=26597021

https://blog.csdn.net/weixin_43094387/article/details/86534423

sqlite3的函数手册可以去https://www.sqlite.org 官网查看,Document-> Programming Interfaces->Introduction to the C/C API 或者Document-> Programming Interfaces->C/C API Reference。

sqlite3就是一个结构体

typedef struct sqlite3 sqlite3;

1.打开数据库函数接口

SQLITE_API int sqlite3_open( const char *Filename, /* Database filename (UTF-8) */ sqlite3 **ppDb /* OUT: SQLite db handle */ );

参数:filename:很明显就是要打开的数据库的名称,如果数据库不存在的话,会自动建立一个

参数:ppDb:sqlite3数据库操作句柄,最开始定义sqlite3 *db=NULL,然后将&db传递给ppDb,使db指针指向打开的数据库连接对象。

sqlite3 *db;db就是一个指向数据库的指针,可以把db当成一个对象,sqlite3_open()等就像构造函数,sqlite3_close()等就像析构函数。

Each open SQLite database is represented by a pointer to an instance of the opaque structure named "sqlite3". It is useful to think of an sqlite3 pointer as an object. The sqlite3_open(), sqlite3_open16(), and sqlite3_open_v2() interfaces are its constructors, and sqlite3_close() and sqlite3_close_v2() are its destructors. There are many other interfaces (such as sqlite3_prepare_v2(), sqlite3_create_function(), and sqlite3_busy_timeout() to name but three) that are methods on an sqlite3 object.

返回值:成功返回SQLITE_OK,错误返回情况很多。

#define SQLITE_OK 0 /* Successful result */ /* beginning-of-error-codes */ #define SQLITE_ERROR 1 /* Generic error */ #define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */ #define SQLITE_PERM 3 /* Access permission denied */ #define SQLITE_ABORT 4 /* callback routine requested an abort */ #define SQLITE_BUSY 5 /* The database file is locked */ #define SQLITE_LOCKED 6 /* A table in the database is locked */ #define SQLITE_NOMEM 7 /* A malloc() failed */ #define SQLITE_READONLY 8 /* Attempt to write a readonly database */ #define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/ #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ #define sqlITE_CORRUPT 11 /* The database disk image is malformed */ #define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */ #define SQLITE_FULL 13 /* Insertion failed because database is full */ #define SQLITE_CANTOPEN 14 /* Unable to open the database file */ #define SQLITE_PROTOCOL 15 /* Database lock protocol error */ #define SQLITE_EMPTY 16 /* Internal use only */ #define SQLITE_SCHEMA 17 /* The database schema changed */ #define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ #define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */ #define SQLITE_MISMATCH 20 /* Data type mismatch */ #define SQLITE_MISUSE 21 /* Library used incorrectly */ #define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ #define SQLITE_AUTH 23 /* Authorization denied */ #define SQLITE_FORMAT 24 /* Not used */ #define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ #define SQLITE_NOTADB 26 /* File opened that is not a database file */ #define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */ #define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */ #define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */

2.错误信息

SQLITE_API const char *sqlite3_errmsg(sqlite3*);

The sqlite3_errmsg() return English-language text that describes the error,返回描述错误的英文描述。

2.关闭数据库

SQLITE_API int sqlite3_close(sqlite3*);

非常简单,直接把sqlite3指针传递过来,把数据库内存释放即可。

3.执行sql语句

SQLITE_API int sqlite3_exec( sqlite3*, /* An open database */ const char *sql, /* SQL to be evaluated */ int (*callback)(void*,int,char**,char**), /* Callback function */ void *, /* 1st argument to callback */ char **errmsg /* Error msg written here */ );

参数:sqlite3 *指向打开的数据库

参数:sql就是我们要执行的sql语句,要执行的sql语句都放在字符串里面,解析成sql语句进行执行

参数:callback回调函数,分析一下这个回调函数,

参数:void*,就是我们要传递给回调函数的参数,对应回调函数第一个参数。

参数 :errmsg,保存错误信息。先声明一个指针char * myerrmsg,把&myerrmsg传递给errmsg,当sqlite3_exec出错时,会调用sqlite3_malloc()函数分配空间存放错误描述,直接printf("zErrMsg = %s \n", zErrMsg)得到一串字符串信息,这串信息告诉你错在什么地方。在使用完errmsg后,需要调用sqlite3_free()释放空间。

参数:callback,回调函数,看一下回调函数

int sqlite_callback( void* pv, /* 由 sqlite3_exec() 的第四个参数传递而来 */ int argc, /* 表的列数 */ char** argv, /* 指向查询结果的指针数组, 可以由 sqlite3_column_text() 得到 */ char** col /* 指向表头名的指针数组, 可以由 sqlite3_column_name() 得到 */ );

回调函数用来显示查询结果,对每一条查询结果,调用一次回调函数。后面三个参数都是系统自动传递的,

argv指针数组,存储查询到的一行结果,根据前面的参数argc确定表的列数,就可以把一行结果存储在指针数组中了。

col:用来存储表头字段,方法同argv。

不需要使用回调函数的时候,sqlite3_exec参数3,4直接设置为NULL即可。

回调函数使用:

表内容如下:

sqlite3参数设置,sqlite3操作实例(8)

栏目热文

sqlite优缺点(sqlite 利弊)

sqlite优缺点(sqlite 利弊)

英文原文: https://blog.nelhage.com/post/reflections-on-performan...

2023-04-22 14:28:25查看全文 >>

sqlite3查询(sqlite3查询表信息)

sqlite3查询(sqlite3查询表信息)

今天学习使用Sqlite3创建数据库、表、运行查询,并记录学习过程欢迎大家一起交流分享。首先新建一个python文件命名...

2023-04-22 14:33:18查看全文 >>

sqlite3 菜鸟教程(如何注册sqlite3)

sqlite3 菜鸟教程(如何注册sqlite3)

白天看完阅兵式,咱们继续学习SQL,学好本领,报效祖国!前两天我们学会了创建数据库和创建表(Table),今天我们学习往...

2023-04-22 13:59:21查看全文 >>

sqlite3基本操作

sqlite3基本操作

前言数据在实际工作中应用非常广泛,数据库的产品也比较多,oracle、DB2、SQL2000、mySQL;基于嵌入式li...

2023-04-22 14:32:18查看全文 >>

sqlite3图解(sqlite3菜鸟笔记)

sqlite3图解(sqlite3菜鸟笔记)

开始使用这个功能强大且通用的数据库吧。应用程序经常需要保存数据。无论你的用户是创建简单的文本文档、复杂的图形布局、游戏...

2023-04-22 14:27:05查看全文 >>

sqlite怎么删除指定的记录(sqlite删除所有数据库)

sqlite怎么删除指定的记录(sqlite删除所有数据库)

一、概念:1.1什么是数据:在计算机科学中,数据是所有能输入计算机并被计算机程序处理的符号的介质的总称、是信息的集合1....

2023-04-22 14:33:36查看全文 >>

sqlite3数据库高级使用

sqlite3数据库高级使用

SQLite在《嵌入式数据库sqlite3命令操作基础篇-增删改查,小白一文入门》一文中讲解了如何实现sqlite3的基...

2023-04-22 14:36:29查看全文 >>

sqlite3功能(sqlite3官网中文)

sqlite3功能(sqlite3官网中文)

关注开源中国OSC头条号,获取最新技术资讯SQLite3 详细介绍sqlite3pp该库对SQLite3 API使用...

2023-04-22 14:30:00查看全文 >>

sqlite3数据库优缺点(sqlite适合多少数据量)

sqlite3数据库优缺点(sqlite适合多少数据量)

来源:机器之心本文约4700字,建议阅读8分钟。本文介绍了十三个适合中级 Python 开发人员练手的项目。[ 导 读 ...

2023-04-22 14:25:55查看全文 >>

sqlite3.5教程(sqlite3 最新版)

sqlite3.5教程(sqlite3 最新版)

SQLite是一种轻量级的嵌入式关系型数据库管理系统,它可以在多种操作系统上运行,并且无需独立的数据库服务器。以下是一个...

2023-04-22 14:40:21查看全文 >>

文档排行