android SQLite数据库用法图文详解(附源码)
SQLite是嵌入式设备使用的一种轻量型数据库。可以通过执行sql语句对数据库进行操作,android也为其提供了完善的函数接口支持。下面就来实现一个简单的数据库,可以实现查询以及插入等功能。
例图如下:
1-1 数据库app
如果你的手机已经root过,将手机连到pc端eclipse我们可以在DDMS中查看这个数据库文件。路径是data/data/app的包/databases就可一看到自己命名的*.db格式的数据库文件了
1-2 DDMS截图
要想在pc端查看这个数据库文件的内容,我们需要先将这个.db文件copy到电脑上,我是用的是linux系统,可以使用系统自带的sqlite3来查看数据库文件。
进入shell,执行下面命令,进入数据库
- sqlite3 /home/CORPUSERS/28852262/Desktop/sec_db
1-3 进入数据库
以下为sqlite 在linux下的使用方法
1、安装sqlite3
ubuntu下安装sqlite3直接在终端运行命令:
#apt-get install sqlite3
查看版本信息:
#sqlite3 -version
2 、sqlite3常用命令
当前目录下建立或打开test.db数据库文件,并进入sqlite命令终端,以sqlite>前缀标识:
#sqlite3 test.db
查看数据库文件信息命令(注意命令前带字符‘.‘):
sqlite>.database
查看所有表的创建语句:
sqlite>.schema
查看指定表的创建语句:
sqlite>.schema table_name
以sql语句的形式列出表内容:
sqlite>.dump table_name
设置显示信息的分隔符:
sqlite>.separator symble
Example:设置显示信息以‘:’分隔
sqlite>.separator :
设置显示模式:
sqlite>.mode mode_name
Example:默认为list,设置为column,其他模式可通过.help查看mode相关内容
sqlite>.mode column
输出帮助信息:
sqlite>.help
设置每一列的显示宽度:
sqlite>.width width_value
Example:设置宽度为2
sqlite>.width 2
列出当前显示格式的配置:
sqlite>.show
退出sqlite终端命令:
sqlite>.quit
或
sqlite>.exit
3、sqlite3指令
sql的指令格式:所有sql指令都是以分号(;)结尾,两个减号(--)则表示注释。
如:
sqlite>create studen_table(Stu_no interger PRIMARY KEY, Name text NOT NULL, Id interger UNIQUE, Age interger CHECK(Age>6), School text DEFAULT ‘xx小学);
该语句创建一个记录学生信息的数据表。
3.1 sqlite3存储数据的类型
NULL:标识一个NULL值
INTERGER:整数类型
REAL:浮点数
TEXT:字符串
BLOB:二进制数
3.2 sqlite3存储数据的约束条件
Sqlite常用约束条件如下:
PRIMARY KEY - 主键:
1)主键的值必须唯一,用于标识每一条记录,如学生的学号
2)主键同时也是一个索引,通过主键查找记录速度较快
3)主键如果是整数类型,该列的值可以自动增长
NOT NULL - 非空:
约束列记录不能为空,否则报错
UNIQUE - 唯一:
除主键外,约束其他列的数据的值唯一
CHECK - 条件检查:
约束该列的值必须符合条件才可存入
DEFAULT - 默认值:
列数据中的值基本都是一样的,这样的字段列可设为默认值
3.3 sqlite3常用指令
1)建立数据表
create table table_name(field1 type1, field2 type1, ...);
table_name是要创建数据表名称,fieldx是数据表内字段名称,typex则是字段类型。
例,建立一个简单的学生信息表,它包含学号与姓名等学生信息:
create table student_info(stu_no interger primary key, name text);
2)添加数据记录
insert into table_name(field1, field2, ...) values(val1, val2, ...);
valx为需要存入字段的值。
例,往学生信息表添加数据:
Insert into student_info(stu_no, name) values(0001, alex);
3)修改数据记录
update table_name set field1=val1, field2=val2 where expression;
where是sql语句中用于条件判断的命令,expression为判断表达式
例,修改学生信息表学号为0001的数据记录:
update student_info set stu_no=0001, name=hence where stu_no=0001;
4)删除数据记录
delete from table_name [where expression];
不加判断条件则清空表所有数据记录。
例,删除学生信息表学号为0001的数据记录:
delete from student_info where stu_no=0001;
5)查询数据记录
select指令基本格式:
select columns from table_name [where expression];
a查询输出所有数据记录
select * from table_name;
b限制输出数据记录数量
select * from table_name limit val;
c升序输出数据记录
select * from table_name order by field asc;
d降序输出数据记录
select * from table_name order by field desc;
e条件查询
select * from table_name where expression;
select * from table_name where field in (‘val1‘, ‘val2‘, ‘val3‘);
select * from table_name where field between val1 and val2;
f查询记录数目
select count (*) from table_name;
g区分列数据
select distinct field from table_name;
有一些字段的值可能会重复出现,distinct去掉重复项,将列中各字段值单个列出。
6)建立索引
当说数据表存在大量记录,索引有助于加快查找数据表速度。
create index index_name on table_name(field);
例,针对学生表stu_no字段,建立一个索引:
create index student_index on student_table(stu_no);
建立完成后,sqlite3在对该字段查询时,会自动使用该索引。
7)删除数据表或索引
drop table table_name;
drop index index_name;
输入相应命令我们看到了对数据库的所有操作。
1-4 数据库操作
2.代码
代码分两部分,第一部分dbHelper,继承android提供的类SQLiteOpenHelper用来管理数据库。
第一部分
这个类的基本架构如下所示,包括建表,更新,命名等。此外我们如果要对数据库增添插入,查询,修改等功能的时候,也要写在这个类里。
- public class dbhelp extends SQLiteOpenHelper{
- public dbhelp(Context context, String name, CursorFactory factory, int version) {
- super(context, name, factory, version);
- // TODO Auto-generated constructor stub
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- // TODO Auto-generated method stub
- }
- }
第二部分
这个就是数据库的调用,首先生成一个dbHelper对象,因为我们要将数据库内容显示在listivew里,我们要建立一 个curse来连接数据库和适配器adapter,最后用listview输出这个adapter即可。
- db=new dbHelper(MainActivity.this);
- myCursor=db.select();
- SimpleCursorAdapter adpater=new SimpleCursorAdapter(this
- , R.layout.test, myCursor,
- new String[]{dbHelper.FIELD_TITLE},
- new int[]{R.id.topTextView});
- lv.setAdapter(adpater);
- http://www.huihui.cn/share/43951082
http://www.huihui.cn/share/43951058
http://www.huihui.cn/share/43951140
http://www.huihui.cn/share/43951980
http://www.huihui.cn/share/43951882
http://www.huihui.cn/share/43951926
http://www.huihui.cn/share/43952638
http://www.huihui.cn/share/43952672
http://www.huihui.cn/share/43952698
http://www.huihui.cn/share/43955448
http://www.huihui.cn/share/43955514
http://www.huihui.cn/share/43955536
http://www.huihui.cn/share/43956154
http://www.huihui.cn/share/43956196
http://www.huihui.cn/share/43956324
http://www.huihui.cn/share/43956858
http://www.huihui.cn/share/43956878
http://www.huihui.cn/share/43956922
http://www.huihui.cn/share/43957724
http://www.huihui.cn/share/43957784
http://www.huihui.cn/share/43957808
http://www.huihui.cn/share/43958346
http://www.huihui.cn/share/43958370
http://www.huihui.cn/share/43958410
http://www.huihui.cn/share/43958892
http://www.huihui.cn/share/43958930
http://www.huihui.cn/share/43958966
http://www.huihui.cn/share/43960412
http://www.huihui.cn/share/43960442
http://www.huihui.cn/share/43960474
http://www.huihui.cn/share/43961416
http://www.huihui.cn/share/43961468
http://www.huihui.cn/share/43961448
http://www.huihui.cn/share/43962146
http://www.huihui.cn/share/43962192
http://www.huihui.cn/share/43962224
http://www.huihui.cn/share/43962868
http://www.huihui.cn/share/43962886
http://www.huihui.cn/share/43962912
http://www.huihui.cn/share/43963426
http://www.huihui.cn/share/43963460
http://www.huihui.cn/share/43963508
http://www.huihui.cn/share/43964034
http://www.huihui.cn/share/43964056
http://www.huihui.cn/share/43964082
http://www.huihui.cn/share/43965114
http://www.huihui.cn/share/43965072
http://www.huihui.cn/share/43965030
http://www.huihui.cn/share/43965756
http://www.huihui.cn/share/43965792
http://www.huihui.cn/share/43965854
http://www.huihui.cn/share/43966476
http://www.huihui.cn/share/43966502
http://www.huihui.cn/share/43966548
http://www.huihui.cn/share/43967364
http://www.huihui.cn/share/43967400
http://www.huihui.cn/share/43967432
http://www.huihui.cn/share/43967924
http://www.huihui.cn/share/43967986
http://www.huihui.cn/share/43967958
http://www.huihui.cn/share/43968326
http://www.huihui.cn/share/43968376
http://www.huihui.cn/share/43968402
http://www.huihui.cn/share/43968750
http://www.huihui.cn/share/43968866
http://www.huihui.cn/share/43968920
http://www.huihui.cn/share/43969974
http://www.huihui.cn/share/43969996
http://www.huihui.cn/share/43970036
http://www.huihui.cn/share/43970514
http://www.huihui.cn/share/43970544
http://www.huihui.cn/share/43970580
http://www.huihui.cn/share/43971076
http://www.huihui.cn/share/43971112
http://www.huihui.cn/share/43971102
http://www.huihui.cn/share/43972358
http://www.huihui.cn/share/43972438
http://www.huihui.cn/share/43972938
http://www.huihui.cn/share/43972976
http://www.huihui.cn/share/43973006
http://www.huihui.cn/share/43973374
http://www.huihui.cn/share/43973400
http://www.huihui.cn/share/43973458
http://www.huihui.cn/share/43974002
http://www.huihui.cn/share/43974030
http://www.huihui.cn/share/43972406
http://www.huihui.cn/share/43974048
http://www.huihui.cn/share/43974002
http://www.huihui.cn/share/43976096
http://www.huihui.cn/share/43976124
http://www.huihui.cn/share/43976158
http://www.huihui.cn/share/43976754
http://www.huihui.cn/share/43976728
http://www.huihui.cn/share/43976702
http://www.huihui.cn/share/43990136
http://www.huihui.cn/share/43990162
http://www.huihui.cn/share/43990182
http://www.huihui.cn/share/43990914
http://www.huihui.cn/share/43990944
http://www.huihui.cn/share/43990976
http://www.huihui.cn/share/43991504
http://www.huihui.cn/share/43991492
http://www.huihui.cn/share/43991466
http://www.huihui.cn/share/43992734
http://www.huihui.cn/share/43992772
http://www.huihui.cn/share/43992814
http://www.huihui.cn/share/43993332
http://www.huihui.cn/share/43993358
http://www.huihui.cn/share/43993398
http://www.huihui.cn/share/43993968
http://www.huihui.cn/share/43993998
http://www.huihui.cn/share/43994040
http://www.huihui.cn/share/43994668
http://www.huihui.cn/share/43994812
http://www.huihui.cn/share/43994908
http://www.huihui.cn/share/43997132
http://www.huihui.cn/share/43997194
http://www.huihui.cn/share/43997168
http://www.huihui.cn/share/43997782
http://www.huihui.cn/share/43997804
http://www.huihui.cn/share/43997816
http://www.huihui.cn/share/43998274
http://www.huihui.cn/share/43998316
http://www.huihui.cn/share/43998290
http://www.huihui.cn/share/44000486
http://www.huihui.cn/share/44000628
http://www.huihui.cn/share/44000740
http://www.huihui.cn/share/44002186
http://www.huihui.cn/share/44002212
http://www.huihui.cn/share/44002238
http://www.huihui.cn/share/44002866
http://www.huihui.cn/share/44002894
http://www.huihui.cn/share/44002954
http://www.huihui.cn/share/44003548
http://www.huihui.cn/share/44003540
http://www.huihui.cn/share/44003520
http://www.huihui.cn/share/44005478
http://www.huihui.cn/share/44005512
http://www.huihui.cn/share/44005560
http://www.huihui.cn/share/44006256
http://www.huihui.cn/share/44006280
http://www.huihui.cn/share/44006294
http://www.huihui.cn/share/44006882
http://www.huihui.cn/share/44006904
http://www.huihui.cn/share/44006928
http://www.huihui.cn/share/44008940
http://www.huihui.cn/share/44008976
http://www.huihui.cn/share/44009000
http://www.huihui.cn/share/44009814
http://www.huihui.cn/share/44009818
http://www.huihui.cn/share/44009816
http://www.huihui.cn/share/44010560
http://www.huihui.cn/share/44010590
http://www.huihui.cn/share/44010602
http://www.huihui.cn/share/44011658
http://www.huihui.cn/share/44011690
http://www.huihui.cn/share/44011758
http://www.huihui.cn/share/44012394
http://www.huihui.cn/share/44012430
http://www.huihui.cn/share/44012460
http://www.huihui.cn/share/44013100
http://www.huihui.cn/share/44013116
http://www.huihui.cn/share/44013138
http://www.huihui.cn/share/44013810
http://www.huihui.cn/share/44013932
http://www.huihui.cn/share/44014044
http://www.huihui.cn/share/44015118
http://www.huihui.cn/share/44015154
http://www.huihui.cn/share/44015200
http://www.huihui.cn/share/44015852
http://www.huihui.cn/share/44015888
http://www.huihui.cn/share/44015938
http://www.huihui.cn/share/44016546
http://www.huihui.cn/share/44016570
http://www.huihui.cn/share/44016624
http://www.huihui.cn/share/44017256
http://www.huihui.cn/share/44017394
http://www.huihui.cn/share/44017488
http://www.huihui.cn/share/44036414
http://www.huihui.cn/share/44036496
http://www.huihui.cn/share/44036802
http://www.huihui.cn/share/44039228
http://www.huihui.cn/share/44039272
http://www.huihui.cn/share/44039366
http://www.huihui.cn/share/44040006
http://www.huihui.cn/share/44040072
http://www.huihui.cn/share/44040168
http://www.huihui.cn/share/44041516
http://www.huihui.cn/share/44041562
http://www.huihui.cn/share/44041618
http://www.huihui.cn/share/44042270
http://www.huihui.cn/share/44042306
http://www.huihui.cn/share/44042328
http://www.huihui.cn/share/44043028
http://www.huihui.cn/share/44043086
http://www.huihui.cn/share/44043150
http://www.huihui.cn/share/44044130
http://www.huihui.cn/share/44044164
http://www.huihui.cn/share/44044216
http://www.huihui.cn/share/44044898
http://www.huihui.cn/share/44044942
http://www.huihui.cn/share/44045000
http://www.huihui.cn/share/44045676
http://www.huihui.cn/share/44045700
http://www.huihui.cn/share/44045732
http://www.huihui.cn/share/44046276
http://www.huihui.cn/share/44046432
http://www.huihui.cn/share/44046510\
http://www.huihui.cn/share/44048168
http://www.huihui.cn/share/44048208
http://www.huihui.cn/share/44048268
http://www.huihui.cn/share/44048986
http://www.huihui.cn/share/44049008
http://www.huihui.cn/share/44049066
http://www.huihui.cn/share/44049796
http://www.huihui.cn/share/44049822
http://www.huihui.cn/share/44049852
http://www.huihui.cn/share/44056112
http://www.huihui.cn/share/44056172
http://www.huihui.cn/share/44056188‘
http://www.huihui.cn/share/44056920
http://www.huihui.cn/share/44056978
http://www.huihui.cn/share/44057044
http://www.huihui.cn/share/44057782
http://www.huihui.cn/share/44057846
http://www.huihui.cn/share/44057908
http://www.huihui.cn/share/44060600
http://www.huihui.cn/share/44060656
http://www.huihui.cn/share/44061548
http://www.huihui.cn/share/44061502
http://www.huihui.cn/share/44061452
http://www.huihui.cn/share/44064082
http://www.huihui.cn/share/44064122
http://www.huihui.cn/share/44064154
http://www.huihui.cn/share/44060710
http://www.huihui.cn/share/44083064
http://www.huihui.cn/share/44083136
http://www.huihui.cn/share/44083214
http://www.huihui.cn/share/44085208
http://www.huihui.cn/share/44085238
http://www.huihui.cn/share/44085310
http://www.huihui.cn/share/44086094
http://www.huihui.cn/share/44086118
http://www.huihui.cn/share/44086146
http://www.huihui.cn/share/44100392
http://www.huihui.cn/share/44100428
http://www.huihui.cn/share/44100464
http://www.huihui.cn/share/44101450
http://www.huihui.cn/share/44101424
http://www.huihui.cn/share/44101386
http://www.huihui.cn/share/44105060
http://www.huihui.cn/share/44105120
http://www.huihui.cn/share/44105180
http://www.huihui.cn/share/44106476
http://www.huihui.cn/share/44106562
http://www.huihui.cn/share/44106612
http://www.huihui.cn/share/44107570
http://www.huihui.cn/share/44107630
http://www.huihui.cn/share/44107700
http://www.huihui.cn/share/44108648
http://www.huihui.cn/share/44108724
http://www.huihui.cn/share/44108836
www.yishujiayuan.com
www.zimi68.com
yishujiayuan.com
zimi68.com
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。