日期:2014-05-16  浏览次数:20500 次

Android学习之创建和使用数据库

    Android使用的是SQLite数据库系统,为一个应用程序所创建的数据库只能被此应用程序访问,其他应用程序不能访问!

   下面的程序片段将说明如何以编程的方式在Android应用程序中创建一个SQLite数据库。对于Android,在一个应用程序中以编程方式创建的SQLite数据库总是存储在/data/data/<pakage_name>/databases文件夹下。

  1. 创建数据库辅助类

      

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 DBAdapter {

	static final String KEY_ROWID = "_id";
	static final String KEY_NAME = "name";
	static final String KEY_EMAIL = "email";
	static final String TAG = "DBAdapter";
	
	static final String DATABASE_NAME = "MyDB";
	static final String DATABASE_TABLE = "contacts";
	static final int DATABASE_VERSION = 1;
	
	static final String DATABASE_CREATE = 
			"create table contacts( _id integer primary key autoincrement, " + 
			"name text not null, email text not null);";
	final Context context;
	
	DatabaseHelper DBHelper;
	SQLiteDatabase db;
	
	public DBAdapter(Context cxt)
	{
		this.context = cxt;
		DBHelper = new DatabaseHelper(context);
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper
	{

		DatabaseHelper(Context context)
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			try
			{
				db.execSQL(DATABASE_CREATE);
			}
			catch(SQLException e)
			{
				e.printStackTrace();
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			Log.wtf(TAG, "Upgrading database from version "+ oldVersion + "to "+
			 newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS contacts");
			onCreate(db);
		}
	}
	
	//open the database
	public DBAdapter open() throws SQLException
	{
		db = DBHelper.getWritableDatabase();
		return this;
	}
	//close the database
	public void close()
	{
		DBHelper.close();
	}
	
	//insert a contact into the database
	public long insertContact(String name, String email)
	{
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_NAME, name);
		initialValues.put(KEY_EMAIL, email);
		return db.insert(DATABASE_TABLE, null, initialValues);
	}
	//delete a particular contact
	public boolean deleteContact(long rowId)
	{
		return db.delete(DATABASE_TABLE, KEY_ROWID + "=" +rowId, null) > 0;
	}
	//retreves all the contacts
	public Cursor getAllContacts()
	{
		return db.query(DATABASE_TABLE, new String[]{ KEY_ROWID,KEY_NAME,KEY_EMAIL}, null, null, null, null, null);
	}
	//retreves a particular contact
	public Cursor getContact(long rowId) throws SQLException
	{
		Cursor mCursor = 
				db.query(true, DATABASE_TABLE, new String[]{ KEY_ROWID,
						 KEY_NAME, KEY_EMAIL}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
		if (mCursor != null)
			mCursor.moveToFirst();
		return mCursor;
	}
	//updates a contact
	public boolean updateContact(long rowId, String name, String email)
	{
		ContentValues args = new ContentValues();
		args.put(KEY_NAME, name);
		args.put(KEY_EMAIL, email);
		return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" +rowId, null) > 0;
	}
}

每个类的作用已经做了说明。本例子中创建一个名为MyDB的数据库,包含一个名为contacts的表,这个表有3个列:_id、name和email。

    2、以编程的方式使用数据库

     
package com.wshouyou.database;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.view.Menu;
import