Clion创建项目sqlite数据库

1.创建工程并运行

(1)新建——project,选择工程文件夹,创建工程

image-20241226104938570

(2)进入界面,等待项目启动,显示main.cpp主文件

image-20241226105011680

(3)配置编译环境

默认生成了main.cpp文件,但是我们不能运行。我们缺少编译环境,点击Setting打开设置,搜索工具链,发现Clion提供的编译环境有几种MinGW,CygWin,Visual studio等。我此处选择的时MinGW,因为我的安装了MinGW,默认选择了MinGW,如果你没有下载的话,后面的位置原先有个download,点击就可以下载了。安装好之后,然后再Environment右侧选择mingw-w64的位置即可。

image-20241226105450116

(4)运行main.cpp文件即可

image-20241226105043555

2.连接Sqlite数据库

(1)首先前往SQLite官网下载sqlite3所需文件 SQLite Download Page

image-20241226112441040

(2)解压文件,将其中的sqlite3.c和sqlite3.h拷贝到你对应的文件工程中

image-20241226110140287

(3)修改CMakeLists.txt文件,添加编译选项及连接文件

image-20241226111546797

cmake_minimum_required(VERSION 3.28)
project(MyDemo)

set(CMAKE_CXX_STANDARD 17)


add_executable(MyDemo main.cpp sqlite3.c)

# 指定包含目录
target_include_directories(MyDemo PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
(4)创建Sqlite数据库

image-20241226110557142

(5)使用Navicate监视刚才创建的Sqlite数据库

image-20241226110929849

(6)编写代码:数据库连接、表创建、数据表读写
#include <iostream>
#include <sqlite3.h>

// 回调函数用于处理查询结果
static int callback(void* NotUsed, int argc, char** argv, char** azColName) {
    for (int i = 0; i < argc; i++) {
        std::cout << azColName[i] << ": " << (argv[i] ? argv[i] : "NULL") << std::endl;
    }
    std::cout << std::endl;
    return 0;
}

int main() {
    sqlite3* DB;
    char* errorMessage = 0;
    int exit = 0;

    // 打开或创建数据库文件
    exit = sqlite3_open("D:/WorkOther/MyDemo/test.db", &DB);

    // 检查是否成功打开数据库
    if (exit) {
        std::cerr << "Error open DB: " << sqlite3_errmsg(DB) << std::endl;
        return -1;
    } else {
        std::cout << "Opened Database Successfully!" << std::endl;
    }

    // SQL语句:创建表
    const char* sql_create =
        "CREATE TABLE IF NOT EXISTS COMPANY("
        "ID INT PRIMARY KEY NOT NULL,"
        "NAME TEXT NOT NULL,"
        "AGE INT NOT NULL,"
        "ADDRESS CHAR(50),"
        "SALARY REAL);";

    // 执行SQL语句
    exit = sqlite3_exec(DB, sql_create, 0, 0, &errorMessage);
    if (exit != SQLITE_OK) {
        std::cerr << "Error CREATE TABLE: " << errorMessage << std::endl;
        sqlite3_free(errorMessage);
    } else {
        std::cout << "Table created successfully" << std::endl;
    }

    // 插入数据到表中
    const char* sql_insert =
        "INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) "
        "VALUES (1, 'Paul', 32, 'California', 20000.00 ); "
        "INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) "
        "VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); "
        "INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) "
        "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 ); "
        "INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) "
        "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 ); ";

    // 执行SQL语句
    exit = sqlite3_exec(DB, sql_insert, 0, 0, &errorMessage);
    if (exit != SQLITE_OK) {
        std::cerr << "Error INSERT: " << errorMessage << std::endl;
        sqlite3_free(errorMessage);
    } else {
        std::cout << "Records created successfully" << std::endl;
    }

    // 查询表中的数据
    const char* sql_select = "SELECT * FROM COMPANY";
    exit = sqlite3_exec(DB, sql_select, callback, 0, &errorMessage);
    if (exit != SQLITE_OK) {
        std::cerr << "Error SELECT: " << errorMessage << std::endl;
        sqlite3_free(errorMessage);
    } else {
        std::cout << "Operation done successfully" << std::endl;
    }

    // 关闭数据库连接
    sqlite3_close(DB);

    return 0;
}

image-20241226111357560

(7)数据库查询

image-20241226111627794

posted @ 2024-12-26 11:27  德琪  阅读(16)  评论(0编辑  收藏  举报