Android手机开发:SQLite数据库

1. 定义一个类,方便存储和获取数据

package com.eoeAndroid.SQLite;


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 DataDbAdapter {
	public static final String KEY_COL = "col";
	public static final String KEY_TEL = "telephone";
	
	private static final String TAG = "DataDbAdapter";
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;
	
	private static final String DATABASE_CREATE = "create table tlist (col text primary key, "
			+ "telephone text not null);";

	private static final String DATABASE_NAME = "dataBaseMM";
	private static final String DATABASE_TABLE = "tlist";
	private static final int DATABASE_VERSION = 1;
	
	private final Context mCtx;
	
	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS list");
			onCreate(db);
		}
	}
	
	public DataDbAdapter(Context ctx) {
		this.mCtx = ctx;
	}
	
	public DataDbAdapter open()  {
		try {
			mDbHelper = new DatabaseHelper(mCtx);
			mDb = mDbHelper.getWritableDatabase();
		}catch(SQLException e) {
			Log.i("TTTTT", "尼玛啊,没有建立成功!");
		}
		
		return this;
	}

	public void closeclose() {
		mDbHelper.close();
	}
	
	public long createDiary(String col, String tel) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_COL, col);
		initialValues.put(KEY_TEL, tel);
//		String strSql = "insert into " + DATABASE_TABLE + " (" + KEY_COL + ", " + KEY_TEL
//				+ ") values(col, tel);";
//		mDb.execSQL(strSql);
		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}
	
	public boolean deleteDiary(String col) {
		return mDb.delete(DATABASE_TABLE, KEY_COL + "=" + col, null) > 0;
	}
	
	public Cursor getAllNotes() {
		return mDb.query(DATABASE_TABLE, new String[] { KEY_COL, KEY_TEL
				}, null, null, null, null, null);
	}
	
	public Cursor getDiary(String col) throws SQLException {

		Cursor mCursor =

		mDb.query(true, DATABASE_TABLE, new String[] { KEY_COL, KEY_TEL}, 
				KEY_COL + "=" + col, null, null,	null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	public boolean updateDiary(String col, String tel) {
		ContentValues args = new ContentValues();
		args.put(KEY_TEL, tel);

		return mDb.update(DATABASE_TABLE, args, KEY_COL + "=" + col, null) > 0;
	}

}

2. 使用上面的类,创建对象

private DataDbAdapter mDbHelper;
mDbHelper = new DataDbAdapter(this);
mDbHelper.open();
open函数创建或打开数据库

3. 存储数据

//col和tel是我们要存储的一条记录中的2列数据
mDbHelper.createDiary(col, tel);

4. 读取数据,使用Cursor

private Cursor mDataCursor;
mDataCursor = mDbHelper.getAllNotes();
startManagingCursor(mDataCursor);
private List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
//必须先清除
items.clear();
Map<String, Object> map = null;
for(int i=0; i<mDataCursor.getCount(); i++){	
	mDataCursor.moveToPosition(i);
	map = new HashMap<String, Object>();
	map.put("sellitem_1", mDataCursor.getString(0));
	map.put("sellitem_2", mDataCursor.getString(1));
	items.add(map);					
}




你可能感兴趣的:(android,sqlite,String,null,database,手机)