Android数据存储引擎---SQLite数据库
目标:是否可以在PC端桌面上使用SQLite数据库制作一个财务文件?
目录:
来源:
实践:
总结和比较:
SQLite数据简介
是什么,内部结构是怎样的,数据库和表的关系是什么
有什么用
常用的操作是什么
SQLite数据库使用
增
删
改
查
SQLite数据库实践上的优化措施
对于Android平台来说,系统内置了丰富的API来供开发人员操作SQLite,使我们轻松完成对数据的存取。
步骤1,熟悉创建数据库表,熟悉相关的操作指令,实现对SQLite数据库的感性认识
创建一个包含简单内容的数据库(数据库名)和对应的表(表名);创建表后,可执行简单的“增删改查”指令。
/**
* <功能描述> 初始化数据库,创建数据库
*
* @return void [返回类型说明]
*/
private void initDataBase() {
mDatabase = openOrCreateDatabase("SqlDemo.db", Context.MODE_PRIVATE,
null);
// CREATE_TABLE = "CREATE TABLE person (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age SMALLINT)";
mDatabase.execSQL(CREATE_TABLE);
}
创建数据库的操作调用了execSQL(...),参数代表的是创建数据库指令,其结果会在/data/data/packageName/databases/目录下生成对应的数据库文件,同时执行了在数据库中创建了person的数据表。
步骤2,在数据库中创建数据表,并执行相关的基本操作
/**
* <功能描述> 执行一些数据库和表的操作
*
* @return void [返回类型说明]
*/
private void doSomeAction() {
Person person = new Person("john", 30);
mDatabase.execSQL("INSERT INTO person VALUES (NULL, ?, ?)",
new Object[] {
person.getName(), person.getAge()
});
person.setName("David");
person.setAge(33);
ContentValues cv = new ContentValues();
cv.put("name", person.getName());
cv.put("age", person.getAge());
mDatabase.insert("person", null, cv);
cv = new ContentValues();
cv.put("age", 35);
mDatabase.update("person", cv, "name=?", new String[] {
"john"
});
Cursor cursor = mDatabase.rawQuery(
"SELECT * FROM person WHERE age >= ?", new String[] {
"33"
});
while (cursor != null && cursor.moveToFirst()) {
int _id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Log.d(TAG, "_id>=" + _id + ", name=>" + name + ", age=>" + age);
}
cursor.close();
mDatabase.delete("person", "age<?", new String[] {
"35"
});
mDatabase.close();
}
粗略地区分对数据库的操作,有以下两种:数据库操作、数据库中数据表的操作。
对于数据库中数据表的操作中,添加、删除和更新可以使用下述两种方式:
public void execSQL(String sql) throws SQLException;
public void execSQL(String sql, Object[] bindArgs) throws SQLException;
上述可以认为是一种统一的形式,执行的是传统数据库操作的指令。
此外,还有对应的可供区分的API方法:
public long insert(String table, String nullColumnHack, ContentValues values);
public int update(String table, ContentValues values, String whereClause, String[] whereArgs);
public int delete(String table, String whereClause, String[] whereArgs);
参数中的table都表示对应数据库中对应table;ContentValues实例values如下内容:
/** Holds the actual values */
private HashMap<String, Object> mValues;
其中Key表示的列名,Values表示的是待写入的内容。参数String whereClause表示WHERE表达式,而String[]则为上述对应占位符的实际参数值。
查询语句相对来说较为复杂,因为工程问题中经常会面对各种各样的查询问题,系统也考虑到了这种复杂性,为我们提供了较为丰富的查询形式:
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
public Cursor query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
public Cursor query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit, CancellationSignal cancellationSignal)
public Cursor queryWithFactory(CursorFactory cursorFactory, boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
public Cursor queryWithFactory(CursorFactory cursorFactory, boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit, CancellationSignal cancellationSignal)
此外还有如下四种粗略查询方式:
public Cursor rawQuery(String sql, String[] selectionArgs)
public Cursor rawQuery(String sql, String[] selectionArgs, CancellationSignal cancellationSignal)
public Cursor rawQueryWithFactory(CursorFactory cursorFactory, String sql, String[] selectionArgs, String editTable)
public Cursor rawQueryWithFactory(CursorFactory cursorFactory, String sql, String[] selectionArgs, String editTable, CancellationSignal cancellationSignal)
其中sql字符串,表示一个SQL语句,可以使用占位符代替实际值;而selectionArgs就是占位符的实际参数集合;columns表示要查询的列的所有名称集合;selection表示WHERE之后的条件语句,可以使用占位符。
上述API都会返回Cursor实例,代表数据集的游标。
SQL数据库的灵活使用,更多的是在查询语句中,包含更多的技巧和方法;因为这也是Android UI展示的内容来源。
Cursor的可用方法如下:
c.move(int offset); //以当前位置为参考,移动到指定行
c.moveToFirst(); //移动到第一行
c.moveToLast(); //移动到最后一行
c.moveToPosition(int position); //移动到指定行
c.moveToPrevious(); //移动到前一行
c.moveToNext(); //移动到下一行
c.isFirst(); //是否指向第一条
c.isLast(); //是否指向最后一条
c.isBeforeFirst(); //是否指向第一条之前
c.isAfterLast(); //是否指向最后一条之后
c.isNull(int columnIndex); //指定列是否为空(列基数为0)
c.isClosed(); //游标是否已关闭
c.getCount(); //返回总数据项数(行数)
c.getPosition(); //返回当前游标所指向的行数
c.getColumnIndex(String columnName);//返回某列名对应的列索引值
c.getString(int columnIndex); //返回当前行指定列的值
获取内容则是使用如下API:
byte[] getBlob(int columnIndex);
String getString(int columnIndex);
short getShort(int columnIndex);
int getInt(int columnIndex);
long getLong(int columnIndex);
float getFloat(int columnIndex);
double getDouble(int columnIndex);
步骤3:实际开发对上述操作做改进,以更方便开发流程。
在实际开发中,为了更好地管理和维护数据库,会封装一个继承自SQLiteOpenHelper类的数据库操作管理类,然后以这个类为基础再封装自己的业务逻辑。
A helper class to manage database creation and version management.
Android数据存储引擎---SQLite数据库的更多相关文章
- Android数据存储之SQLite数据库
Android数据存储 之SQLite数据库简介 SQLite的相关知识,并结合Java实现对SQLite数据库的操作. SQLite是D.Richard Hipp用C语言编写的开源嵌入式数据库引擎. ...
- Android 数据存储之 SQLite数据库存储
----------------------------------------SQLite数据库---------------------------------------------- SQLi ...
- 【Android 应用开发】Android 数据存储 之 SQLite数据库详解
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19028665 . SQLiteDataBase示例程序下 ...
- Android 数据存储 之 SQLite数据库详解
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19028665 . SQLiteDataBase示例程序下 ...
- Android数据存储之SQLite 数据库学习
Android提供了五种存取数据的方式 (1)SharedPreference,存放较少的五种类型的数据,只能在同一个包内使用,生成XML的格式存放在设备中 (2) SQLite数据库,存放各种数据, ...
- Android数据存储之SQLCipher数据库加密
前言: 最近研究了Android Sqlite数据库(文章地址:Android数据存储之Sqlite的介绍及使用)以及ContentProvider程序间数据共享(Android探索之ContentP ...
- Android数据存储:SQLite
Android数据存储之SQLite SQLite:Android提供的一个标准的数据库,支持SQL语句.用来处理数据量较大的数据.△ SQLite特征:1.轻量性2.独立性3.隔离性4.跨平台性5. ...
- 【转载】Android数据存储之SQLite
SQLite是D.Richard Hipp用C语言编写的开源嵌入式数据库引擎.它支持大多数的SQL92标准,并且可以在所有主要的操作系统上运行. 在Android中创建的SQLite数据库存储在:/d ...
- Android数据存储之SQLite使用
SQLite是D.Richard Hipp用C语言编写的开源嵌入式数据库引擎.它支持大多数的SQL92标准,并且可以在所有主要的操作系统上运行. 在Android中创建的SQLite数据库存储在:/d ...
随机推荐
- StringJdbc :jdbcTemplate
Spring框架对Jdbc进行了封装 提供了一个JDBCTemplated对象简化Jdbc开发 步骤 1 导包 2 创建JDBCTemplate 对象 依赖于DataSource 3 调用JDBCTe ...
- Redis的Cluster配置
Redis的Cluster配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装Redis并启动 1>.修改Redis的配置文件(本实验只有三个节点) [root@no ...
- 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)
一. 说在前面的话 本节主要在前面章节的基础上补充了几个简单的知识点,比如:第三方调用通过 GlobalHost.ConnectionManager.GetHubContext<MySpecHu ...
- CSS iconfont阿里巴巴矢量图库在开发中实战使用
前言 项目开发中,是避免不了使用小图标的,那么国内比较好用的图标网站当属iconfont了,下面我们将详细介绍如何使用. iconfont选择所需图标 1.iconfont官网 2.把所需要的添加进入 ...
- 网站设置ico图标
1.用设计的png图片去在线图标网站上生成一个16*16大小的图标,命名favcon.ico放置到网站根目录下如:http://www.faviconico.org/favicon2.添加代码 < ...
- Groovy 设计模式 -- 保镖模式
Bouncer Pattern http://groovy-lang.org/design-patterns.html#_bouncer_pattern 保镖模式主要负责对函数的输入参数的合法性检查, ...
- DensePose: Dense Human Pose Estimation In The Wild(理解)
0 - 背景 Facebook AI Research(FAIR)开源了一项将2D的RGB图像的所有人体像素实时映射到3D模型的技术(DensePose).支持户外和穿着宽松衣服的对象识别,支持多人同 ...
- codeblocks更改颜色主题
链接:http://www.cnblogs.com/wenbosheng/p/5899483.html
- pythonのsqlalchemy多对多关系
现在来设计一个能描述“图书”与“作者”的关系的表结构,需求是 一本书可以有好几个作者一起出版 一个作者可以写好几本书 #!/usr/bin/env python from sqlalchemy imp ...
- Encode and Decode TinyURL
TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/desi ...