sqlite3

来源:互联网 发布:swatch知乎 编辑:程序博客网 时间:2024/06/05 14:29

内存数据库

rc = sqlite3_open(":memory:", &db);

在调用完以上函数后,不会有任何磁盘文件被生成,取而代之的是,一个新的数据库在纯内存中被成功创建了。

临时数据库

rc = sqlite3_open("", &db);

磁盘文件被创建用于存储临时数据库中的数据信息,但是实际上临时数据库通常也会驻留在内存中,以提高增删查改的速度。如果不删除数据库里的数据,随着数据不断地添加到数据库,数据库越来越大,占用的内存也越来越大。 所以用过的数据应尽量删除。


建数据库:

linux@Ubuntu:~$ sqlite3 my.db    //没有则创建 有则打开
查看帮助:
sqlite> .help
文件存放位置:
sqlite> .database
退出:
sqlite> .quit
查看表:
sqlite> .tables
显示表的结构:
sqlite> .schema 
命令错误退出
 ...> ;
*******************************************************************

---------------语法------------------
1.建表:
sqlite> create table usr(id integer primary key, name text,age integer null, gender text, salary real not null);
注意:没有默认字段,加单引号则为字符串,其他为整型,如果输入字符串不加单引号,如:123abc,则数据库会报错;
2.删除表
sqlite> drop table usr;
3.增:
sqlite> insert into usr(id, name, age, salary) values(2, 'liu', 20, 6000);
4.删
sqlite> delete from usr where id = 2;
5.改:
sqlite> update usr set gender = 'man' where id = 3;
6.查:

(1):查找表中所有内容
sqlite> select * from usr ;
(2):按指定条件查找表中内容
1)在表中查找id=2项的所有信息
sqlite> select * from usr where id = 2;
2)在表中超找id 大于3,小于10的项的所有信息
sqlite> select * from usr where id>=2 and id<=10;
3)在表中查找id等于3的选项,并显示其中的name 和age
sqlite> select name,age from usr id=3;
4)显示表中的前两项信息
sqlite> select * from usr limit 2;
5)以年龄排序显示表中信息
sqlite> select * from usr order by age ;
7.添加字段
alter table usr add column addr; //alter table usr add column age;
注意:添加字段只能为字符串格式
8.更新表中的记录
sqlite> update usr name='farsight' where id=88 and age=23;
9.更改表中的字段名
sqlite3> alter table usr rename column oldColumnName to newColumnName;

备注:
1)alter table usr rename to temp;
2)create table usr(id,name,height);
3)insert into usr select id,name,age from temp;
10.删除字段 age
1)create table usr(id,name,height,age); //已经有的表
2)create table temp(id,name,height); //创建一个新的表 比已有的表少age字段
3)insert into temp select id,name,heiget from usr;//将usr表中的 id,name,height字段copy到temp表中
0 0