Linux下c编程sqlite3的基本语句

目录

 

Ubuntu下安装sqlite3

sqlite3的基本系统命令

sqlite3基本命令 

使用sqlite3查看已经创建的表

使用C语言的接口函数来进行对数据库的增删改查的操作 

编程示例 

测试实例


  • Ubuntu下安装sqlite3

  1. 命令行安装
    sudo apt-get install sqlite3
  2. 检查是否安装成功
    //在shell界面下输入
     
    sqlite3

    扫描二维码关注公众号,回复: 11006856 查看本文章
  • sqlite3的基本系统命令

  1. sqlite3的系统命令是以‘.’开头的;
  2. 基本命令
    .help                  //打印帮助信息
    .databases             //列出附加数据库的名称和文件
    .exit                  //退出程序
    .quit                  //退出程序
    .schema                //查看当前表结构
    .show                  //显示各种设置的当前值

  • sqlite3基本命令 

  1. 创建一个表
     create table ex (id integer, name char, tel integer);  //创建一个表
  2. 插入一条数据

     insert into ex values(1001, "zhangsan", 138789);    
  3. 查询数据

    select * from ex;  //查询所有数据
    
    select * from ex where id = 1001;      //按指定id的内容查询数据
    select * from ex where name = "lisi";  //按指定name的内容查询数据
  4. 更新一条数据

    update ex set name = "update" where id = 1001;  //将id = 1001 的name 更新为 update
    
  5. 删除一条数据

    delete from ex where id = 1001;  //删除id为1001的所有数据
  • 使用sqlite3查看已经创建的表

    sqlite3 ex.db

  • 使用C语言的接口函数来进行对数据库的增删改查的操作 

  1. 创建一个表
    int sqlite_create(char *pthaname)
    {
        sqlite3*          db;
        const char*       sql_create = "create table stu (id integer, name char, score integer)";
        printf("access ok ");
        int               cmd;
    
        if (access(PATHNAME, F_OK) != 0)  //判断数据库文件是否存在
        {
    
            if (sqlite3_open(PATHNAME, &db) != SQLITE_OK) //不存在则创建
            {
                printf("Open sqlite error:%s\n", sqlite3_errmsg(db));
                return -1;
            }
            else
            {
                printf("Open sqlite success.\n");
            }       printf("access ok ");
            if (sqlite3_exec(db, sql_create, NULL, NULL, NULL)!= SQLITE_OK)
            {
                printf("Create table error:%s\n", sqlite3_errmsg(db));
                return -1;
            }
            else
            {
                printf("Create table success.\n");
            }
        }
    
        if (sqlite3_open(PATHNAME, &db) != SQLITE_OK)   //存在则直接打开,
        {
            printf("Open sqlite error:%s\n", sqlite3_errmsg(db));
            return -1;
        }
        else
        {
            printf("Open sqlite success.\n");
        }
        return 0;
    }
  2. 插入一条数据
    int InsertInto_db(sqlite3* db)
    {
        int     id;
        char    name[32] = {};
        int     score;
        char    sql_insert[128] = {};
        memset(sql_insert, 0, sizeof(sql_insert));
    
        puts("Please enter id num:");
        scanf("%d", &id);
        getchar();
        puts("Please enter name:");
        scanf("%s", name);
        getchar();
        puts("Please enter score:");
        scanf("%d", &score);
        getchar();
    
        sprintf(sql_insert, "insert into stu values(%d, '%s', %d);",id, name, score);
    
        if (sqlite3_exec(db, sql_insert, NULL, NULL, NULL)!= SQLITE_OK)
        {
            printf("Insert data into table error:%s\n", sqlite3_errmsg(db));
            return -1;
        }
        else
        {
            printf("Insernt data to table success.\n");
        }
    
        return 0;
    }
    
  3. 查询数据(需要使用回调函数来处理查询到的数据)
    int QueryFrom_db(sqlite3* db)
    {
        char del_sql[128] = {};
        snprintf(del_sql, 128, "select * from stu");
        if (sqlite3_exec(db, del_sql, callback, NULL, NULL)!= SQLITE_OK)
        {
            printf("Delete data from table error:%s\n", sqlite3_errmsg(db));
            return -1;
        }
        else
        {
            printf("Query  data from table success.\n");
        }
    
        return 0;
    }
    
    int callback(void* para, int f_num, char ** f_value,char** f_name) //回调函数来打印查询到的数据
    {
        int  i =0;
        for (i = 0; i < f_num; ++i)
        {
            printf("%-11s", f_value[i]);
        }
        putchar(10);
    
        return 0;
    
    }
  4. 更新一条数据
    int UpdateInto_db(sqlite3* db)
    {
        char del_sql[128] = {};
        int           id  = 0;
        int        score  = 0;
        puts("Enter the id you want Update:");
        scanf("%d", &id);
        getchar();
        puts("Enter the data you want Update:");
        scanf("%d", &score);
        getchar();
        snprintf(del_sql, 128, "update stu set score = %d where id = %d",score, id);
        if (sqlite3_exec(db, del_sql, NULL, NULL, NULL)!= SQLITE_OK)
        {
            printf("Delete data from table error:%s\n", sqlite3_errmsg(db));
            return -1;
        }
        else
        {
            printf("Update data from table success.\n");
        }
    
        return 0;
    }
  5. 删除一条数据
    int DeleteFrom_db(sqlite3* db)
    {
        char del_sql[128] = {};
        int           id  = 0;
        puts("Enter the id you want delete:");
        scanf("%d", &id);
        snprintf(del_sql, 128, "delete from stu where id = %d",id);
        if (sqlite3_exec(db, del_sql, NULL, NULL, NULL)!= SQLITE_OK)
        {
            printf("Delete data from table error:%s\n", sqlite3_errmsg(db));
            return -1;
        }
        else
        {
            printf("Delete data from table success.\n");
        }
    
        return 0;
    }
  • 编程示例 

#include <stdio.h>
#include <string.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <unistd.h>
#define  HEAD   0x5A
#define  TAG    0xfd
#define  LENGTH 0x03 

#define  PATHNAME  "/tmp/stu.db"  //设置数据库的文件路经

int InsertInto_db( sqlite3* db);
int DeleteFrom_db( sqlite3* db);
int QueryFrom_db(  sqlite3* db);
int UpdateInto_db( sqlite3* db);
int callback(void* para, int f_num, char ** f_value,char** f_name);

int sqlite_create(char *pthaname)
{
    sqlite3*          db;
    const char*       sql_create = "create table stu (id integer, name char, score integer)";
    int               cmd;

    if (access(PATHNAME, F_OK) != 0) 
    {

        if (sqlite3_open(PATHNAME, &db) != SQLITE_OK)
        {
            printf("Open sqlite error:%s\n", sqlite3_errmsg(db));
            return -1;
        }
        else 
        {
            printf("Open sqlite success.\n");
        }       printf("access ok ");
        if (sqlite3_exec(db, sql_create, NULL, NULL, NULL)!= SQLITE_OK)
        {
            printf("Create table error:%s\n", sqlite3_errmsg(db));
            return -1;
        }
        else 
        {
            printf("Create table success.\n");
        }


    }

    if (sqlite3_open(PATHNAME, &db) != SQLITE_OK)
    {
        printf("Open sqlite error:%s\n", sqlite3_errmsg(db));
        return -1;
    }
    else 
    {
        printf("Open sqlite success.\n");
    }
    while (1) 
    {
        puts("**************************************");
        printf("1: insert data, 2:delete data, 3: query data, 4: update data, 5: qiut. \n");
        puts("**************************************");
        puts("Enter cmd(1,2...)");
        scanf("%d", &cmd);

        switch (cmd) 
        {
            case 1:
                InsertInto_db(db);
                break;
            case 2:
                DeleteFrom_db(db);
                break; 
            case 3:
                QueryFrom_db(db);
                break;
            case 4:
                UpdateInto_db(db);
                break;
            case 5:
                sqlite3_close(db);
                exit(0);
            default:
                puts("Error cmd.");

        }

    }


}


int InsertInto_db(sqlite3* db)
{
    int     id;
    char    name[32] = {};
    int     score;
    char    sql_insert[128] = {};
    memset(sql_insert, 0, sizeof(sql_insert));

    puts("Please enter id num:");
    scanf("%d", &id);
    getchar();
    puts("Please enter name:");
    scanf("%s", name);
    getchar();
    puts("Please enter score:");
    scanf("%d", &score);
    getchar();

    sprintf(sql_insert, "insert into stu values(%d, '%s', %d);",id, name, score);

    if (sqlite3_exec(db, sql_insert, NULL, NULL, NULL)!= SQLITE_OK)
    {
        printf("Insert data into table error:%s\n", sqlite3_errmsg(db));
        return -1;
    }
    else
    {
        printf("Insernt data to table success.\n");
    }

    return 0;
}

int DeleteFrom_db(sqlite3* db)
{
    char del_sql[128] = {};
    int           id  = 0;
    puts("Enter the id you want delete:");
    scanf("%d", &id);
    snprintf(del_sql, 128, "delete from stu where id = %d",id);
    if (sqlite3_exec(db, del_sql, NULL, NULL, NULL)!= SQLITE_OK)
    {
        printf("Delete data from table error:%s\n", sqlite3_errmsg(db));
        return -1;
    }       
    else
    {           
        printf("Delete data from table success.\n");       
    }  

    return 0;
}
int QueryFrom_db(sqlite3* db)
{
    char del_sql[128] = {};
    snprintf(del_sql, 128, "select * from stu");
    if (sqlite3_exec(db, del_sql, callback, NULL, NULL)!= SQLITE_OK)
    {
        printf("Delete data from table error:%s\n", sqlite3_errmsg(db));
        return -1;
    }
    else
    {
        printf("Query  data from table success.\n");
    }

    return 0;
}
int callback(void* para, int f_num, char ** f_value,char** f_name)
{
    int  i =0;
    for (i = 0; i < f_num; ++i) 
    {
        printf("%-11s", f_value[i]);
    }
    putchar(10);

    return 0;

}
int UpdateInto_db(sqlite3* db)
{
    char del_sql[128] = {};
    int           id  = 0;
    int        score  = 0;  
    puts("Enter the id you want Update:");
    scanf("%d", &id);
    getchar();
    puts("Enter the data you want Update:");
    scanf("%d", &score);
    getchar();
    snprintf(del_sql, 128, "update stu set score = %d where id = %d",score, id);
    if (sqlite3_exec(db, del_sql, NULL, NULL, NULL)!= SQLITE_OK)
    {
        printf("Delete data from table error:%s\n", sqlite3_errmsg(db));
        return -1;
    }
    else
    {
        printf("Update data from table success.\n");
    }

    return 0;
}

void test()
{
    sqlite_create(PATHNAME);
}
int main(int argc, char *argv[])
{
    test();

    return 0;
}
  • 测试实例

  1. 插入数据

  2. 查询数据

    3.删除数据

4.修改数据

发布了47 篇原创文章 · 获赞 37 · 访问量 3679

猜你喜欢

转载自blog.csdn.net/qq_44045338/article/details/105500637
今日推荐