安卓数据库查询细节

// 判断users表中的是否包含某个UserID的记录
	public UserInfo GetUserByName(String userName) {
		Boolean b = false;
		// 注意汉字为查询条件时需要加''
		Cursor cursor = db.query(SqliteHelper.TB_NAME, null, UserInfo.USERNAME
				+ "='" + userName + "'", null, null, null, null);
		b = cursor.moveToFirst();
		Log.e("GetUserByName", b.toString());
		if (b != false) {
			UserInfo user = new UserInfo();
			user.setId(cursor.getString(0));
			user.setUserId(cursor.getString(1));
			user.setToken(cursor.getString(2));
			user.setTokenSecret(cursor.getString(3));
			user.setUserName(cursor.getString(4));
			ByteArrayInputStream stream = new ByteArrayInputStream(
					cursor.getBlob(5));
			Drawable icon = Drawable.createFromStream(stream, "image");
			user.setUserIcon(icon);
			cursor.close();
			return user;
		}
		return null;
	}

特别注意如果查询条件为汉字时,需要加 ' '   如:Cursor cursor = db.query(SqliteHelper.TB_NAME, null, UserInfo.USERNAME + "='" + userName + "'", null, null, null, null);
另外,如果需要在数据库中创建图片信息是三步:

ByteArrayInputStream stream = new ByteArrayInputStream( cursor.getBlob(5) );

Drawable icon = Drawable.createFromStream(stream, "image");

user.setUserIcon(icon);



你可能感兴趣的:(安卓数据库查询细节)