一、SQLite

SQLite第一个Alpha版本诞生于2000年5月,它是一款轻量级数据库,它的设计目标是嵌入式的,占用资源非常的低,只需要几百K的内存就够了。SQLite已经被多种软件和产品使用

二、SQLite特性

  、轻量级
SQLite和C\S模式的数据库软件不同,它是进程内的数据库引擎,因此不存在数据库的客户端和服务器。使用SQLite一般只需要带上它的一个动态库,就可以享受它的全部功能。而且那个动态库的尺寸也相当小。
、独立性
SQLite数据库的核心引擎本身不依赖第三方软件,使用它也不需要“安装”,所以在使用的时候能够省去不少麻烦。
、隔离性
SQLite数据库中的所有信息(比如表、视图、触发器)都包含在一个文件内,方便管理和维护。
、跨平台
SQLite数据库支持大部分操作系统,除了我们在电脑上使用的操作系统之外,很多手机操作系统同样可以运行,比如Android、Windows Mobile、Symbian、Palm等。
、多语言接口
SQLite数据库支持很多语言编程接口,比如C\C++、Java、Python、dotNet、Ruby、Perl等,得到更多开发者的喜爱。
、安全性
SQLite数据库通过数据库级上的独占性和共享锁来实现独立事务处理。这意味着多个进程可以在同一时间从同一数据库读取数据,但只有一个可以写入数据。在某个进程或线程向数据库执行写操作之前,必须获得独占锁定。在发出独占锁定后,其他的读或写操作将不会再发生。

三、SQLiteDatabase方法

1、添加

 public long insert (String table, String nullColumnHack, ContentValues values)
Added in API level
Convenience method for inserting a row into the database. Parameters
table the table to insert the row into
nullColumnHack optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty.
values this map contains the initial column values for the row. The keys should be the column names and the values the column values Returns
the row ID of the newly inserted row, or - if an error occurred

2、删除

 public int delete (String table, String whereClause, String[] whereArgs)
Added in API level
Convenience method for deleting rows in the database. Parameters
table the table to delete from
whereClause the optional WHERE clause to apply when deleting. Passing null will delete all rows.
whereArgs You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings. Returns
the number of rows affected if a whereClause is passed in, otherwise. To remove all rows and get a count pass "" as the whereClause.

3、修改

 public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
Added in API level
Convenience method for updating rows in the database. Parameters
table the table to update in
values a map from column names to new column values. null is a valid value that will be translated to NULL.
whereClause the optional WHERE clause to apply when updating. Passing null will update all rows.
whereArgs You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings. Returns
the number of rows affected

4、查询

 public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
Added in API level
Query the given table, returning a Cursor over the result set. Parameters
table The table name to compile the query against.
columns A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data from storage that isn't going to be used.
selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.
selectionArgs You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.
groupBy A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped.
having A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used.
orderBy How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered. Returns
A Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized, see the documentation for more details.
See Also
Cursor

四、Demo

 package com.example.demosql;

 import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper{ public DBHelper(Context context)
{
super(context, "mydata.db", null, );
}
@Override
public void onCreate(SQLiteDatabase db) {
//建立数据表
db.execSQL("create table mydatabase(_id integer primary key,name text,age text)"); } @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//版本更新
if(newVersion>oldVersion)
{
db.execSQL("drop table if exists mydatabase");
} } }

--------------------------------------------------------------------------------------------------------------------------------------

Activity

 package com.example.demosql;

 import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle; public class MainActivity extends Activity { private DBHelper dbhelper ;
private SQLiteDatabase sqldb;
private int num = ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbhelper = new DBHelper(this);
//添加数据
addData("Alice");
selectData();
System.out.println("---------------------------");
//修改数据
updataData();
selectData();
System.out.println("---------------------------");
//删除数据
deleteData();
selectData();
System.out.println("---------------------------");
//添加数据
addData("Tom");
addData("Mark");
selectData();
clearData();
selectData();
System.out.println("---------------------------"); } //添加数据
public void addData(String name)
{
sqldb = dbhelper.getWritableDatabase();
ContentValues value = new ContentValues();
value.put("name", name);
value.put("age", "");
//第一个参数是要修改的数据库名称,第三个参数是数据源(数组格式),返回成功添加的数据条数
int num = (int) sqldb.insert("mydatabase", null, value);
if(num>)
System.out.println("添加数据成功");
//关闭数据库
sqldb.close();
}
//删除数据
public void deleteData()
{
sqldb = dbhelper.getWritableDatabase();
String[] name = {"Alice"};
//第一个参数是修改的数据库名称,第二个参数是删除数据要符合的条件,第三个参数是修改的数据源(数组格式),对应第二个参数?的位置
int num = sqldb.delete("mydatabase", "name=?", name);
if(num>)
System.out.println("删除数据成功");
//关闭数据库
sqldb.close();
}
//修改数据
public void updataData()
{
sqldb = dbhelper.getWritableDatabase();
ContentValues value = new ContentValues();
value.put("name", "Alice");
value.put("age", "");
String names[] = {"Alice"};
//第一个参数为要修改的数据库名称,第二个参数为数据源,第三个参数为修改要符合的条件,第四个参数是对应第三个参数?位置的值
int num = sqldb.update("mydatabase", value, "name=?",names );
if(num>)
System.out.println("修改数据成功");
sqldb.close();
}
//查询数据
public void selectData()
{
//注意,查询不用修改 getReadableDatabase()而不是getWritableDatabase()
sqldb = dbhelper.getReadableDatabase();
String columns[] = {"name","age"};
//第一个参数为数据库名称,第二个参数为要查询的字段,
Cursor cursor = sqldb.query("mydatabase", columns, null, null, null, null, null);
System.out.println("此时的数据为:");
while(cursor.moveToNext())
{
//参数为第几个字段,注意第0个字段不是id
String name = cursor.getString();
String age = cursor.getString();
System.out.println("name:"+name+",age:"+age);
}
sqldb.close();
}
//清空数据库
public void clearData()
{
sqldb = dbhelper.getWritableDatabase();
sqldb.delete("mydatabase", null, null);
System.out.println("执行清空数据库操作");
sqldb.close();
}
}

看一下打印结果:

数据库存放位置:

data/data/包名/数据库名

----------------------------------------------------------------------------------------------------------------------------------

相关知识:

安卓开发_数据存储技术_外部存储

安卓开发_数据存储技术_内部存储

安卓开发_数据存储技术_SharedPreferences类

安卓开发_数据存储技术_sqlite的更多相关文章

  1. 安卓开发_数据存储技术_SharedPreferences类

    SharedPreferences类 供开发人员保存和获取基本数据类型的键值对. 该类主要用于基本类型,例如:booleans,ints,longs,strings.在应用程序结束后,数据仍旧会保存. ...

  2. iOS开发_数据存储方式

    对于数据持久化的问题,博主并不准备在博文内放很多的代码进行更深一步解释,只是简单的介绍一下四种数据持久化,如有时间,会另外针对各个数据持久化的方法进行更进一步的阐述. 直接进入主题: 〈1.NSUse ...

  3. 安卓开发_深入学习ViewPager控件

    一.概述 ViewPager是android扩展包v4包(android.support.v4.view.ViewPager)中的类,这个类可以让用户左右切换当前的view. ViewPager特点: ...

  4. 安卓开发_浅谈ListView(SimpleAdapter数组适配器)

    安卓开发_浅谈ListView(ArrayAdapter数组适配器) 学习使用ListView组件和SimapleAdapter适配器实现一个带图标的ListView列表 总共3部分 一.MainAc ...

  5. Android实现数据存储技术

    转载:Android实现数据存储技术 本文介绍Android中的5种数据存储方式. 数据存储在开发中是使用最频繁的,在这里主要介绍Android平台中实现数据存储的5种方式,分别是: 1 使用Shar ...

  6. Android数据存储技术

    Android提供了4种数据存储技术,分别是SharedPreferences.Files.SQLite数据库和网络存储数据.(有的开发者认为使用ContentProvider也可以算是一种,但我觉得 ...

  7. 安卓开发_浅谈ListView(自定义适配器)

    ListView作为一个实际开发中使用率非常高的视图,一般的系统自带的适配器都无法满足开发中的需求,这时候就需要开发人员来自定义适配器使得ListView能够有一个不错的显示效果 有这样一个Demo ...

  8. 安卓开发_浅谈Fragment之ListFragment

    ListFragment,即Fragment的一个子类,当我们用的一个Fragment只需要一个listview视图的时候使用 该类有几个特点: 1.ListFragment 本身具只有一个ListV ...

  9. 安卓开发_浅谈Android动画(四)

    Property动画 概念:属性动画,即通过改变对象属性的动画. 特点:属性动画真正改变了一个UI控件,包括其事件触发焦点的位置 一.重要的动画类及属性值: 1.  ValueAnimator 基本属 ...

随机推荐

  1. Linux 后台运行命令:nohup 和 &

    [参开文章]:nohup 与 & 的区别 1. nohup 1.1 基本概念 将程序以忽略挂起信号的方式运行起来: 不可以免疫 Ctrl + C  的 SIGINT 中断信号: 可以免疫 SI ...

  2. javaScript 节流与防抖

    首先 我们要知道 节流与防抖可以干什么. 优化网络请求性能——节流 优化页面请求性能——防抖 举两个简单的小例子: 节流: 例如 有些购物页面,会有一些让你抢购的活动,到点的时候,需要你快速的点某个按 ...

  3. linux下的初始化系统systemd简科普

    systemd是什么?名字很奇怪,不认识. 扒一扒wiki,你就会知道我是谁了? 在下有眼不识泰山,原来你就是盘古老爷爷用的开天辟地大斧头啊. 贫穷不可怕,可怕的是贫穷限制了你的想象--------- ...

  4. 程序性能调优工具之gprob

    1 简介改进应用程序的性能是一项非常耗时耗力的工作,但是究竟程序中是哪些函数消耗掉了大部分执行时间,这通常都不是非常明显的.GNU 编译器工具包所提供了一种剖析工具 GNU profiler(gpro ...

  5. Python基础入门教程(3)

    人生苦短,我学Pyhton Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于199 ...

  6. Angular使用总结 --- 通过指令动态添加组件

    之前自己写的公共组件,都是会先引入,需要调起的时候再通过service控制公共组件状态.值.回调函数什么的.但是有一些场景不适合这种方式,还是动态添加组件更加好.通过写过的一个小组件来总结下. 创建组 ...

  7. Liferay7 BPM门户开发之19: 理解Service Builder体系

    Service Builder是Liferay为业务开发而设计的模型驱动(model-driven)平台工具,提供一系列的实体类.数据持久化.服务相关的代码自动生成服务.支持Hibernate and ...

  8. redis lru实现策略

    转载自http://blog.chinaunix.net/uid-20708886-id-5753422.html 在使用redis作为缓存的场景下,内存淘汰策略决定的redis的内存使用效率.在大部 ...

  9. [源码]python Scapy Ftp密码嗅探

    [源码]python Scapy Ftp密码嗅探 原理很简单,FTP密码明文传输的 截取tcp 21端口User和Pass数据即可 Scapy框架编译程序较大(一个空程序都25M),所以就不提供exe ...

  10. cmd窗口使用sftp命令非密钥和密钥登录SFTP服务器的两种方式

    cmd窗口使用sftp命令非密钥和密钥登录SFTP服务器的两种方式 一.在Windows环境下搭建SFTP服务器可参见http://www.cnblogs.com/Kevin00/p/6341295. ...