一、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. python绘图工具matplotlib在linux下安装和使用

    工具介绍:matplotlib 是Python编程语言的一个绘图库及其数值数学扩展 NumPy.它为利用通用的图形用户界面工具包, 如Tkinter, wxPython, Qt或GTK+向应用程序嵌入 ...

  2. Shell - 文本处理

    珠玉在前,不再赘言. 常用命令 LinuxShell文本处理工具集锦 数据工程师常用的Shell命令 文件和目录管理 简明教程 AWK简明教程 SED简明教程 命令详解 linux sort,uniq ...

  3. VS Code:让你工作效率翻倍的23个插件和23个编辑技巧

    VS Code:让你工作效率翻倍的23个插件和23个编辑技巧 总结了一些平时常用且好用的 VS Code 的插件和编辑技巧分享出来. 文章详情可查阅我的博客:lishaoy.net ,欢迎大家访问. ...

  4. 可能比文档还详细--VueRouter完全指北

    可能比文档还详细--VueRouter完全指北 前言 关于标题,应该算不上是标题党,因为内容真的很多很长很全面.主要是在官网的基础上又详细总结,举例了很多东西.确保所有新人都能理解!所以实际上很多东西 ...

  5. 深入浅出分析MySQL MyISAM与INNODB索引原理、优缺点分析

    本文浅显的分析了MySQL索引的原理及针对主程面试的一些问题,对各种资料进行了分析总结,分享给大家,希望祝大家早上走上属于自己的"成金之路". 学习知识最好的方式是带着问题去研究所 ...

  6. Asp.net webform scaffolding结合Generic Unit of Work & (Extensible) Repositories Framework代码生成向导

    Asp.net webform scaffolding结合Generic Unit of Work & (Extensible) Repositories Framework代码生成向导 在上 ...

  7. Struts2学习(四)———— ognl表达式、值栈、actionContext之间的关系

    一.什么是Ognl? 通过百度百科查询到的解释,其中详细的说明了OGNL的作用. 下面我们就对OGNL这5个作用进行讲解 1.存取对象的任意属性,简单说就是对javabean进行操作(重要) 2.调用 ...

  8. #11 Python字典

    前言 前两节介绍了Python列表和字符串的相关用法,这两种数据类型都是有序的数据类型,所以它们可以通过索引来访问内部元素.本文将记录一种无序的数据类型——字典! 一.字典与列表和字符串的区别 字典是 ...

  9. 一个复杂Json的解析

    { "website": { "1": { "basic": { "homepage": "http://py ...

  10. [AGC 018 E] Sightseeing plan

    STO ZKY ORZ Description 给定一张网格图和三个矩形,每次只能向上或向右走.你需要从矩形 \(A\) 中的一个点 \(S\) 出发,到达矩形 \(B\) 中的一个点 \(P\) , ...