1.Sqlite的介绍
www.sqlite.org
mini版的关系型数据库
2. Sqlite的使用步骤
* 继承SQLiteOpenHelper类,编写自己的DatabaseHelper
package mars.sqlite.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
/*
* DatabaseHelper 作为一个访问SQLite的助手类,提供两个方面的功能:
*
* 第一, getReadableDatabase(), getWritableDatabase()可以获得SQLiteDatabase对象,通过该对象可以对数据库进行操作
*
* 第二, 提供了onCreate()和onUpgrade()两个回调函数,允许我们在创建和升级数据库时,进行自己的操作
* */
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int VERSION = 1;
//在SQLiteOpenHelper的子类中,必须有该构造函数
//context 是一个Activity对象
//name是数据表的名字
public DatabaseHelper(Context context, String name, CursorFactory factory,
int version) {
//必须通过super调用父类的构造函数
super(context, name, factory, version);
}
public DatabaseHelper(Context context, String name) {
this(context, name, VERSION);
}
public DatabaseHelper(Context context, String name, int version) {
super(context, name, null, version);
}
//该函数是在第一次创建数据库的时候执行的,实际上是在第一次得到SQLiteDatabase对象时才会调用这个方法
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("create a Database");
db.execSQL("create table user(id int,name varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
System.out.println("update a Database");
}
}
* 创建或打开数据库
// 创建或打开一个DatabaseHelper对象
DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,
"test_sqlitedb");
// 只有调用了DatabaseHelper对象的getReadableDatabase()或getWritableDatabase()方法,才能创建数据库
SQLiteDatabase db = dbHelper.getReadableDatabase();
* 增删改查操作
package mars.sqlite;
import mars.sqlite.db.DatabaseHelper;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SQLiteActivity extends Activity {
private Button createButton = null;
private Button insertButton = null;
private Button updateButton = null;
private Button queryButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createButton = (Button) findViewById(R.id.createButton);
createButton.setOnClickListener(new CreateListener());
updateButton = (Button) findViewById(R.id.updateeButton);
updateButton.setOnClickListener(new UpdateListener());
insertButton = (Button) findViewById(R.id.insertButton);
insertButton.setOnClickListener(new InsertListener());
queryButton = (Button) findViewById(R.id.queryButton);
queryButton.setOnClickListener(new QueryListener());
}
class CreateListener implements OnClickListener {
@Override
public void onClick(View arg0) {
// 创建或打开一个DatabaseHelper对象
DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,
"test_sqlitedb");
// 只有调用了DatabaseHelper对象的getReadableDatabase()方法,或者getWritableDatabase()方法,才能创建数据库
SQLiteDatabase db = dbHelper.getReadableDatabase();
}
}
// 更新操作相当于SQL语句中的update语句
// update table_name set XXCol=XXX where XXcol=XX
class UpdateListener implements OnClickListener {
@Override
public void onClick(View v) {
DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,
"test_sqlitedb", 2);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "张 san feng");
// 第一个参数是表名,第二个参数是ContentValues对象
// 第三个参数是where子句,?是占位符,有几个占位符,第四个参数数组就有多长
db.update("user", values, "id=?", new String[] { "1" });
}
}
class InsertListener implements OnClickListener {
@Override
public void onClick(View v) {
// contentValues对象承载要插入数据库的数据
ContentValues values = new ContentValues();
values.put("id", 1);
values.put("name", "zzq");
DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,
"test_sqlitedb");
SQLiteDatabase db = dbHelper.getWritableDatabase();
// 调用insert方法,将数据插入数据库
db.insert("user", null, values);
}
}
class QueryListener implements OnClickListener {
@Override
public void onClick(View v) {
Log.d("","");
DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,
"test_sqlitedb");
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("user", new String[] { "id", "name" },
"id=?", new String[] { "1" }, "", "", "");
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
System.out.println("query--->" + name);
}
}
}
}
3.使用adb访问Sqlite
* Dos下运行adb shell命令
* 进入/data/data/mars.sqlite/目录
* 命令: sqlite3 dbname 进入sqlite命令
* 命令: .schmea 显示当前数据库的数据表
* 在sqlite命令基本和正常的sql命令一样,如查询操作: select * from user wher id = 1