一、 SQLite介绍
SQLite是android内置的一个很小的关系型数据库。
SQLite的官网是http://www.sqlite.org/,可以去下载一些文档或相关信息。
博客中有一篇有稍微详细一点的介绍,大家可以去看一下。
二、 SQLiteOpenHelper的使用方法
SQLiteOpenHelper是一个辅助类来管理数据库的创建和版本。
可以通过继承这个类,实现它的一些方法来对数据库进行一些操作。
所有继承了这个类的类都必须实现下面这样的一个构造方法:
public DatabaseHelper(Context context, String name, CursorFactory factory, int version)
第一个参数:Context类型,上下文对象。
第二个参数:String类型,数据库的名称
第三个参数:CursorFactory类型
第四个参数:int类型,数据库版本
下面是这个类的几个方法:
方法名 返回类型 描述 备注
getReadableDatabase() synchronized SQLiteDatabase 创建或打开一个数据库 可以通过这两个方法返回的SQLiteDatabase对象对数据库进行一系列的操作,如新建一个表,插入一条数据等
getWritableDatabase() synchronized SQLiteDatabase 创建或打开一个可以读写的数据库
onCreate(SQLiteDatabase db) abstract void 第一次创建的时候调用
onOpen(SQLiteDatabase db) void 打开数据库
onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) abstract void 升级数据库
close() synchronized void 关闭所有打开的数据库对象
下面有一个例子,当点击按钮时进行相应的操作,效果图如下:
介于代码中有详细备注了,在此我就不多写了,直接贴代码了,代码如下:
DatabaseHelper类:
- package android.sqlite;
-
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
-
- public class DatabaseHelper extends SQLiteOpenHelper {
- private static final int VERSION = 1;
-
-
- public DatabaseHelper(Context context, String name, CursorFactory factory,
- int version) {
-
- super(context, name, factory, version);
- }
-
- public DatabaseHelper(Context context, String name, int version){
- this(context,name,null,version);
- }
-
- public DatabaseHelper(Context context, String name){
- this(context,name,VERSION);
- }
-
-
- @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("upgrade a database");
- }
- }
Activity类:
- package android.sqlite;
-
- import android.app.Activity;
- import android.content.ContentValues;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
-
- public class SQLiteActivity extends Activity {
-
- private Button createDatabaseButton = null;
- private Button updateDatabaseButton = null;
- private Button insertButton = null;
- private Button updateButton = null;
- private Button selectButton = null;
- private Button deleteButton = null;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- createDatabaseButton = (Button) findViewById(R.id.createDatabase);
- updateDatabaseButton = (Button) findViewById(R.id.updateDatabase);
- insertButton = (Button) findViewById(R.id.insert);
- updateButton = (Button) findViewById(R.id.update);
- selectButton = (Button) findViewById(R.id.select);
- deleteButton = (Button) findViewById(R.id.delete);
-
- createDatabaseButton
- .setOnClickListener(new CreateDatabaseOnClickListener());
- updateDatabaseButton
- .setOnClickListener(new UpdateDatabaseOnClickListener());
- insertButton.setOnClickListener(new InsertOnClickListener());
- updateButton.setOnClickListener(new UpdateOnClickListener());
- selectButton.setOnClickListener(new SelectOnClickListener());
- deleteButton.setOnClickListener(new DeleteOnClickListener());
- }
-
-
- class CreateDatabaseOnClickListener implements OnClickListener {
- public void onClick(View v) {
-
- DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,
- "test_yangyz_db");
-
- SQLiteDatabase sqliteDatabase = dbHelper.getReadableDatabase();
- }
- }
-
-
- class UpdateDatabaseOnClickListener implements OnClickListener {
- public void onClick(View v) {
-
- DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,
- "test_yangyz_db", 2);
-
- SQLiteDatabase sqliteDatabase = dbHelper.getReadableDatabase();
- }
-
- }
-
-
- class InsertOnClickListener implements OnClickListener {
- public void onClick(View v) {
-
- ContentValues values = new ContentValues();
-
- values.put("id", 1);
- values.put("name", "yangyz");
-
- DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,
- "test_yangyz_db", 2);
-
- SQLiteDatabase sqliteDatabase = dbHelper.getWritableDatabase();
-
-
-
-
- sqliteDatabase.insert("user", null, values);
- }
- }
-
-
- class UpdateOnClickListener implements OnClickListener {
- public void onClick(View v) {
-
- DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,
- "test_yangyz_db", 2);
-
- SQLiteDatabase sqliteDatabase = dbHelper.getWritableDatabase();
-
- ContentValues values = new ContentValues();
- values.put("name", "zhangsan");
-
-
-
-
-
- sqliteDatabase.update("user", values, "id=?", new String[] { "1" });
- System.out.println("-----------update------------");
- }
- }
-
-
- class SelectOnClickListener implements OnClickListener {
- public void onClick(View v) {
- String id = null;
- String name = null;
-
- DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,
- "test_yangyz_db", 2);
-
- SQLiteDatabase sqliteDatabase = dbHelper.getReadableDatabase();
-
-
-
-
-
-
-
-
- Cursor cursor = sqliteDatabase.query("user", new String[] { "id",
- "name" }, "id=?", new String[] { "1" }, null, null, null);
-
- while (cursor.moveToNext()) {
- id = cursor.getString(cursor.getColumnIndex("id"));
- name = cursor.getString(cursor.getColumnIndex("name"));
- }
- System.out.println("-------------select------------");
- System.out.println("id: "+id);
- System.out.println("name: "+name);
- }
- }
-
-
- class DeleteOnClickListener implements OnClickListener {
- public void onClick(View v) {
-
- DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this,"test_yangyz_db",2);
-
- SQLiteDatabase sqliteDatabase = dbHelper.getWritableDatabase();
-
-
-
-
- sqliteDatabase.delete("user", "id=?", new String[]{"1"});
- System.out.println("----------delete----------");
- }
- }
- }
布局文件:
- xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_attribute-value">"fill_parent"
- android:layout_attribute-value">"fill_parent"
- >
- <TextView
- android:layout_attribute-value">"fill_parent"
- android:layout_attribute-value">"wrap_content"
- android:text="@string/hello"
- />
-
- <Button
- android:id="@+id/createDatabase"
- android:layout_attribute-value">"fill_parent"
- android:layout_attribute-value">"wrap_content"
- android:text="@string/createDatabaseButton"
- />
-
- <Button
- android:id="@+id/updateDatabase"
- android:layout_attribute-value">"fill_parent"
- android:layout_attribute-value">"wrap_content"
- android:text="@string/updateDatabaseButton"
- />
-
- <Button
- android:id="@+id/insert"
- android:layout_attribute-value">"fill_parent"
- android:layout_attribute-value">"wrap_content"
- android:text="@string/insertButton"
- />
-
- <Button
- android:id="@+id/update"
- android:layout_attribute-value">"fill_parent"
- android:layout_attribute-value">"wrap_content"
- android:text="@string/updateButton"
- />
-
- <Button
- android:id="@+id/select"
- android:layout_attribute-value">"fill_parent"
- android:layout_attribute-value">"wrap_content"
- android:text="@string/selectButton"
- />
-
- <Button
- android:id="@+id/delete"
- android:layout_attribute-value">"fill_parent"
- android:layout_attribute-value">"wrap_content"
- android:text="@string/deleteButton"
- />
- LinearLayout>
String文件:
- xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, SQLiteActivity!string>
- <string name="app_name">SQLiteTeststring>
- <string name="createDatabaseButton">createDatabasestring>
- <string name="updateDatabaseButton">updateDatabasestring>
- <string name="insertButton">insertstring>
- <string name="updateButton">updatestring>
- <string name="selectButton">selectstring>
- <string name="deleteButton">deletestring>
- resources>
项目源码已上传,有需要的可下载。