目标:是否可以在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数据库的更多相关文章

  1. Android数据存储之SQLite数据库

    Android数据存储 之SQLite数据库简介 SQLite的相关知识,并结合Java实现对SQLite数据库的操作. SQLite是D.Richard Hipp用C语言编写的开源嵌入式数据库引擎. ...

  2. Android 数据存储之 SQLite数据库存储

    ----------------------------------------SQLite数据库---------------------------------------------- SQLi ...

  3. 【Android 应用开发】Android 数据存储 之 SQLite数据库详解

    . 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19028665 . SQLiteDataBase示例程序下 ...

  4. Android 数据存储 之 SQLite数据库详解

    . 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19028665 . SQLiteDataBase示例程序下 ...

  5. Android数据存储之SQLite 数据库学习

    Android提供了五种存取数据的方式 (1)SharedPreference,存放较少的五种类型的数据,只能在同一个包内使用,生成XML的格式存放在设备中 (2) SQLite数据库,存放各种数据, ...

  6. Android数据存储之SQLCipher数据库加密

    前言: 最近研究了Android Sqlite数据库(文章地址:Android数据存储之Sqlite的介绍及使用)以及ContentProvider程序间数据共享(Android探索之ContentP ...

  7. Android数据存储:SQLite

    Android数据存储之SQLite SQLite:Android提供的一个标准的数据库,支持SQL语句.用来处理数据量较大的数据.△ SQLite特征:1.轻量性2.独立性3.隔离性4.跨平台性5. ...

  8. 【转载】Android数据存储之SQLite

    SQLite是D.Richard Hipp用C语言编写的开源嵌入式数据库引擎.它支持大多数的SQL92标准,并且可以在所有主要的操作系统上运行. 在Android中创建的SQLite数据库存储在:/d ...

  9. Android数据存储之SQLite使用

    SQLite是D.Richard Hipp用C语言编写的开源嵌入式数据库引擎.它支持大多数的SQL92标准,并且可以在所有主要的操作系统上运行. 在Android中创建的SQLite数据库存储在:/d ...

随机推荐

  1. SQL Server 2008还原数据库时出现“备份集中的数据库备份与现有的数据库不同”的解决方法

    引言 现在在做项目,由于每个人是分模块的,所以大家的测试数据都不同步,导致好多时候会因为别人填的数据不同而调半天的错.所以我还是自己还原一个数据库,自己填自己的数据吧. 报错 之前还原过很多个数据库都 ...

  2. Gaussian Process for Regression

    python风控评分卡建模和风控常识(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005214003&am ...

  3. Numpy系列(二)- 数据类型

    Numpy 中的数组比 Python 原生中的数组(只支持整数类型与浮点类型)强大的一点就是它支持更多的数据类型. 基本数据类型 numpy常见的数据类型 数据类型 描述 bool_ 布尔(True或 ...

  4. Vue工具

    首先介绍几个概念 webpack: 打包机.它能将我们的html,css,js.png,font进行打包,交给服务器. vue-cli: 用户生成Vue工程模板.(帮你快速开始一个vue的项目,也就是 ...

  5. 金融量化分析【day112】:因子选股

    一.因子选股基础 二.因子选股策略实现代码 # 导入函数库 import jqdata import psutil #初始化函数,设定基准等等 def initialize(context): set ...

  6. Entity Framework入门教程(16)---Enum

    EF DbFirst模式中的枚举类型使用 这一节介绍EF DbFirst模式中的Enum(枚举类型),CodeFirst模式中的Enum会在以后的EF CoreFirst系列中介绍.EF5中添加了对E ...

  7. SQL修改日期类型字段为字符串类型

    select * from test1 --添加行 ) --将转换格式后的数据放到列中 ) --删除老的字段 alter table test1 drop column startdate --修改字 ...

  8. C# Textbox 自动换行

    方法1 使用textbox的AppendText方法 方法2 textBox.ScrollToCaret(); this.textBox.Focus();//获取焦点 this.textBox.Sel ...

  9. Dilated Convolution

    各种各样的卷积方式, 详细见 各种卷积的 gif 图 Convolution animations  Padding, strides Transposed convolution animatio ...

  10. WPF 之 调用线程必须为 STA,因为许多 UI 组件都需要

    WPF中,代码中准备控制控件内容时,有时会报错:“ 调用线程必须为 STA,因为许多 UI 组件都需要 ”. 如在winform下面,使用多线程时,控件的值读取是可以的,但如果要更改,那么就必须进行一 ...