android sqlite3 常用命令

来源:互联网 发布:百锐腾软件 编辑:程序博客网 时间:2024/05/23 11:32

$ adb shell


$ cd /data/data/com.android.providers.media/databases //进入
media数据库目录


$ sqlite3 settings.db //启动sqlite3
SQLite version 3.6.22
Enter ".help" for instructions //输入.help获取帮助信息
Enter SQL statements terminated with a ";" //SQL命令以;作为结束符

sqlite> .tables //查看当前的table

android_metadata bookmarks system 
bluetooth_devices secure


sqlite> .headers ON //显示表头信息


sqlite> select * from secure; //查看secure表中的值,命令以;结束

sqlite> select * from audio_genres;
_id|name //只有
 .headers ON打开后才有表头显示
1|Unknown
2|Other
3|Rock
4|Soul And R&B
5|pop
6|Soundtrack
7|Goa
8|Pop
9|华语流行音乐【Chinese Pop Music】
10|POP
11|148
12|Blues
13|genre
14|Asian
15|www.ting30.com
16|General Rock
17|Folk
18|General Soundtrack


sqlite> INSERT INTO secure (name, value) VALUES (‘device_provisioned’, 1); //向secure 表中插入device_provisioned,其值设置为1

sqlite> .exit //退出


附录: SQL常用命令


(1) 数据记录筛选:

sql="select * from 数据表 where 字段名=字段值 order by 字段名 [desc]"

sql="select * from 数据表 where 字段名 like '%字段值%' order by 字段名 [desc]"

sql="select top 10 * from 数据表 where 字段名 order by 字段名 [desc]"

sql="select * from 数据表 where 字段名 in ('值1','值2','值3')"

sql="select * from 数据表 where 字段名 between 值1 and 值2"

(2) 更新数据记录:

sql="update 数据表 set 字段名=字段值 where 条件表达式"

sql="update 数据表 set 字段1=值1,字段2=值2 …… 字段n=值n where 条件表达式"

(3) 删除数据记录:

sql="delete from 数据表 where 条件表达式"

sql="delete from 数据表" (将数据表所有记录删除)

(4) 添加数据记录:

sql="insert into 数据表 (字段1,字段2,字段3 …) valuess (值1,值2,值3 …)"

sql="insert into 目标数据表 select * from 源数据表" (把源数据表的记录添加到目标数据表)

(5) 数据记录统计函数:

AVG(字段名) 得出一个表格栏平均值
COUNT(*|字段名) 对数据行数的统计或对某一栏有值的数据行数统计
MAX(字段名) 取得一个表格栏最大的值
MIN(字段名) 取得一个表格栏最小的值
SUM(字段名) 把数据栏的值相加

引用以上函数的方法:

sql="select sum(字段名) as 别名 from 数据表 where 条件表达式"
set rs=conn.excute(sql)

用 rs("别名") 获取统的计值,其它函数运用同上。

(6) 数据表的建立和删除:

CREATE TABLE 数据表名称(字段1 类型1(长度),字段2 类型2(长度) …… )

例:CREATE TABLE tab01(name varchar(50),datetime default now())

DROP TABLE 数据表名称 (永久性删除一个数据表)



另外一些常用
 
一,SQLite常见的数据类型
SQLite是无类型的。 这意味着你可以保存任何类型的数据到你所想要保存的任何表的任何列中,无论这列声明的数据类型是什么(只有自动递增Integer Primary Key才有用)。对于SQLite来说对字段不指定类型是完全有效的。 即使SQLite允许忽略数据类型,但是仍然建议在你的Create Table语句中指定数据类型。 因为数据类型对于你和其他的程序员交
流,或者你准备换掉你的数据库引擎是非常有用的。SQLite只支持常见的5种存储类,
   NULL
   INTEGER  --整型
   REAL      --浮点数
   TEXT      --文本
   BLOB      --大二进制对象
以下定义的数据类型都会转到相应的存储类中。
create  table  tab(            --注意其中的注释方式 
  a  VARCHAR(10),          --长度不固定且其最大长度为n的字符串
  b  NVARCHAR(15),
c  TEXT,                  --二进制对象
d  INTEGER,              --带符号的整型,具体取决于存入数字的范围大小
e  FLOAT,            
f  BOOLEAN,   
g  CLOB,                 --使用CHAR来保存数据
      h  BLOB,                 --使用二进制对象保存数据,如保存位图
      i  TIMESTAMP,
      j  NUMBERIC(10,5),
      k  VARYING CHARACTER(24),
      l  NATIONAL VARYING CHARACTER(16),    //
      j  REAL                  --浮点数字,存储为8-byte IEEE浮点数
);
二,        基本的数据操作
1,建立表
Create  table  admin(
username text,
age integer);
2,插入数据
    insert into 表名(字段列表) values(值列表);
    例如:insert  into admin values(‘song’,25);
3,查询
    select 字段名 from 表名;
    select * from admin;
    select  distinct  field  from  table_name;(distinct去掉重复项,将列中各字段值单个列出)
    
4,删除数据
    Delete from 表名 where 条件子句。
    delete from admin form where username=’song’;
5,修改
    update 表名 set 字段名=值 where 条件子句。
    update admin set username=’zhang’,age=24 where username=’song’ and age=25;
6,按条件分组
   select * from 表名 where 条件子句 group by 分组子句 having …order by排子句
    例如:
       select * from admin;
       select * from admin order by id desc(降序) | asc(升序);
       select username from admin group by username having count(*)>1;
7,多条件查询语句
    select 字段名 from 表名 where  子句1 按 子句二
    select * from admin where username=’song’ and age=24;
    select * from table_name where field in (‘val1’ , ’val2’ , ‘val3’ );
    select * from table_name where field between  val1 and val2;
    select * from admin limit 5;        --限制输出数据记录数量
8,多条件排序
   select 字段名 from 表名 order by 字段1 (desc),字段2(desc);
   select * from admin order by t1 ,t2 desc;
9,索引
    例如 建立复合索引:create index  idxT1  on admin(username,age);
         各自建立索引:create index  idxUsername on  admin(username);
                       create index  idxAge on admin(age);
10,外键FOREIGN KEY(UNIQUE | PRIMARY KEY | NOT NULL)的用法()
     create table a(
a1 INTEGER  PRIMARY KEY | UNIQUE | NOT NULL,
a2 TEXT,
a3 INTEGER );
     create table b()(
b1 INTEGER ,
b2 TEXT,
b3 INTEGER,
foreign key(b3) references a(a1));
11,分页
     select * from account limit 5 offset 3; 
     或者  select * from account limit 5,3;
12,模糊查询
      SELECT 字段 FROM 表 WHERE 某字段 LIKE 条件
     (1)%:表示任意0个或多个字符
     (2)_ :表示任意单个字符,匹配单个任意字符,常用来限制表达式的字符长度语句。
     (3)[ ]:表示括号内所列字符中的一个(类似正则表达式)
           select * from admin where username like ‘[张李王]三’;
           表示搜索的是“张三”,“李三”或“王三”
       [4]:[^]表示不在括号所列之类的单个字符。
       [5]:查询内容包含通配符时,用“[ ]”括起来。
13,删除表 | 索引
     drop table [ IF EXISTS] admin;
     drop index index_name
14,查询记录数目
     select count(*) from table_name;

0 0
原创粉丝点击