一、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. [Mac]如何让两个窗口各占半个屏幕

    OS X中的拆分视图El Capitan或更高版本允许您使用两个应用程序填充Mac屏幕,而无需手动移动和调整窗口大小. 进入拆分视图 按住 窗口左上角的全屏按钮  . 当您按住按钮时,窗口会缩小,您可 ...

  2. Shell-13--while和until

  3. 开源性能测试工具Locust使用篇(一)

    1. 环境准备 安装python3.6 ,安装步骤略 pip install locust 安装完成后使用locust -V检查 2.locust使用,先编辑一个简单的load_test.py的脚本 ...

  4. IntelliJ Idea 授权服务器使用

    JetBrains授权服务器 1 http://intellij.mandroid.cn/ 支持的版本 IntelliJ IDEA 7.0 或更高ReSharper 3.1 或更高ReSharper ...

  5. 使用POI导出Excel文件

    创建表头信息 表头信息用于自动生成表头结构及排序 public class ExcelHeader implements Comparable<ExcelHeader>{ /** * ex ...

  6. ZooKeeper和Curator相关经验总结

    一.关于ZooKeeper的watch用法,需要注意 详细说明如下: ZooKeeper Watches All of the read operations in ZooKeeper - getDa ...

  7. IDEA中SpringBoot项目快速创建单元测试

    如何在IDEA中对于SpringBoot项目快速创建单元测试 创建测试用例 右键需要进行测试的方法,选择GO TO然后选择Test 点击Create New Test 勾选需要创建单元测试的方法 然后 ...

  8. Java并发编程笔记之ThreadLocal内存泄漏探究

    使用 ThreadLocal 不当可能会导致内存泄露,是什么原因导致的内存泄漏呢? 我们首先看一个例子,代码如下: /** * Created by cong on 2018/7/14. */ pub ...

  9. Python机器学习笔记:不得不了解的机器学习面试知识点(1)

    机器学习岗位的面试中通常会对一些常见的机器学习算法和思想进行提问,在平时的学习过程中可能对算法的理论,注意点,区别会有一定的认识,但是这些知识可能不系统,在回答的时候未必能在短时间内答出自己的认识,因 ...

  10. Java设计模式学习记录-迭代器模式

    前言 这次要介绍的是迭代器模式,也是一种行为模式.我现在觉得写博客有点应付了,前阵子一天一篇,感觉这样其实有点没理解透彻就写下来了,而且写完后自己也没有多看几遍,上次在面试的时候被问到java中的I/ ...