一个操作SQLite数据库的例子

public class User {
	private int id;
	private String name;
	private int age;
	
	public User(int id) {
		this.id = id;		
	}
	// getter and setter
}

public class UserDbSchema {
	
	public static final class UserTable {
		public static final String NAME = "user";
	}
	
	public static final class Cols {
		public static final String ID = "id";
		public static final String NAME = "name";
		public static final String AGE = "age";
	}
}

public class UserBaseHelper extends SQLiteOpenHelper {

	private static final int VERSION = 1;
	private static final String DATABASE_NAME = "user.db";
	
	public UserBaseHelper(Context context) {
		super(context, DATABASE_NAME, null, VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + UserTable.NAME + "(" +
			UserTable.Cols.ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
			UserTable.Cols.NAME + "TEXT, " +
			UserTable.Cols.AGE + "INTEGER"
			")"
		);
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
	}
}

public class UserLab {
	private static UserLab sUserLab;
	private Context mContext;
	private SQLiteDatabase mDatabase;
	
	public static UserLab get(Context context) {
		if (sUserLab == null) {
			sUserLab = new UserLab(context);			
		}
		return sUserLab;
	}

	private UserLab(Context context) {
		mContext = context.getApplicationContext();
		mDatabase = new UserBaseHelper(mContext).getWritableDatabase();
	}
	
	public void addUser(User user) {
		ContentValues values = getContentValues(user);
		
		mDatabase.insert(UserTable.NAME, null, values);
	}
	
	public List<User> getUsers() {
		List<User> users = new ArrayList<>();
		
		UserCursorWrapper cursor = queryUsers(null, null);
		
		try {
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				users.add(cursor.getCrime());
				cursor.moveToNext();
			}
		} finally {
			cursor.close();
		}
		
		return users;
	}
	
	public User getUser(int id) {
		UserCursorWrapper cursor = queryUser(
			UserTable.Cols.ID + " = ?",
			new String[] { id }
		);
		
		try {
			if (cursor.getCount() == 0) {
				return null;
			}
			
			cursor.moveToFirst();
			return cursor.getCrime();
		finally {
			cursor.close();
		}
	}
	
	public void updateUser(User user) {
		int id = user.getId();
		ContentValues values = getContentValues(user);
		
		mDatabase.update(UserTable.NAME, values,
			UserTable.Cols.ID + " = ?",
			new String[] { id });
	}
	
	private static ContentValues getContentValues(User user) {
		ContentValues values = new ContentValues();
		values.put(UserTable.Cols.ID, user.getId());
		values.put(UserTable.Cols.NAME, user.getName());
		values.put(UserTable.Cols.AGE, user.getAge());
		
		return values;
	}
	
	private UserCursorWrapper queryCrimes(String whereClause, String[] whereArgs) {
		Cursor cursor = mDatabase.query(
			UserTable.NAME,
			null, // Colums - null selects all colums
			whereClause,
			whereArgs,
			null, // group by
			null, // having
			null  // gorderBy
		);
		
		return new UserCursorWrapper(cursor);
	}
}

public class UserCursorWrapper extends CursorWrapper {
	
	public UserCursorWrapper(Cursor cursor) {
		super(cursor);
	}
	
	public User getUser() {
		int id = getInt(getColumnIndex(UserTable.Cols.ID));
		String name = getString(getColumnIndex(UserTable.Cols.NAME));
		String name = getInt(getColumnIndex(UserTable.Cols.AGE));
		
		User user = new User(id);
		user.setName(name);
		user.setAge(age);
		
		return user;
	}
}
posted @ 2016-10-17 23:14  勇敢的少年啊  阅读(379)  评论(0编辑  收藏  举报