Sqlite3数据库

来源:互联网 发布:淘宝扩容128g u盘判断 编辑:程序博客网 时间:2024/06/05 20:25

1.常用语句

终端使用:①进入sqlite3。         输入sqlite3 info.dbinfo.db为数据文件)  一旦进入所有执行语句后‘;’为结束 


 ②添加table即表        create table stu(id integer primary key, name text, age integer);

                                              上述解释:表名字stu      三个信息,id、name、age   分别是int、text(即char)、int    其中primary key为主键,不可有重复,即由id1则不可以再出现id1,否则会报错;


③添加信息                insert into stu (id, name, age)values(1, 'huang', 14)   ; 

 注意字符串不可无单引号      中间字段名需要与后面填写值的相对应


④更改                       update stu set name = 'Jim' where name = 'huang' ;

                                                  更新stu中将huang改成Jim


                 ⑤列出数据                select * from stu  order by id asc/desc;     根据id升序/降序列出全部

                                                  select * from stu  where id in (2,3,4);   列出id为2,3,4的全部

                                                                   select * from stu  where id between 2 to 4;   列出id为2到4区间中全部      

                                                  select * from stu  where name like  '_m%'; 列出名字中第二个字母为m的全部  

                                                                                     _为一个字母,而%为统配,即任意个

⑥删除一行数据          delete from stu  +如select后的条件;

⑦删除table表            drop table 'stu'; 

⑧退出                        .quit确定

 

2.使用c语言编写运用数据库的时候,举一个例子,大部分运用都是通用的,

char str[20] = {0};

char sql[1024] = {0};
    sprintf(sql, "delete from contact where tel = '%s';", idc);  
 //效果是删除,即将原程序用句运用sqlite3_exec再运行到数据库中
    ret = sqlite3_exec(db, sql, NULL, NULL, &errmsg);   //db即数据文件指针,sql为载入字符串,  errmsg为错误信息存放


*当需要在c语言中实现将数据显示出来时,需要加上以下

char *result[];

int row, column;

char sql[1024] = {0};

    sprintf(sql, "select * from contact where id = %d;", id);

ret = sqlite3_get_table(db, sql, &result, &row, &column, &errmsg);
    int i, j;
    for (i = 0; i <= row; i++)
    {
        for (j = 0; j < column; j++)
        {
            printf("%s|", result[i * column + j]);
        }
        printf("\n");
    }


 









遇到问题:1.编译时提示没有库, 

answer:在编译gcc  1.c  -o 1 后添加  -lsqlite3。