当前位置:网站首页>Embedded database development programming (VI) -- C API

Embedded database development programming (VI) -- C API

2022-07-05 05:07:00 Light chasing rain

One 、 open 、 Shutdown and error handling

 Insert picture description here
Error handling
 Insert picture description here
 Insert picture description here

 Insert picture description here

#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
int main(int argc, char const *argv[])
{
    
    if (argc != 2)
    {
    
        printf("Please input db name\n");
        exit(1);
    }
    
    sqlite3 *db;

    int ret = sqlite3_open(argv[1], &db);

    if(ret != SQLITE_OK)
    {
    
        printf("sqlite3 open:%s",sqlite3_errmsg(db));
        exit(1);
    }

    printf("sqlite open db successful!\n");

    sqlite3_close(db);

    return 0;
}

 Insert picture description here

Two 、 perform sql

 Insert picture description here

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>

void print_error(int ret, char *err, sqlite3 *db)
{
    
    if (ret != SQLITE_OK)
    {
    
        printf("%s:%s\n",err, sqlite3_errmsg(db));
        exit(1);
    }
}

int main(int argc, char const *argv[])
{
    
    if (argc != 2)
    {
    
        printf("Please input db name\n");
        exit(1);
    }
    
    sqlite3 *db;
    char *errmsg;
    char sql[1024] = {
    0};

    int ret = sqlite3_open(argv[1], &db);

    print_error(ret, "sqlite_open",db);
    printf("sqlite open db successful!\n");

    memset(sql, 0, sizeof(sql));
    strcpy(sql, "create table IF NOT EXISTS student (integer primary key, name text, age integer)");

    ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);

    print_error(ret, "sqlite exec create table", db);
    
    sqlite3_close(db);

    return 0;
}

Input sqlitebrower test.db
 Insert picture description here
So it's a success , without sqlitebrower

Input :sudo apt-get install sqlitebrower

3、 ... and 、 encapsulation sql

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>

void print_error(int ret, char *err, sqlite3 *db)
{
    
    if (ret != SQLITE_OK)
    {
    
        printf("%s:%s\n",err, sqlite3_errmsg(db));
        exit(1);
    }
}

int main(int argc, char const *argv[])
{
    
    if (argc != 2)
    {
    
        printf("Please input db name\n");
        exit(1);
    }
    
    sqlite3 *db;
    char *errmsg;
    char sql[1024] = {
    0};
    int id;
    char name[32];
    int age;

    int ret = sqlite3_open(argv[1], &db);

    print_error(ret, "sqlite_open",db);
    printf("sqlite open db successful!\n");

    memset(sql, 0, sizeof(sql));
    strcpy(sql, "create table IF NOT EXISTS student (id integer primary key, name text, age integer)");

    ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);

    print_error(ret, "sqlite exec create table", db);

    // Insert 2 Row data :id,name,age  Keyboard entry 

    for (size_t i = 0; i < 2; i++)
    {
    
        printf("Please input id:\n");
        scanf("%d", &id);

        printf("Please input name:\n");
        scanf("%s", name);

        printf("Please input age:\n");
        scanf("%d", &age);

        //sql:insert into student(id, name, age) values()
        //sprintf(); fprintf

        memset(sql, 0, sizeof(sql));
        sprintf(sql, "insert into student(id, name, age) values(%d, '%s', %d)",id, name, age);
        printf("%s\n",sql);
        ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
        print_error(ret, "insert into", db);
    }
    
    // Delete 
    memset(sql, 0, sizeof(sql));
    printf("Please input delete name:\n");
    scanf("%s", name);
    sprintf(sql, "delete from student where name = '%s'", name);
    ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
    print_error(ret, "delete", db);

    sqlite3_close(db);

    return 0;
}

Four 、 Callback function

The line buffer
 Insert picture description here

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>

void print_error(int ret, char *err, sqlite3 *db)
{
    
    if (ret != SQLITE_OK)
    {
    
        printf("%s:%s\n",err, sqlite3_errmsg(db));
        exit(1);
    }
}

// In the pointer array 
int my_sqlite_callback(void *para, int columnCount, char **columnValue, char **columnName)
{
    
    int flag = *((int *)para);

    printf("flag = %d\n",flag);

    printf("columbCount = %d\n", columnCount);

    for (size_t i = 0; i < columnCount; i++)
    {
    
        printf("%s:%s|",columnName[i] ,columnValue[i]);
    }
    printf("\n");
    

    return 0;
}

int main(int argc, char const *argv[])
{
    
    if (argc != 2)
    {
    
        printf("Please input db name\n");
        exit(1);
    }
    
    sqlite3 *db;
    char *errmsg;
    char sql[1024] = {
    0};
    int id;
    char name[32];
    int age;

    int ret = sqlite3_open(argv[1], &db);

    print_error(ret, "sqlite_open",db);
    printf("sqlite open db successful!\n");

    memset(sql, 0, sizeof(sql));
    strcpy(sql, "create table IF NOT EXISTS student (id integer primary key, name text, age integer)");

    ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);

    print_error(ret, "sqlite exec create table", db);

    // Insert 2 Row data :id,name,age  Keyboard entry 
#if 0
    for (size_t i = 0; i < 1; i++)
    {
    
        printf("Please input id:\n");
        scanf("%d", &id);

        printf("Please input name:\n");
        scanf("%s", name);

        printf("Please input age:\n");
        scanf("%d", &age);

        //sql:insert into student(id, name, age) values()
        //sprintf(); fprintf

        memset(sql, 0, sizeof(sql));
        sprintf(sql, "insert into student(id, name, age) values(%d, '%s', %d)",id, name, age);
        printf("%s\n",sql);
        ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
        print_error(ret, "insert into", db);
    }

    // Delete 
    memset(sql, 0, sizeof(sql));
    printf("Please input delete name:\n");
    scanf("%s", name);
    sprintf(sql, "delete from student where name = '%s'", name);
    ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
    print_error(ret, "delete", db);
#endif
    memset(sql, 0, sizeof(sql));
    strcpy(sql, "select * from student");
    int flag = 0;
    ret = sqlite3_exec(db, sql, my_sqlite_callback, (void *)&flag, &errmsg);
    // How many lines , Will be executed less times 
    print_error(ret, "select", db);
    sqlite3_close(db);
    //flag Cannot be in function my_sqlite_callback Use in 
    printf("flag = %d\n",flag);
    // No select When ,callback It doesn't work 

    return 0;
}

5、 ... and 、 Fully buffered query

Global buffering
 Insert picture description here

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>

void print_error(int ret, char *err, sqlite3 *db)
{
    
    if (ret != SQLITE_OK)
    {
    
        printf("%s:%s\n",err, sqlite3_errmsg(db));
        exit(1);
    }
}

// In the pointer array 
int my_sqlite_callback(void *para, int columnCount, char **columnValue, char **columnName)
{
    
    int flag = *((int *)para);

    printf("flag = %d\n",flag);

    printf("columbCount = %d\n", columnCount);

    for (size_t i = 0; i < columnCount; i++)
    {
    
        printf("%s:%s|",columnName[i] ,columnValue[i]);
    }
    printf("\n");
    

    return 0;
}

int main(int argc, char const *argv[])
{
    
    if (argc != 2)
    {
    
        printf("Please input db name\n");
        exit(1);
    }
    
    sqlite3 *db;
    char *errmsg;
    char sql[1024] = {
    0};
    int id;
    char name[32];
    int age;

    int ret = sqlite3_open(argv[1], &db);

    print_error(ret, "sqlite_open",db);
    printf("sqlite open db successful!\n");

    memset(sql, 0, sizeof(sql));
    strcpy(sql, "create table IF NOT EXISTS student (id integer primary key, name text, age integer)");

    ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);

    print_error(ret, "sqlite exec create table", db);

    // Insert 2 Row data :id,name,age  Keyboard entry 
#if 0
    for (size_t i = 0; i < 1; i++)
    {
    
        printf("Please input id:\n");
        scanf("%d", &id);

        printf("Please input name:\n");
        scanf("%s", name);

        printf("Please input age:\n");
        scanf("%d", &age);

        //sql:insert into student(id, name, age) values()
        //sprintf(); fprintf

        memset(sql, 0, sizeof(sql));
        sprintf(sql, "insert into student(id, name, age) values(%d, '%s', %d)",id, name, age);
        printf("%s\n",sql);
        ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
        print_error(ret, "insert into", db);
    }

    // Delete 
    memset(sql, 0, sizeof(sql));
    printf("Please input delete name:\n");
    scanf("%s", name);
    sprintf(sql, "delete from student where name = '%s'", name);
    ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
    print_error(ret, "delete", db);
#endif

#if 0
    memset(sql, 0, sizeof(sql));
    strcpy(sql, "select * from student");
    int flag = 0;
    ret = sqlite3_exec(db, sql, my_sqlite_callback, (void *)&flag, &errmsg);
    // How many lines , Will be executed less times 
    print_error(ret, "select", db);
    //flag Cannot be in function my_sqlite_callback Use in 
    printf("flag = %d\n",flag);
    // No select When ,callback It doesn't work 
#endif
    char **result;
    int nrow;
    int ncolumn;

    memset(sql, 0, sizeof(sql));
    strcpy(sql, "select * from student");

    ret = sqlite3_get_table(db, sql, &result, &nrow, &ncolumn, &errmsg);
    // from 1 To traverse the , If you start from 0 Words , The column names will also be printed 
    for (size_t i = 1; i <= nrow; i++)
    {
    
        for (size_t j = 0; j < ncolumn; j++)
        {
    
            printf("%s|", result[i * ncolumn + j]);
        }
        printf("\n");
        
    }
    
    sqlite3_close(db);
    return 0;
}

6、 ... and 、 Byte buffer

Highest efficiency
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here

7、 ... and 、 summary

Just be careful , It should be sealed

原网站

版权声明
本文为[Light chasing rain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050456185701.html