SQLiteDatabase操作

ContentValues类:

ContentValues 类和Hashmap/Hashtable 比较类似,它也是负责存储一些名值对,但是它存储的名值对当中的名是一个String 类型,而值都是基本类型

Cursor类:

Cursor 是每行的集合。
使用 moveToFirst() 定位第一行。
p  close() //关闭游标,释放资源
p copyStringToBuffer(int columnIndex, CharArrayBuffer buffer)
在缓冲区中检索请求的列的文本,将将其存储
p getColumnCount()//返回所有列的总数
p getColumnIndex(String columnName)
返回指定列的名称,如果不存在返回-1
p getColumnIndexOrThrow(String columnName)
从零开始返回指定列名称,如果不存在将抛出IllegalArgumentException 异常。
p getColumnName(int columnIndex)//从给定的索引返回列名
p getColumnNames()//返回一个字符串数组的列名
p getCount()//返回Cursor 中的行数
p moveToFirst()//移动光标到第一行
p moveToLast()//移动光标到最后一行
p moveToNext()//移动光标到下一行
p moveToPosition(int position)//移动光标到一个绝对的位置
p moveToPrevious()//移动光标到上一行


public class DBAdapter {

	private static final String DB_NAME = "people.db";
	private static final String DB_TABLE = "peopleinfo";
	private static final int DB_VERSION = 1;
	 
	//表格的列名
	public static final String KEY_ID = "_id";
	public static final String KEY_NAME = "name";
	public static final String KEY_AGE = "age";
	public static final String KEY_HEIGHT = "height";
	
	private SQLiteDatabase db;
	private final Context context;
	private DBOpenHelper dbOpenHelper;
	
	public DBAdapter(Context _context) {
	    context = _context;
	  }

	  /** Close the database */
	  public void close() {
		  if (db != null){
			  db.close();
			  db = null;
		  }
		}

	  /** Open the database */
	  public void open() throws SQLiteException {  
		  dbOpenHelper = new DBOpenHelper(context, DB_NAME, null, DB_VERSION);
		  try {
			  db = dbOpenHelper.getWritableDatabase();
		  }
		  catch (SQLiteException ex) {
			  db = dbOpenHelper.getReadableDatabase();
		  }	  
		}
	  
	
	  public long insert(People people) {
	    ContentValues newValues = new ContentValues();
	  
	    newValues.put(KEY_NAME, people.Name);
	    newValues.put(KEY_AGE, people.Age);
	    newValues.put(KEY_HEIGHT, people.Height);
	    
	    return db.insert(DB_TABLE, null, newValues);
	  }


	  public People[] queryAllData() {  
		  Cursor results =  db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_AGE, KEY_HEIGHT}, 
				  null, null, null, null, null);
		  return ConvertToPeople(results);   
	  }
	  
	  public People[] queryOneData(long id) {  
		  Cursor results =  db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_AGE, KEY_HEIGHT}, 
				  KEY_ID + "=" + id, null, null, null, null);
		  return ConvertToPeople(results);   
	  }
	  
	  private People[] ConvertToPeople(Cursor cursor){
		  int resultCounts = cursor.getCount();
		  if (resultCounts == 0 || !cursor.moveToFirst()){
			  return null;
		  }
		  People[] peoples = new People[resultCounts];
		  for (int i = 0 ; i<resultCounts; i++){
			  peoples[i] = new People();
			  peoples[i].ID = cursor.getInt(0);
			  peoples[i].Name = cursor.getString(cursor.getColumnIndex(KEY_NAME));
			  peoples[i].Age = cursor.getInt(cursor.getColumnIndex(KEY_AGE));
			  peoples[i].Height = cursor.getFloat(cursor.getColumnIndex(KEY_HEIGHT));
			  
			  cursor.moveToNext();
		  }	  
		  return peoples; 
	  }
	  
	  public long deleteAllData() {
		  return db.delete(DB_TABLE, null, null);
	  }

	  public long deleteOneData(long id) {
		  return db.delete(DB_TABLE,  KEY_ID + "=" + id, null);
	  }

	  public long updateOneData(long id , People people){
		  ContentValues updateValues = new ContentValues();	  
		  updateValues.put(KEY_NAME, people.Name);
		  updateValues.put(KEY_AGE, people.Age);
		  updateValues.put(KEY_HEIGHT, people.Height);
		  
		  return db.update(DB_TABLE, updateValues,  KEY_ID + "=" + id, null);
	  }
	  
		/** 静态Helper类,用于建立、更新和打开数据库*/
	  private static class DBOpenHelper extends SQLiteOpenHelper {

		  public DBOpenHelper(Context context, String name, CursorFactory factory, int version) {
		    super(context, name, factory, version);
		  }

		  private static final String DB_CREATE = "create table " + 
		    DB_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " +
		    KEY_NAME+ " text not null, " + KEY_AGE+ " integer," + KEY_HEIGHT + " float);";

		  @Override
		  public void onCreate(SQLiteDatabase _db) {
		    _db.execSQL(DB_CREATE);
		  }

		  @Override
		  public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {		    
		    _db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
		    onCreate(_db);
		  }
		  
		}
	}


多重条件选择

public People[] select2(){
		  //String table="DB_TABLE";
		  String[] columns = new String[] { KEY_ID,KEY_NUM, KEY_NAME, KEY_ADDRESS, KEY_SPECIALTY};
		  String selection = "_id>?"+"and Address=?";  
		  String[] selectionArgs = new String[]{"12","北京"};  
		  String groupBy = null;  
		  String having = null;  
		  String orderBy = null;   		  
		Cursor results =  db.query(DB_TABLE, columns, selection, selectionArgs, groupBy, having, orderBy, null);
		return ConvertToPeople(results);
	  }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值