首页 > 代码库 > Android之SQLiteOpenHelper
Android之SQLiteOpenHelper
1.SQLiteOpenHelper
SQliteOpenHelper是一个抽象类,来管理数据库的创建和版本的管理。要使用它必须实现它的nCreate(SQLiteDatabase),onUpgrade(SQLiteDatabase, int, int)方法
onCreate:当数据库第一次被建立的时候被执行,例如创建表,初始化数据等。
onUpgrade:当数据库需要被更新的时候执行,例如删除久表,创建新表。
2.实现代码
[java] view plaincopy
<EMBED id=ZeroClipboardMovie_1 height=18 name=ZeroClipboardMovie_1 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=1&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
package xqh.utils;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class DBHelper extends SQLiteOpenHelper {
//数据库版本
private static final int VERSION = 1;
//新建一个表
String sql = "create table if not exists TestUsers"+
"(id int primary key,name varchar,sex varchar)";
public DBHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
public DBHelper(Context context,String name,int version){
this(context,name,null,version);
}
public DBHelper(Context context,String name){
this(context,name,VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
3.SQLite的使用
Android提供了一个名为SQLiteDatabase的类,它封装了一些操作数据库的API。使用它能实现基本的CRUD操作,通过getWritableDatabase()和getReadableDatabase()可以获取数据库实例。
4.实现代码
[java] view plaincopy
<EMBED id=ZeroClipboardMovie_2 height=18 name=ZeroClipboardMovie_2 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=2&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
package xqh.sqlite;
import xqh.utils.DBHelper;
import android.app.Activity;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;;
public class TestSQLite extends Activity {
Button textBtn = null;
Button btnCreateDb = null;
Button btnCreateTb = null;
Button btnInsert = null;
Button btnUpdate = null;
Button btnDelete = null;
DBHelper dbHelper = null;
SQLiteDatabase db = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlitetest);
OpenDb();
textBtn = (Button)findViewById(R.id.btnHeader);
textBtn.setFocusable(true);
// btnCreateDb = (Button)findViewById(R.id.btnCreateDb);
// btnCreateDb.setOnClickListener(createDbListener);
//
// btnCreateTb = (Button)findViewById(R.id.btnCreateTb);
// btnCreateTb.setOnClickListener(createTbListener);
btnInsert = (Button)findViewById(R.id.btnInsert);
btnInsert.setOnClickListener(insertTbListener);
btnUpdate = (Button)findViewById(R.id.btnUpdate);
btnUpdate.setOnClickListener(updateTbListener);
btnDelete = (Button)findViewById(R.id.btnDelete);
btnDelete.setOnClickListener(deleteTbListener);
}
public OnClickListener deleteTbListener = new OnClickListener() {
public void onClick(View v) {
DeleteTb();
}
};
public OnClickListener updateTbListener = new OnClickListener() {
public void onClick(View v) {
UpdateTb();
}
};
public OnClickListener insertTbListener = new OnClickListener() {
public void onClick(View v) {
InsertTb();
}
};
// public OnClickListener createDbListener = new OnClickListener() {
// public void onClick(View v) {
// CreateDatabase("TestDb01");
// }
// };
// public OnClickListener createTbListener = new OnClickListener() {
// public void onClick(View v) {
// CreateTable();
// }
// };
// /**
// * 新建一个数据库
// * @param dbName
// * @return
// */
// public SQLiteDatabase CreateDatabase(String dbName){
// dbHelper = new DBHelper(this, dbName);
// return dbHelper.getWritableDatabase();
// }
/**
* 新建一个表
* @param db
*/
public void CreateTable(){
db = dbHelper.getWritableDatabase();
String sql = "create table if not exists TestUsers"+
"(id int primary key,name varchar,sex varchar)";
try {
db.execSQL(sql);
} catch (SQLException e) {
Log.i("err", "create table failed");
}
}
/**
* 插入数据
*/
public void InsertTb(){
db = dbHelper.getWritableDatabase();
String sql = "insert into TestUsers (id,name,sex) values (2,‘hongguang‘,‘men‘)";
try {
db.execSQL(sql);
} catch (SQLException e) {
Log.i("err", "insert failed");
}
}
/**
* 更新数据
*/
public void UpdateTb() {
db = dbHelper.getWritableDatabase();
String sql = "Update TestUsers set name = ‘anhong‘,sex = ‘men‘ where id = 2";
try {
db.execSQL(sql);
} catch (SQLException e) {
Log.i("err", "update failed");
}
}
/**
* 删除数据
*/
public void DeleteTb(){
db = dbHelper.getWritableDatabase();
String sql = "delete from TestUsers where id = 2";
try {
db.execSQL(sql);
} catch (SQLException e) {
Log.i("err", "delete failed");
}
}
/**
* 打开数据库
*/
public void OpenDb(){
dbHelper = new DBHelper(this, "TestDb01");
db = dbHelper.getWritableDatabase();
}
/**
* 关闭数据库
*/
public void CloseDb(){
dbHelper.close();
}
@Override
protected void onDestroy() {
super.onDestroy();
if(db!=null){
db.close();
}
if(dbHelper!=null){
dbHelper.close();
}
}
}
5.一些SQLite操作命令
5.1 adb shell 进入命令模式
5.2 cd 文件名 进入文件
5.3 ls或ls -l 查看目录下的文件
5.4 sqlite3 数据库名 进入数据库
5.5 .schema 查看数据库下的信息
5.6 ctrl+d 退出sqlite模式