SQLite安装与使用

在之前的文章中,给出了一个数据库知识的概览:《数据库概览》,本文介绍一些基础的、常用的SQLite数据库知识。

SQLite基础

SQL基础语法

  • 插入

    INSERT INTO 表名 (列名1,...) 
    VALUES (列1值,...);
    
  • 修改

    UPDATE 表名
    SET 列名1= 列1值, ....
    WHERE [条件表达式];
    
  • 删除

    DELETE FROM 表名
    WHERE [条件表达式];
    
  • 查询

    SELECT 列名1, .... FROM 表名;
    WHERE [条件表达式];
    
  • 创建表

    CREATE TABLE 库名.表名(
      列1名 类型 特征(主键、唯一、非空、自增、注释),
      ...
    );
    
  • 删除表

    DROP TABLE 表名;
    

SQLite接口函数

// 打开文件:如果文件存在,则尝试打开;如果文件不存在,则创建
// 注意:sqlite中这个文件就是一个库
// 不支持多线程访问,不支持多实例访问
SQLITE_API int sqlite3_open(
  const char*filename,   /* Database filename (UTF-8)*/
  sqlite3**ppDb          /* OUT: SQLite db handle*/
);
SQLITE_API int sqlite3_open16(
  const void*filename,   /* Database filename (UTF-16)*/
  sqlite3**ppDb          /* OUT: SQLite db handle*/
);

// 关闭文件, Open之后一定要close
SQLITE_API int sqlite3_close(sqlite3*);

// 执行操作
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*/
);

SQLITE_API int sqlite3_prepare(
  sqlite3*db,            /* Database handle*/
  const char*zSql,       /* SQL statement, UTF-8 encoded*/
  int nByte,              /* Maximum length of zSql in bytes.*/
  sqlite3_stmt**ppStmt,  /* OUT: Statement handle*/
  const char**pzTail     /* OUT: Pointer to unused portion of zSql*/
);

SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);

SQLITE_API int sqlite3_step(sqlite3_stmt*);
SQLITE_API int sqlite3_finalize(sqlite3_stmt*pStmt);

SQLITE_API const void*sqlite3_column_blob(sqlite3_stmt*, int iCol);
SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
SQLITE_API const unsigned char*sqlite3_column_text(sqlite3_stmt*, int iCol);
SQLITE_API const void*sqlite3_column_text16(sqlite3_stmt*, int iCol);
SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
SQLITE_API sqlite3_value*sqlite3_column_value(sqlite3_stmt*, int iCol);

SQLite 安装

SQLite官网:https://www.sqlite.org/index.html

SQLite官网下载地址:https://www.sqlite.org/download.html

在c++项目中使用SQLite 操作数据库有两种方式,接下来分别讲两种方式:

源文件操作数据库

C++项目中,通过调用SQLite 源文件代码,实现操作SQLite 数据库。 实现基本功能,包括创建数据库,打开连接数据库,建表,添加数据,关闭数据库的功能。

  1. 官网下载sqlite源文件,创建sqlite的文件夹,并将解压后的文件放入sqlite的文件夹:
    在这里插入图片描述

  2. 用到以下几个文件,特别是sqlite3.c sqlite3.h这两个文件。
    在这里插入图片描述

  3. vs2019新建VC++解决方案,新增项目。

  4. sqlite的文件夹,内含头文件和源文件放到项目里。

  5. 将项目设置为多字节字符集,避免字符集带来的问题。

    1. 多字节字符集使用sqlite3_open
    2. 宽字符集使用sqlite3_open16
      在这里插入图片描述
  6. C/C++混合编程,编译报头文件错误,sqlite3.c设置不使用预编译头,或者编译成动态库载入
    在这里插入图片描述

  7. 引用头文件,创建sqlite3对象,后续操作数据库都要用到

  8. 新建数据库, 此时可以看到项目目录下生成数据库文件。 注意:打开(连接)数据库,和新建是同一个函数,内部会判断是否存在,不存在会新建再打开,存在直接打开。

    #include “sqlite3.h”
    #include <iostream>
    
    int callback(void*, int argc, char* argv[], char* names[])
    {
       for (int i = 0; i < argc; i++)
        {
            std::cout << names[i] << "= " << argv[i] << std::endl;
       }
    	return 0; 
    }
    
    int main()
    {
        sqlite3* pdb = NULL;
        char* errMsg = NULL;
        int ret = sqlite3_open("edyun. db", &pdb);
        if (ret){
            std::cout << sqlite3_errmsg(pdb) << std::endl;
            return -1;
        else {
            std::cout << "open edyun.db success! \r\n";
        }
        
        const char* sql =
            "CREATE TABLE EDoYun(" \
            "ID	    INT PRIMARY KEY NOT NULL,"\
            "NAME   TEXT            NOT NULL);";
    	do {
    		ret = sqlite3_exec(pdb, sql, NULL, NULL, &errMsg);
        	if (ret != SQLITE_OK){
           		std::cout<< errMsg << " return " << ret << std::endl;
            	sqlite3_free(errMsg);
            	break;
        	} else {
    			std::cout<< "create table EDoYun success!" << std::endl;
            	break;
        	}
            sql = "INSERT INTO EDoYun (ID,NAME)VALUES(1,\"jueding\");";
            ret = sqlite3_exec(pdb, sq, NULL, NULL, &errMsg);
            if (ret != SQLITE_OK){
                std::cout<< errMsg << " return " << ret << std::endl;
                sqlite3_free(errMsg);
                break;
            } else {
                std::cout << "insert table EDoYun success!" << std::endl;
            }
    	
            sql = "SELECT * FROM EDoYun;";
            ret = sqlite3_exec(pdb, sql, callback, NULL, &errMsg);
            if (ret != SQLITE_OK){
                std::cout<< errMsg << " return " << ret << std::endl;
                sqlite3_free(errMsg);
                break;
            } else {
        		std:: cout<< "insert table EDoYun success!" << std::endl;
        	}
            sql = "DROP TABLE EDoYun;" ;
    		ret = sqlite3_exec(pdb, sql, NULL, NULL, &errMsg);
            if (ret != SQLITE_OK){
    			std::cout << errMsg << " return " << ret << std::endl;
                sqlite3_free(errMsg);
    			break;
            } else {
    			std::cout << " drop table EDoYun success!" << std::endl;
            }
        } while (false);
            
    	sqlite3_close(pdb);
    }
    
    

在这里插入图片描述在这里插入图片描述

动态库操作数据库

通过调用sqlite动态库,实现操作sqlite数据库,实现 基本功能,包括创建数据库,打开连接数据库,建表,添加数据,关闭数据库的功能。由vs2019创建的演示工程,在release文件夹下可以执行.exe文件演示

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值