Android内置了
SQLite
数据库系统。
关于一些SQL基本命令的用法(
CREATE,INSERT,UPDATE,DELETE
和
SELECT
),
请参阅SQLite文档( http://www.sqlite.org/lang.html )
与
SharedPreferences
不同,
不能
将数据库设为
WORLD_READABLE
,每个数据库都只能创建它的包访问,
这意味着只有由创建数据库的进程可访问它。如果需要在进程间传递数据,
则可以使用AIDL/Binder或创建一个
ContentProvider
,
但是不能跨越进程/包边界直接来使用数据库
。
数据库文件存放在
/data/data/[PACKAGE_NAME]/database/
下。这些文件是SQLite专有的。
可以通过ADB shell(sqlite3工具)来操作它。
详细命令可参考
http://www.sqlite.org/sqlite.html 。
实例1:
DBHelper.java文件
package com.teleca;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;;
public class
DBHelper
{
public static
String DB_NAME="
peopledb
";
public static
String
DB_TABLE_NAME
="
people
";
SQLiteDatabase db;
final
DBOpenHelper dbOpenHelper;
final static
String COLS[]=new String[]
{"_id","name","phone","age"};
final
String tag="robin";
public
DBHelper(Context context)
{
this.dbOpenHelper=new DBOpenHelper(context,DB_NAME,1);
establishDb();
}
private void
establishDb()
{
if
(db==null)
db=dbOpenHelper.getWritableDatabase();
}
public void
cleanup()
{
if(db!=null)
{
db.close();
db=null;
}
}
public void
insert(People people)
{
ContentValues values=new ContentValues();
values.put("name", people.name);
values.put("phone", people.phone);
values.put("age", people.age);
people.id=db.insert(DBHelper.DB_TABLE_NAME, null, values);
Log.i(tag,"insert:"+people.id);
//select top 1 id from tablename order by id
Cursor c=db.query(DBHelper.DB_TABLE_NAME, new String[]{"_id"}, null, null, null, null, "_id DESC","1");
c.moveToFirst();
Log.i("hubin", "id Max:"+c.getInt(0));
c.close();
}
public void
update(People people)
{
ContentValues values=new ContentValues();
values.put("name", people.name);
values.put("phone", people.phone);
values.put("age", people.age);
db.update(DBHelper.DB_TABLE_NAME,values,"_id="+people.id,null);
}
public void
delete(long id)
{
db.delete(DB_TABLE_NAME, "_id="+id, null);
}
public void
deleteAll()
{
db.delete(DB_TABLE_NAME, null, null);
}
public
People get(long id)
{
People people=new People();
Cursor c=null;
try
{
c=db.query(DB_TABLE_NAME, COLS,"_id= '"+id+"'",null,null,null,null);
Log.i(tag,"count:"+c.getCount());
if(c.getCount()>0)
{
c.moveToFirst();
people=new People();
people.id=c.getLong(0);
people.name=c.getString(1);
people.phone=c.getString(2);
people.age=c.getInt(3);
}
}
catch
(SQLException e)
{
Log.i(tag,"",e);
}
finally
{
if(c!=null&&!c.isClosed())
{
c.close();
}
}
return
people;
}
public
List<People> getAll()
{
ArrayList<People> ret=new ArrayList<People>();
Cursor c=null;
try
{
c=db.query(DB_TABLE_NAME, COLS,null,null,null,null,null);
int count=c.getCount();
c.moveToFirst();
People people;
for(int i=0;i<count;i++)
{
people=new People();
people.id=c.getLong(0);
people.name=c.getString(1);
people.phone=c.getString(2);
people.age=c.getInt(3);
ret.add(people);
c.moveToNext();
}
}
catch
(SQLException e)
{
Log.i(tag,"",e);
}
finally
{
if(c!=null&&!c.isClosed())
{
c.close();
}
}
return
ret;
}
}
class
DBOpenHelper
extends SQLiteOpenHelper
{
private static final
String DB_CREATE="CREATE TABLE "
+DBHelper.DB_TABLE_NAME
+" (_id INTEGER PRIMARY KEY,name TEXT UNIQUE NOT NULL,"
+"phone TEXT,age INTEGER);";
final static
String tag="robin";
public
DBOpenHelper(Context context,String dbName,int version)
{
super(context,dbName,null,version);
}
public void
onCreate(SQLiteDatabase db)
{
try{
db.execSQL(DB_CREATE);
}
catch(SQLException e )
{
Log.e(tag,"",e);
}
}
public void
onOpen(SQLiteDatabase db)
{
super.onOpen(db);
}
public void
onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS "+DBHelper.DB_TABLE_NAME);
this.onCreate(db);
}
}
Hello.java文件
package com.teleca;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class
Hello
extends
Activity {
/** Called when the activity is first created. */
DBHelper dbHelper;
final static
String tag="hubin";
@Override
public void
onCreate
(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.Button01);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
cmd = CMD_ADD;
doAction();
}
};
button.setOnClickListener(listener);
Button button2 = (Button) findViewById(R.id.Button02);
OnClickListener listener2 = new OnClickListener() {
@Override
public void onClick(View v) {
cmd = CMD_UPDATE;
doAction();
}
};
button2.setOnClickListener(listener2);
Button button3 = (Button) findViewById(R.id.Button03);
OnClickListener listener3 = new OnClickListener() {
@Override
public void onClick(View v) {
cmd = CMD_QUERY;
doAction();
}
};
button3.setOnClickListener(listener3);
Button button4 = (Button) findViewById(R.id.Button04);
OnClickListener listener4 = new OnClickListener() {
@Override
public void onClick(View v) {
cmd = CMD_QUERY_ALL;
doAction();
}
};
button4.setOnClickListener(listener4);
Button button5 = (Button) findViewById(R.id.Button05);
OnClickListener listener5 = new OnClickListener() {
@Override
public void onClick(View v) {
cmd = CMD_DELETE;
doAction();
}
};
button5.setOnClickListener(listener5);
Button button6 = (Button) findViewById(R.id.Button06);
OnClickListener listener6 = new OnClickListener() {
@Override
public void onClick(View v) {
cmd = CMD_DELETE_ALL;
doAction();
}
};
button6.setOnClickListener(listener6);
mHandler = new Handler();
}
int cnt = 0;
private Handler mHandler;
int cmd = 0;
final int CMD_ADD = 1;
final int CMD_UPDATE = 2;
final int CMD_QUERY= 3;
final int CMD_QUERY_ALL = 4;
final int CMD_DELETE = 5;
final int CMD_DELETE_ALL = 6;
People people=new People();
class
DatabaseThread
implements Runnable {
public void
run()
{
if
(dbHelper==null)
dbHelper=new DBHelper(Hello.this);
if
(cmd == CMD_ADD) {
people.name="robin"+System.currentTimeMillis()%100;
people.phone=""+System.currentTimeMillis();
people.age=1;
dbHelper.insert(people);
}
else if
(cmd == CMD_UPDATE) {
people.phone=""+System.currentTimeMillis();
dbHelper.update(people);
}
else if
(cmd == CMD_QUERY) {
People p=dbHelper.get(people.id);
printPeople(p);
}
else if
(cmd == CMD_QUERY_ALL) {
List<People> list=dbHelper.getAll();
int total=list.size();
for(int i=0;i<total;i++)
{
printPeople(list.get(i));
}
}
else if
(cmd==CMD_DELETE)
{
dbHelper.delete(people.id);
}
else if
(cmd==CMD_DELETE_ALL)
{
dbHelper.deleteAll();
}
cnt++;
}
}
void
printPeople
(People p)
{
Log.i(tag, "id:"+p.id);
Log.i(tag, "name:"+p.name);
Log.i(tag,"phone:"+p.phone);
Log.i(tag,"age:"+p.age);
}
DatabaseThread dataDealer=new DatabaseThread();
void
doAction()
{
mHandler.post(dataDealer);
}
}
people.java文件
package com.teleca;
public class
People
{
public long id;
public String name;
public String phone;
public int age;
}