这里如果数据库存在的话,就选择这个数据库;如果数据库不存在的话,系统会创建一个数据库test.db,然后选中这个数据库。
8.创建表(需要先选中数据库),和mysql中差不多
CREATE TABLE Student(
ID INT PRIMARY KEY NOT NULL,
NAME VARCHAR(20),
AGE INT);
9.查看数据库中有哪些表:.tables
10.查看表的字段信息:.schema 表名
11.删除指定的表:drop table 表名
12.其他增删查改操作和mysql中一致。例如查找:
select * from student where ID=1;
13.调整显示
四、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即可。
回调函数使用:
表内容如下: