Linux下c编程sqlite3的基本语句

目录

 

Ubuntu下安装sqlite3

sqlite3的基本系统命令

sqlite3基本命令 

使用sqlite3查看已经创建的表

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

编程示例 

测试实例


  • Ubuntu下安装sqlite3

  1. 命令行安装
    sudo apt-get install sqlite3

     

  2. 检查是否安装成功
    //在shell界面下输入
     
    sqlite3

 

  • 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.修改数据

 

 

 

 

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XiaoCheng'Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值