SQLite 数据库操作类

前端之家收集整理的这篇文章主要介绍了SQLite 数据库操作类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
GoDB {

  private Database database;
	private sqliteDatabase db;

	public GoosunDB(Context context) {
		super();
		database = new Database(context);
	}

	public GoosunDB open() throws sqlException {
		db = database.getWritableDatabase();
		return this;
	}

	public void close() {
		db.close();
	}
	
	public Cursor search(int mark) {
		Cursor cursor = db.query(TABLE_SEARCH,new String[] { COL_id,COL_code,COL_name },"mark =" + mark,null,COL_date
				+ " desc");
		if (cursor != null) {
			cursor.moveToFirst();
		}
		return cursor;
	}

	public Cursor searchByCode(String string) {
		Cursor cursor = db.query(TABLE_SEARCH,new String[] { COL_id },"gid = " + "'" + string + "'",null);
		if (cursor != null) {
			cursor.moveToFirst();
		}
		return cursor;
	}

	public long insertEntity(SearchEntity entity) {
		
		Cursor cursor = searchByCode(entity.getCode());

		if (cursor != null && cursor.moveToFirst()) {
			return updateEntity(entity);
		} else {
			ContentValues values = new ContentValues();
			values.put(COL_code,entity.getCode());
			values.put(COL_name,entity.getName());
			values.put(COL_mark,entity.getMark());
			values.put(COL_date,new Date().toString());
			return db.insert(TABLE_SEARCH,values);
		}
	}

	public long updateEntity(SearchEntity entity) {
		ContentValues values = new ContentValues();
		values.put(COL_date,new Date().toString());
		return db.update(TABLE_SEARCH,values,"_id=?",new String[] { String.valueOf(entity.getId()) });
	}

	public long deleteEntity(int mark) {
		return db.delete(TABLE_SEARCH,"mark=?",new String[] { String.valueOf(mark) });
	}
	
	private class Database extends sqliteOpenHelper {

		public Database(Context context) {
			super(context,DATABASE_NAME,DATABASE_VERSION);
			
		}

		@Override
		public void onCreate(sqliteDatabase db) {
			
			db.execsql(TABLE_InsideLetter);
			db.execsql(TABLE_CREATE);
		}

		@Override
		public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
			
			db.execsql("DROP TABLE IF EXISTS" + TABLE_SEARCH);
			db.execsql("DROP TABLE IF EXISTS" + TABLE_Inside);
			onCreate(db);
		}
	}
}
原文链接:https://www.f2er.com/sqlite/199774.html

猜你在找的Sqlite相关文章