当我们存储的数据比较多的时候,我们就应该用到数据库了。在安卓中集成了SQLite数据库,这里简单的介绍下SQLite的使用。
a.SQLite的特点:
1.最大支持2TB的数据存储(这已经非常大了,相对于智能手机而言,因为我们的手机现在存储还停留在几百GB的水平,等到手机能存储几TB的时候,说不定SQLite就能存储更大的数据了)
2.占用资源非常少,大概在250KB左右,着相对于现在智能手机动辄几GB的内存,简直就可以忽略了。
3.API简单,易于使用
4.没有额外的依赖项,是独立的存在
5.开源
6.支持主流的编程语言(Java,C#,PHP等)
b.有了上面的特性,下面我们简单的看下SQLite的基本应用:
1.创建数据库
创建数据库使用openDataBase方法,这个方法可以用来创建和打开数据库。
public static SQLiteDataBase openDataBase(Stringpath,SQLiteDataBase.CursorFactory factory,int flag)
path:数据库所在的路径
factory:游标工厂,这个值通常为null
flag:标识,可控制数据库的访问模式
2.插入数据
插入数据使用insert方法
public long insert(String table,String nullColumnHack,ContenValuesvalues)
table:待插入的表名
nullColumnHack:通常为null
values:待插入的数据
3.更新数据
更新数据使用update方法
public int update(String table,ContentValues values,StringwhereClause,String[] whereArgs)
table:待更新的表名
values:待更新的内容
whereClause:where子句的内容
whereArgs:where子句的参数
4.删除数据
删除数据使用delete方法
public int delete(String table,String whereClause,String[]whereArgs)
table:要操作的表名
whereClause:where子句的内容
whereArgs:where子句的参数
5.查询数据
查询数据使用query方法
public Cursor query(String table,String[]columns,Stringselection,String[] selectionArgs,String groupBy,String having, String orderBy)
table:查询的表
colurmus:查询的列
selection:过滤记录的子句
selectionArgs:过滤的参数值
groupBy:分组子句
having:过滤分组的子句
orderBy:记录排序子句
6.执行非查询语句
执行非查询语句使用execSQL方法
public void execSQL(String sql);
public void execSQL(String sql,Object[] bindArgs);
sql:需要执行的sql语句
bindArgs:带参数的sql语句的参数数组
7.执行查询语句
执行查询语句使用rawQuery方法
public Cursor rawQuery(String sql,String[] selectionArgs)
sql:需要执行时sql语句
selectionArgs:查询参数的值
简单的Demo:
MainActivity.java
package com.example.sampel2_4;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
SQLiteDatabase sld ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button cteOpenButton = (Button)this.findViewById(R.id.btn_create_open);
cteOpenButton.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "创建/打开数据库", Toast.LENGTH_SHORT).show();
createOrOpenDataBase();
}
});
Button closeButton = (Button)this.findViewById(R.id.btn_close) ;
closeButton.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "关闭数据库", Toast.LENGTH_SHORT).show();
closeDataBase();
}
}
) ;
Button addButton = (Button)this.findViewById(R.id.btn_add) ;
addButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "添加新纪录", Toast.LENGTH_SHORT).show();
insert();
}
}) ;
Button delButton = (Button)this.findViewById(R.id.btn_delete) ;
delButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "删除记录", Toast.LENGTH_SHORT).show();
delete();
}
});
Button queButton = (Button)this.findViewById(R.id.btn_query) ;
queButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "查询记录", Toast.LENGTH_SHORT).show() ;
query();
}
}) ;
}
public void createOrOpenDataBase()
{
try {
sld = SQLiteDatabase.openDatabase(
"/data/data/com.example.sampel2_4/myDB",
null,
SQLiteDatabase.OPEN_READWRITE|
SQLiteDatabase.CREATE_IF_NECESSARY) ;
String sqlString = "create table if not exists student"+
"(sno char(5),stuname varchar(20),"+
"sage integer,sclass char(5))";
sld.execSQL(sqlString);
Toast.makeText(getBaseContext(), "create database succdess", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void closeDataBase()
{
try {
sld.close();
Toast.makeText(getBaseContext(), "close database success", Toast.LENGTH_SHORT).show() ;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace() ;
}
}
public void insert()
{
try {
String sqlString = "insert into student values"+
"('001','Android',22,'283')";
sld.execSQL(sqlString) ;
Toast.makeText(getBaseContext(), "insert success", Toast.LENGTH_SHORT).show() ;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace() ;
}
}
public void delete()
{
try {
String sqlString = "delete from student;" ;
sld.execSQL(sqlString) ;
Toast.makeText(getBaseContext(), "delete success", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace() ;
}
}
public void query()
{
try {
String sqlString = "select * from student where sage>?" ;
Cursor cursor = sld.rawQuery(sqlString, new String[]{"20"}) ;
while (cursor.moveToNext()) {
String snoString = cursor.getString(0) ;
String sname = cursor.getString(1);
int sage = cursor.getInt(2);
String sclass = cursor.getString(3);
Toast.makeText(getBaseContext(), "查询到的记录为:"+snoString+","+sname+","+sage+","+sclass, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sampel2_4.MainActivity" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:text="SQLite基础操作"
android:id="@+id/TextView01"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="创建/打开数据库"
android:id="@+id/btn_create_open"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:text="关闭数据库"
android:id="@+id/btn_close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:text="添加记录"
android:id="@+id/btn_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:text="删除记录"
android:id="@+id/btn_delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:text="查询记录"
android:id="@+id/btn_query"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>