sqlite3数据库使用


sqlite数据库介绍就不用多说。不知道的直接找度娘。


直接介绍数据库的安装和使用了。


在ubuntu上安装sqlite3

先去下载安装包:http://download.csdn.net/detail/hudan2714/4438781


里面有三个.deb的安装包,和一个文档。

把安装包拷贝到;inux下,使用:sudo dpkg -i *.deb安装三个包。


然后再terminate输入:sqlite3 xx.db就可以创建库。并且可以创建表了。

常用命令有:

<1>在终端下运行sqlite3 <*.db>,出现如下提示


<*.db>是要打开的数据库文件。若该文件不存在,则自动创建。

<2>显示所有命令
sqlite> .help

sqlite>.quit

<4>显示当前打开的数据库文件
sqlite>.database

<5>显示数据库中所有表名
sqlite>.tables
<6>查看表的结构
sqlite>.schema <table_name>
注意:这些命令都是以 " . "开头的。

注意:每条语句都必须以";"结尾。

<1>创建新表
sqlite>create table <table_name> (f1 type1,f2 type2,…);
例如:
create table people(id,name,age);

<2>删除
sqlite>drop table <table_name>
例如:
drop table people;

<3>向表中添加新记录
sqlite>insert into <table_name> values (value1,value2,…);
例如:
insert into people values(1,'A',10);
insert into people values(2,'B',13);
insert into people values(3,'C',9);
insert into people values(4,15);
insert into people values(5,NULL,NULL);
注意: 字符串要用单引号括起来。

<4>查询表中所有记录
sqlite>select * from <table_name>;
例如 :
select * from people;

<4>按指定条件查询表中记录
sqlite>select * from <table_name> where <expression>;
例如:
在表中搜索名字是A的项所有信息
select * from people where name='A';
注意:(来自网络)
今天在做数据库升级时,碰到要对原来数据库中一张表的一个字段名进行修改,但是用:
alter table tablename rename column oldColumnName to newColumnName;

始终不成功,后面查阅相关信息:
sqlite supports a limited subset of ALTER TABLE. The ALTER TABLE command in sqlite allows the user to rename a table or to add a new column to an existing table. It is not possible to rename a column,remove a column,or add or remove constraints from a table.

sqlite支持一个更改表内容的有限子集,就是说在sqlite更改表的命令中,只允许用户重命名表名或者增加一个列到一个的表中。而重命名一个字段名和删除一个字段、或者增加删除系统规定的参数这些操作是不可能的。

解决办法:

例如:在上面的操作过程中,我们在people表中新添加一个字段addr,要删除这个字段,直接用sqlite的语句时无法完成的。
我们可以这样干:
A.将people表重命名为temp;
B.重新创建people表;
C.将temp表中的相应字段内容复制到people表中。
D.删除temp表

操作如下:
A.alter table people rename to temp;
B.create table people(id,age);
C.insert into people select id,age from temp;

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能