Native层如何使用sqlite数据库

2023-12-13 18:14:01

一、进入sqlite官网下载sqlite源码。

二、将sqlite源码中的sqlite3.c和sqlite3.h加入工程,并添加到CMakeLists.txt,就可以使用sqlite数据库了。

工程项目结构:

CMakeLists.txt:

# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.22.1)

# Declares and names the project.
project("test-project")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
        test

        # Sets the library as a shared library.
        SHARED

        sqlite3.c
        # Provides a relative path to your source file(s).
        test.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log )

include_directories(include)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
        test

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

三、调用sqlite的接口进行数据库的创建、数据的添加和查询。

#include <jni.h>
#include <android/log.h>
#include <sqlite3.h>

extern "C"
JNIEXPORT void JNICALL
Java_com_example_MainActivity_testMethod(JNIEnv *env, jobject) {
    sqlite3 *db;
    char *err_msg = 0;
    // 创建表
    const char *create_table_sql = "create table if not exists user(id integer, name text);";
    // 插入数据
    const char *insert_data_sql = "insert into user(id, name) values(200, 'hyh');";
    // 查询数据
    const char *query_sql = "select * from user;";

    // 打开数据库
    int rc = sqlite3_open("test.db", &db);
    if (rc != SQLITE_OK) {
        __android_log_print(ANDROID_LOG_INFO, "TAG", "Open db failed: %s\n", sqlite3_errmsg(db));
        goto End;
    }

    // 执行SQL语句创建一个表
    rc = sqlite3_exec(db, create_table_sql, 0, 0, &err_msg);
    if (rc != SQLITE_OK) {
        __android_log_print(ANDROID_LOG_INFO, "TAG", "Create table failed: %s\n", err_msg);
        sqlite3_free(err_msg);
        goto End;
    }

    // 执行SQL语句插入数据
    rc = sqlite3_exec(db, insert_data_sql, 0, 0, &err_msg);
    if (rc != SQLITE_OK) {
        __android_log_print(ANDROID_LOG_INFO, "TAG", "Insert data failed: %s\n", err_msg);
        goto End;
    }

    sqlite3_stmt *stmt;
    // 准备查询语句
    rc = sqlite3_prepare_v2(db, query_sql, -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        __android_log_print(ANDROID_LOG_INFO, "TAG", "Query data failed: %s\n", err_msg);
        goto End;
    }

    // 执行查询并打印结果
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        int id = sqlite3_column_int(stmt, 0);
        const unsigned char *name = sqlite3_column_text(stmt, 1);
        __android_log_print(ANDROID_LOG_INFO, "TAG", "id=%d,name=%s", id, name);
    }
    // 关闭查询语句
    sqlite3_finalize(stmt);

    End:
    // 关闭数据库连接
    sqlite3_close(db);
}

四、如果执行成功,则会打印"id=100,name=hyh"。

文章来源:https://blog.csdn.net/hengfeng430/article/details/134974869
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。