经常用得到的安卓数据库基类_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 经常用得到的安卓数据库基类

经常用得到的安卓数据库基类

 2014/10/18 10:42:20  xiaochao1234  程序员俱乐部  我要评论(0)
  • 摘要:Java代码//创建数据库publicclassDBCreate{publicstaticvoidCreateDatabase(SQLiteDatabasedb){db.beginTransaction();try{create_NetTaskBuffer(db);insert_SQL(db);db.setTransactionSuccessful();}catch(Exceptione){e.printStackTrace();}finally{db.endTransaction();}
  • 标签:常用 数据库 数据
Java代码 javascripts/syntaxhighlighter/clipboard_new.swf" type="application/x-shockwave-flash">class%20DBCreate%20%7B%0A%09public%20static%20void%20CreateDatabase(SQLiteDatabase%20db)%20%7B%0A%09%09db.beginTransaction()%3B%0A%09%09try%20%7B%0A%09%09%09create_NetTaskBuffer(db)%3B%0A%0A%09%09%09insert_SQL(db)%3B%0A%0A%09%09%09db.setTransactionSuccessful()%3B%0A%09%09%7D%20catch%20(Exception%20e)%20%7B%0A%09%09%09e.printStackTrace()%3B%0A%09%09%7D%20finally%20%7B%0A%09%09%09db.endTransaction()%3B%0A%09%09%7D%0A%09%7D%0A%0A%09%2F%2F%20%E7%BC%93%E5%AD%98%0A%09private%20static%20void%20create_NetTaskBuffer(SQLiteDatabase%20db)%20%7B%0A%09%09db.execSQL(%22DROP%20TABLE%20IF%20EXISTS%20NetTaskBuffer%22)%3B%0A%09%09StringBuilder%20sql%20%3D%20new%20StringBuilder()%3B%0A%09%09sql.append(%22CREATE%20TABLE%20IF%20NOT%20EXISTS%20NetTaskBuffer%20(%22)%3B%0A%09%09sql.append(%22%20id%20INTEGER%20PRIMARY%20KEY%20AUTOINCREMENT%2C%22)%3B%20%2F%2F%20%E8%BF%99%E4%B8%80%E5%88%97%E4%B8%8D%E8%83%BD%E4%BF%AE%E6%94%B9%0A%09%09sql.append(%22%20label%20TEXT%20COLLATE%20NOCASE%2C%22)%3B%20%2F%2F%0A%09%09sql.append(%22%20param%20TEXT%20COLLATE%20NOCASE%2C%22)%3B%20%2F%2F%0A%09%09sql.append(%22%20result%20TEXT%20COLLATE%20NOCASE%2C%22)%3B%20%2F%2F%0A%09%09sql.append(%22%20remark%20TEXT%20COLLATE%20NOCASE%2C%22)%3B%0A%09%09sql.append(%22%20time%20LONG)%22)%3B%20%2F%2F%0A%09%09db.execSQL(sql.toString())%3B%0A%09%7D%0A%0A%09%2F%2F%20%E6%8F%92%E5%85%A5%E8%AF%AD%E5%8F%A5%2C%E5%88%9D%E5%A7%8B%E5%8C%96%E6%95%B0%E6%8D%AE%E5%BA%93%E4%BD%BF%E7%94%A8%0A%09private%20static%20void%20insert_SQL(SQLiteDatabase%20db)%20%7B%0A%09%09%0A%09%7D%0A%7D%0A%0A%0A%0A%2F%2F----------------------%E6%95%B0%E6%8D%AE%E5%BA%93%E6%93%8D%E4%BD%9C%E5%9F%BA%E7%B1%BB--------------------------------------%2F%2F%0A%0A%0A%2F**%0A%20*%20%E6%95%B0%E6%8D%AE%E5%BA%93%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C%E7%B1%BB%2C%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E5%88%9B%E5%BB%BA%2C%E6%9B%B4%E6%96%B0%E7%9A%84%E6%93%8D%E4%BD%9C%E9%83%BD%E5%9C%A8%E8%BF%99%E9%87%8C%E8%BF%9B%E8%A1%8C%0A%20*%20%0A%20*%20%40author%20yizhe%0A%20*%20%40date%202014-9-11%0A%20*%2F%0Apublic%20abstract%20class%20DBHelper%20extends%20SQLiteOpenHelper%20%7B%0A%0A%09static%20String%20name%20%3D%20%22hk.db%22%3B%20%2F%2F%20%E6%95%B0%E6%8D%AE%E5%BA%93%E5%90%8D%E7%A7%B0%0A%09static%20CursorFactory%20cursorFactory%20%3D%20null%3B%0A%09static%20int%20version%20%3D%201000%3B%2F%2F%20%E5%88%9D%E5%A7%8B%E7%89%88%E6%9C%AC%3A1000%0A%09Context%20context%3B%0A%0A%09protected%20ContentValues%20contentValues%3B%20%2F%2F%20cursor%E5%AF%B9%E8%B1%A1%E8%BD%AC%E8%A1%8C%E4%B8%AD%E4%BB%8B%2C%E7%BB%99%E5%AD%90%E7%B1%BB%E4%BD%BF%E7%94%A8%0A%0A%09protected%20String%20tableName%3B%0A%0A%09protected%20DBHelper(Context%20context%2C%20String%20tableName)%20%7B%0A%09%09super(context%2C%20name%2C%20cursorFactory%2C%20version)%3B%0A%09%09this.context%20%3D%20context%3B%0A%09%09this.tableName%20%3D%20tableName%3B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E8%BD%AF%E4%BB%B6%E7%AC%AC%E4%B8%80%E6%AC%A1%E5%AE%89%E8%A3%85%E7%9A%84%E6%97%B6%E5%80%99%E4%BC%9A%E8%B0%83%E7%94%A8%2C%E8%A6%86%E7%9B%96%E5%AE%89%E8%A3%85%E4%B8%8D%E4%BC%9A%E8%B0%83%E7%94%A8%0A%09%20*%2F%0A%09public%20void%20onCreate(SQLiteDatabase%20db)%20%7B%0A%09%09%2F%2F%20%E6%89%80%E6%9C%89%E8%A1%A8%E7%9A%84%E5%88%9B%E5%BB%BA%E8%BF%87%E7%A8%8B%E9%83%BD%E5%9C%A8%E8%BF%99%E9%87%8C%E8%BF%9B%E8%A1%8C%0A%09%09DBCreate.createDatabase(db)%3B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E8%A6%86%E7%9B%96%E5%AE%89%E8%A3%85%2C%E5%BD%93%E7%89%88%E6%9C%AC%E5%8F%B7version%E5%8F%91%E7%94%9F%E5%8F%98%E5%8C%96%E7%9A%84%E6%97%B6%E5%80%99%2C%E8%BF%99%E4%B8%AA%E6%96%B9%E6%B3%95%E6%89%8D%E4%BC%9A%E8%A2%AB%E8%B0%83%E7%94%A8%2C%E8%80%8C%E4%B8%94%E5%8F%AA%E6%89%A7%E8%A1%8C%E4%B8%80%E6%AC%A1%0A%09%20*%2F%0A%09public%20void%20onUpgrade(SQLiteDatabase%20db%2C%20int%20oldVersion%2C%20int%20newVersion)%20%7B%0A%09%09onCreate(db)%3B%0A%09%09%2F%2F%20if%20(oldVersion%20%3C%20109)%20%7B%0A%09%09%2F%2F%20onCreate(db)%3B%0A%09%09%2F%2F%20%7D%20else%20%7B%0A%09%09%2F%2F%20%7D%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E6%AF%8F%E6%AC%A1%E6%88%90%E5%8A%9F%E6%89%93%E5%BC%80%E6%95%B0%E6%8D%AE%E5%BA%93%E5%90%8E%E9%A6%96%E5%85%88%E8%A2%AB%E6%89%A7%E8%A1%8C%0A%09%20*%2F%0A%09public%20void%20onOpen(SQLiteDatabase%20db)%20%7B%0A%09%09super.onOpen(db)%3B%20%2F%2F%20%E6%AF%8F%E6%AC%A1%E6%88%90%E5%8A%9F%E6%89%93%E5%BC%80%E6%95%B0%E6%8D%AE%E5%BA%93%E5%90%8E%E9%A6%96%E5%85%88%E8%A2%AB%E6%89%A7%E8%A1%8C%0A%09%7D%0A%0A%09public%20void%20finalize()%20%7B%0A%09%09close()%3B%0A%09%7D%0A%0A%09%2F%2F%20--------------------------sql%E6%96%B9%E6%B3%95---------------------------------%2F%2F%0A%09%2F**%0A%09%20*%20%E6%89%A7%E8%A1%8Csql%2C%E6%B2%A1%E6%9C%89%E8%BF%94%E5%9B%9E%0A%09%20*%2F%0A%09public%20void%20execSQL(String%20sql)%20%7B%0A%09%09System.out.println(%22%3D%3DexecSQL%3D%3D%22%20%2B%20sql)%3B%0A%09%09SQLiteDatabase%20db%20%3D%20getWritableDatabase()%3B%0A%09%09db.execSQL(sql)%3B%0A%09%09db.close()%3B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E6%89%B9%E9%87%8F%E6%89%A7%E8%A1%8Csql%2C%E5%86%85%E9%83%A8%E5%90%AF%E7%94%A8%E4%BA%8B%E5%8A%A1%0A%09%20*%20%0A%09%20*%20%40param%20list%0A%09%20*%20%20%20%20%20%20%20%20%20%20%20%20sql%E5%88%97%E8%A1%A8%0A%09%20*%20%40return%20%E6%98%AF%E5%90%A6%E6%89%A7%E8%A1%8C%E6%88%90%E5%8A%9F%0A%09%20*%2F%0A%09public%20boolean%20execSQLBatch(ArrayList%3CString%3E%20list)%20%7B%0A%09%09SQLiteDatabase%20db%20%3D%20getWritableDatabase()%3B%0A%09%09db.beginTransaction()%3B%0A%09%09try%20%7B%0A%09%09%09for%20(String%20sql%20%3A%20list)%20%7B%0A%09%09%09%09db.execSQL(sql)%3B%0A%09%09%09%7D%0A%09%09%09db.setTransactionSuccessful()%3B%0A%09%09%7D%20catch%20(Exception%20e)%20%7B%0A%09%09%09e.printStackTrace()%3B%0A%09%09%09return%20false%3B%0A%09%09%7D%20finally%20%7B%0A%09%09%09db.endTransaction()%3B%0A%09%09%09db.close()%3B%0A%09%09%7D%0A%09%09return%20true%3B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E6%A0%B9%E6%8D%AEid%E5%88%A0%E9%99%A4%E8%AE%B0%E5%BD%95%0A%09%20*%20%0A%09%20*%20%40param%20id%0A%09%20*%20%20%20%20%20%20%20%20%20%20%20%20%E8%A1%A8%E4%B8%AD%E5%BF%85%E9%A1%BB%E6%9C%89%22id%22%E5%AD%97%E6%AE%B5%0A%09%20*%20%40return%20the%20number%20of%20rows%20affected%20if%20a%20whereClause%20is%20passed%20in%2C%200%0A%09%20*%20%20%20%20%20%20%20%20%20otherwise.%20To%20remove%20all%20rows%20and%20get%20a%20count%20pass%20%221%22%20as%20the%0A%09%20*%20%20%20%20%20%20%20%20%20whereClause.%0A%09%20*%2F%0A%09public%20int%20delete(int%20id)%20%7B%0A%09%09SQLiteDatabase%20db%20%3D%20getWritableDatabase()%3B%0A%09%09int%20result%20%3D%20db.delete(tableName%2C%20%22id%3D%3F%22%2C%20new%20String%5B%5D%20%7B%20id%20%2B%20%22%22%20%7D)%3B%0A%09%09db.close()%3B%0A%09%09return%20result%3B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E5%88%A0%E9%99%A4%3A%20%E5%8F%AA%E9%9C%80%E8%A6%81%E5%86%99where%20%E6%9D%A1%E4%BB%B6(%E4%B8%8D%E5%B8%A6where)%20%E5%92%8C%20%E5%8F%82%E6%95%B0%0A%09%20*%20%0A%09%20*%20%40param%20whereStr%0A%09%20*%20%20%20%20%20%20%20%20%20%20%20%20%E4%BE%8B%E5%A6%82%3A%20%22id%3D%3F%22%0A%09%20*%20%40param%20arr%0A%09%20*%20%20%20%20%20%20%20%20%20%20%20%20%E4%BE%8B%E5%A6%82%3A%20new%20String%5B%5D%20%7B%20%22123%22%20%7D%0A%09%20*%20%40return%20%E5%8F%97%E5%BD%B1%E5%93%8D%E7%9A%84%E8%A1%8C%0A%09%20*%2F%0A%09public%20int%20delete(String%20whereStr%2C%20String%5B%5D%20arr)%20%7B%0A%09%09SQLiteDatabase%20db%20%3D%20getWritableDatabase()%3B%0A%09%09int%20result%20%3D%20db.delete(tableName%2C%20whereStr%2C%20arr)%3B%0A%09%09db.close()%3B%0A%09%09return%20result%3B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E6%B8%85%E7%A9%BA%E8%A1%A8%0A%09%20*%2F%0A%09public%20void%20clearTable()%20%7B%0A%09%09SQLiteDatabase%20db%20%3D%20getWritableDatabase()%3B%0A%09%09db.execSQL(%22delete%20from%20%22%20%2B%20tableName)%3B%0A%09%09db.close()%3B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E9%80%9A%E7%94%A8%E6%9F%A5%E8%AF%A2%2C%E5%A4%9A%E7%94%A8%E4%BA%8E%E4%BA%8B%E5%8A%A1%E4%B8%AD%0A%09%20*%2F%0A%09public%20static%20Cursor%20Query(SQLiteDatabase%20db%2C%20String%20sql)%20%7B%0A%09%09System.out.println(%22%3D%3DQuery%3D%3D%22%20%2B%20sql)%3B%0A%09%09return%20db.rawQuery(sql%2C%20new%20String%5B%5D%20%7B%7D)%3B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E9%80%9A%E7%94%A8%E6%89%A7%E8%A1%8Csql%2C%2C%E5%A4%9A%E7%94%A8%E4%BA%8E%E4%BA%8B%E5%8A%A1%E4%B8%AD%0A%09%20*%20%0A%09%20*%2F%0A%09public%20static%20void%20execSQL(SQLiteDatabase%20db%2C%20String%20sql)%20%7B%0A%09%09System.out.println(%22%3D%3DexecSQL%3D%3D%22%20%2B%20sql)%3B%0A%09%09db.execSQL(sql)%3B%0A%09%7D%0A%0A%7D%0A%0A%2F%2F-------------------------%E4%B8%8B%E9%9D%A2%E6%98%AF%E4%B8%80%E4%B8%AA%E5%85%B7%E4%BD%93%E5%AE%9E%E7%8E%B0%E7%9A%84DEMO-------------------------------%2F%2F%0A%2F**%0A%20*%20dao%E7%B1%BB%2C%E5%AE%9E%E7%8E%B0%E5%9F%BA%E6%9C%AC%E7%9A%84%E6%95%B0%E6%8D%AE%E5%BA%93%E6%93%8D%E4%BD%9C%3Cbr%2F%3E%0A%20*%20%E9%9C%80%E8%A6%81%E4%BF%9D%E5%AD%98%E5%88%B0%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%AD%E7%9A%84%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%B1%9E%E6%80%A7%E4%B8%8D%E8%83%BD%E4%BD%BF%E7%94%A8%E5%9F%BA%E6%9C%AC%E7%B1%BB%E5%9E%8B%2C%E5%BB%BA%E8%AE%AE%E5%8F%AA%E4%BD%BF%E7%94%A8Integer%20%E5%92%8C%20String%3Cbr%2F%3E%0A%20*%20%E5%81%9A%E4%B8%BA%E9%80%9A%E7%94%A8%E5%AF%B9%E8%B1%A1%2C%E4%BB%8Edemo%E5%88%9B%E5%BB%BA%E5%8F%AA%E8%A6%81%E4%BF%AE%E6%94%B9tableName%E5%8D%B3%E5%8F%AF%0A%20*%20%0A%20*%20%40author%20yizhe%0A%20*%20%40date%202012-5-18%0A%20*%2F%0Apublic%20class%20NetTaskBufferDao%20extends%20DBHelper%20%7B%0A%09int%20expiryDays%20%3D%2010%3B%20%2F%2F%20%E6%95%B0%E6%8D%AE%E7%BC%93%E5%AD%98%E6%9C%89%E6%95%88%E6%9C%9F%0A%0A%09public%20NetTaskBufferDao(Context%20context)%20%7B%0A%09%09super(context%2C%20%22NetTaskBuffer%22)%3B%0A%09%7D%0A%0A%09%2F%2F%20pojo%E7%9A%84%E6%95%B4%E6%95%B0%E9%83%BD%E9%9C%80%E8%A6%81%E4%BD%BF%E7%94%A8Long%E7%B1%BB%E5%9E%8B%20%E6%88%96%E8%80%85Integer%E7%B1%BB%E5%9E%8B%20%E5%BB%BA%E8%AE%AE%E4%BD%BF%E7%94%A8Long%0A%09public%20void%20iniContentValues(NetTaskBuffer%20pojo)%20%7B%0A%09%09contentValues%20%3D%20new%20ContentValues()%3B%0A%09%09contentValues.put(%22id%22%2C%20pojo.id)%3B%0A%09%09contentValues.put(%22label%22%2C%20pojo.label)%3B%0A%09%09contentValues.put(%22param%22%2C%20pojo.param)%3B%0A%09%09contentValues.put(%22result%22%2C%20pojo.result)%3B%0A%09%09contentValues.put(%22remark%22%2C%20pojo.remark)%3B%0A%09%09contentValues.put(%22time%22%2C%20pojo.time)%3B%0A%09%7D%0A%0A%09public%20NetTaskBuffer%20setBaseItem(Cursor%20cursor)%20%7B%0A%09%09NetTaskBuffer%20pojo%20%3D%20new%20NetTaskBuffer()%3B%0A%09%09pojo.id%20%3D%20cursor.getInt(cursor.getColumnIndex(%22id%22))%3B%0A%09%09pojo.label%20%3D%20cursor.getString(cursor.getColumnIndex(%22label%22))%3B%0A%09%09pojo.param%20%3D%20cursor.getString(cursor.getColumnIndex(%22param%22))%3B%0A%09%09pojo.result%20%3D%20cursor.getString(cursor.getColumnIndex(%22result%22))%3B%0A%09%09pojo.remark%20%3D%20cursor.getString(cursor.getColumnIndex(%22remark%22))%3B%0A%09%09pojo.time%20%3D%20cursor.getLong(cursor.getColumnIndex(%22time%22))%3B%0A%09%09return%20pojo%3B%0A%09%7D%0A%0A%09%2F%2F%20--------------------%E8%87%AA%E5%AE%9A%E4%B9%89%E6%96%B9%E6%B3%95--------------------------------%2F%2F%0A%09public%20String%20getBuffer(String%20label%2C%20String%20param)%20%7B%0A%09%09String%20sql%20%3D%20%22select%20*%20from%20%22%20%2B%20tableName%0A%09%09%09%09%2B%20%22%20where%20label%3D%3F%20and%20param%3D%3F%20%22%3B%0A%09%09NetTaskBuffer%20obj%20%3D%20getOneAsSQL(sql%2C%20new%20String%5B%5D%20%7B%20label%2C%20param%20%7D)%3B%0A%09%09if%20(null%20%3D%3D%20obj)%20%7B%0A%09%09%09return%20null%3B%0A%09%09%7D%0A%09%09Date%20time%20%3D%20new%20Date(obj.time)%3B%0A%09%09Calendar%20c%20%3D%20Calendar.getInstance()%3B%0A%0A%09%09c.add(Calendar.DAY_OF_MONTH%2C%20-expiryDays)%3B%0A%09%09if%20(time.compareTo(c.getTime())%20%3C%200)%20%7B%0A%09%09%09delete(obj.id)%3B%0A%09%09%09return%20null%3B%0A%09%09%7D%0A%09%09return%20obj.result%3B%0A%09%7D%0A%0A%09public%20String%20getBuffer(String%20label%2C%20String%20param%2C%20String%20remark)%20%7B%0A%09%09String%20sql%20%3D%20%22select%20*%20from%20%22%20%2B%20tableName%0A%09%09%09%09%2B%20%22%20where%20label%3D%3F%20and%20param%3D%3F%20and%20remark%3D%3F%22%3B%0A%09%09NetTaskBuffer%20obj%20%3D%20getOneAsSQL(sql%2C%20new%20String%5B%5D%20%7B%20label%2C%20param%2C%0A%09%09%09%09remark%20%7D)%3B%0A%09%09if%20(null%20%3D%3D%20obj)%20%7B%0A%09%09%09return%20null%3B%0A%09%09%7D%0A%09%09Date%20time%20%3D%20new%20Date(obj.time)%3B%0A%09%09Calendar%20c%20%3D%20Calendar.getInstance()%3B%0A%0A%09%09c.add(Calendar.DAY_OF_MONTH%2C%20-expiryDays)%3B%0A%09%09if%20(time.compareTo(c.getTime())%20%3C%200)%20%7B%0A%09%09%09delete(obj.id)%3B%0A%09%09%09return%20null%3B%0A%09%09%7D%0A%09%09return%20obj.result%3B%0A%09%7D%0A%0A%09public%20void%20deleteBuffer(String%20label)%20%7B%0A%09%09String%20whereSql%20%3D%20%22%20label%3D%3F%22%3B%0A%09%09delete(whereSql%2C%20new%20String%5B%5D%20%7B%20label%20%7D)%3B%0A%09%7D%0A%0A%09public%20void%20deleteBuffer(String%20label%2C%20String%20param)%20%7B%0A%09%09String%20whereSql%20%3D%20%22%20label%3D%3F%20and%20param%3D%3F%22%3B%0A%09%09delete(whereSql%2C%20new%20String%5B%5D%20%7B%20label%2C%20param%20%7D)%3B%0A%09%7D%0A%0A%09public%20void%20deleteBuffer(String%20label%2C%20String%20param%2C%20String%20remark)%20%7B%0A%09%09String%20whereSql%20%3D%20%22%20label%3D%3F%20and%20param%3D%3F%20and%20remark%3D%3F%22%3B%0A%09%09delete(whereSql%2C%20new%20String%5B%5D%20%7B%20label%2C%20param%2C%20remark%20%7D)%3B%0A%09%7D%0A%0A%09%2F%2F%20--------------------%E5%9F%BA%E7%A1%80%E6%96%B9%E6%B3%95---------------------------------%2F%2F%0A%0A%09%2F**%0A%09%20*%20%E6%A0%B9%E6%8D%AEsql%E8%8E%B7%E5%8F%96list%0A%09%20*%2F%0A%09public%20ArrayList%3CNetTaskBuffer%3E%20getListAsSQL(String%20sql)%20%7B%0A%09%09SQLiteDatabase%20db%20%3D%20getReadableDatabase()%3B%0A%09%09Cursor%20cursor%20%3D%20db.rawQuery(sql%2C%20new%20String%5B%5D%20%7B%7D)%3B%0A%09%09ArrayList%3CNetTaskBuffer%3E%20itemList%20%3D%20new%20ArrayList%3CNetTaskBuffer%3E()%3B%0A%09%09for%20(cursor.moveToFirst()%3B%20!(cursor.isAfterLast())%3B%20cursor.moveToNext())%20%7B%0A%09%09%09itemList.add(setBaseItem(cursor))%3B%0A%09%09%7D%0A%09%09cursor.close()%3B%0A%09%09db.close()%3B%0A%09%09return%20itemList%3B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E6%A0%B9%E6%8D%AEID%E8%8E%B7%E5%8F%96%E4%B8%80%E6%9D%A1%E8%AE%B0%E5%BD%95%0A%09%20*%2F%0A%09public%20NetTaskBuffer%20getById(int%20id)%20%7B%0A%09%09String%20sql%20%3D%20%22select%20*%20from%20%22%20%2B%20tableName%20%2B%20%22%20where%20id%3D%22%20%2B%20id%3B%0A%09%09return%20getOneAsSQL(sql%2C%20null)%3B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E8%BF%94%E5%9B%9E%E7%BB%93%E6%9E%9C%E4%B8%AD%E7%9A%84%E6%8F%90%E4%B8%80%E6%9D%A1%E8%AE%B0%E5%BD%95%0A%09%20*%2F%0A%09public%20NetTaskBuffer%20getOneAsSQL(String%20sql%2C%20String%5B%5D%20arr)%20%7B%0A%09%09SQLiteDatabase%20db%20%3D%20getReadableDatabase()%3B%0A%09%09Cursor%20cursor%20%3D%20db.rawQuery(sql%2C%20arr)%3B%0A%09%09try%20%7B%0A%09%09%09if%20(null%20!%3D%20cursor%20%26%26%20cursor.getCount()%20%3E%200)%20%7B%0A%09%09%09%09cursor.moveToFirst()%3B%0A%09%09%09%09return%20setBaseItem(cursor)%3B%0A%09%09%09%7D%0A%09%09%7D%20finally%20%7B%0A%09%09%09cursor.close()%3B%0A%09%09%09db.close()%3B%0A%09%09%7D%0A%09%09return%20null%3B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E4%BF%9D%E5%AD%98%E5%AF%B9%E8%B1%A1%E5%88%B0%E6%95%B0%E6%8D%AE%E5%BA%93%2C%E8%87%AA%E5%8A%A8%E5%88%A4%E6%96%AD%E6%8F%92%E5%85%A5%E8%BF%98%E6%98%AF%E6%9B%B4%E5%83%8F%0A%09%20*%20%0A%09%20*%20%40return%20%E8%BF%94%E5%9B%9E%E6%9B%B4%E6%96%B0%E7%9A%84%E8%AE%B0%E5%BD%95%E6%95%B0%2C%E6%88%96%E8%80%85%20%E6%8F%92%E5%85%A5%E6%95%B0%E6%8D%AE%E7%9A%84id%2C%E5%A6%82%E6%9E%9C%E8%BF%94%E5%9B%9E%E5%80%BC%3C%3D0%E8%A1%A8%E7%A4%BA%E5%A4%B1%E8%B4%A5%0A%09%20*%2F%0A%09public%20long%20save(NetTaskBuffer%20pojo)%20%7B%0A%09%09if%20(null%20%3D%3D%20pojo.time%20%7C%7C%200%20%3D%3D%20pojo.time)%20%7B%0A%09%09%09pojo.time%20%3D%20new%20Date().getTime()%3B%0A%09%09%7D%0A%09%09Long%20idOrEffectRows%20%3D%200l%3B%0A%09%09if%20(null%20%3D%3D%20pojo.id%20%7C%7C%20pojo.id%20%3C%201)%20%7B%0A%09%09%09idOrEffectRows%20%3D%20insert(pojo)%3B%0A%09%09%7D%20else%20%7B%0A%09%09%09idOrEffectRows%20%3D%20(long)%20update(pojo)%3B%0A%09%09%7D%0A%09%09return%20idOrEffectRows%3B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E6%B7%BB%E5%8A%A0%E6%95%B0%E6%8D%AE%2C%E8%87%AA%E5%8A%A8%E6%8F%92%E5%85%A5id%0A%09%20*%20%0A%09%20*%20%40return%20the%20row%20ID%20of%20the%20newly%20inserted%20row%2C%20or%20-1%20if%20an%20error%20occurred%0A%09%20*%2F%0A%09public%20long%20insert(NetTaskBuffer%20pojo)%20%7B%0A%09%09SQLiteDatabase%20db%20%3D%20getWritableDatabase()%3B%2F%2F%20%E8%8E%B7%E5%8F%96%E5%8F%AF%E5%86%99SQLiteDatabase%E5%AF%B9%E8%B1%A1%0A%09%09iniContentValues(pojo)%3B%20%2F%2F%20ContentValues%E7%B1%BB%E4%BC%BCmap%EF%BC%8C%E5%AD%98%E5%85%A5%E7%9A%84%E6%98%AF%E9%94%AE%E5%80%BC%E5%AF%B9%0A%09%09long%20result%20%3D%20db.insert(tableName%2C%20null%2C%20contentValues)%3B%0A%09%09if%20(result%20!%3D%20-1)%20%7B%0A%09%09%09pojo.id%20%3D%20(int)%20result%3B%0A%09%09%7D%0A%09%09db.close()%3B%0A%09%09return%20result%3B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E6%A0%B9%E6%8D%AEID%E6%9B%B4%E6%96%B0%E8%AE%B0%E5%BD%95%E7%9A%84%EF%BC%8C%E8%B7%9F%E6%8F%92%E5%85%A5%E7%9A%84%E5%BE%88%E5%83%8F%0A%09%20*%20%0A%09%20*%20%40return%20the%20number%20of%20rows%20affected%0A%09%20*%20%0A%09%20*%2F%0A%09public%20int%20update(NetTaskBuffer%20pojo)%20%7B%0A%09%09SQLiteDatabase%20db%20%3D%20getWritableDatabase()%3B%0A%09%09iniContentValues(pojo)%3B%20%2F%2F%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%94%AE%E5%80%BC%E5%AF%B9%0A%09%09int%20result%20%3D%20db.update(tableName%2C%20contentValues%2C%20%22id%3D%3F%22%2C%0A%09%09%09%09new%20String%5B%5D%20%7B%20pojo.id%20%2B%20%22%22%20%7D)%3B%0A%09%09db.close()%3B%0A%09%09return%20result%3B%0A%09%7D%0A%0A%7D%0A%0A%0A" />ways" /> 收藏代码
  1. //创建数据库  
  2. public class DBCreate {  
  3.     public static void CreateDatabase(SQLiteDatabase db) {  
  4.         db.beginTransaction();  
  5.         try {  
  6.             create_NetTaskBuffer(db);  
  7.   
  8.             insert_SQL(db);  
  9.   
  10.             db.setTransactionSuccessful();  
  11.         } catch (Exception e) {  
  12.             e.printStackTrace();  
  13.         } finally {  
  14.             db.endTransaction();  
  15.         }  
  16.     }  
  17.   
  18.     // 缓存  
  19.     private static void create_NetTaskBuffer(SQLiteDatabase db) {  
  20.         db.execSQL("DROP TABLE IF EXISTS NetTaskBuffer");  
  21.         StringBuilder sql = new StringBuilder();  
  22.         sql.append("CREATE TABLE IF NOT EXISTS NetTaskBuffer (");  
  23.         sql.append(" id INTEGER PRIMARY KEY AUTOINCREMENT,"); // 这一列不能修改  
  24.         sql.append(" label TEXT COLLATE NOCASE,"); //  
  25.         sql.append(" param TEXT COLLATE NOCASE,"); //  
  26.         sql.append(" result TEXT COLLATE NOCASE,"); //  
  27.         sql.append(" remark TEXT COLLATE NOCASE,");  
  28.         sql.append(" time LONG)"); //  
  29.         db.execSQL(sql.toString());  
  30.     }  
  31.   
  32.     // 插入语句,初始化数据库使用  
  33.     private static void insert_SQL(SQLiteDatabase db) {  
  34.           
  35.     }  
  36. }  
  37.   
  38.   
  39.   
  40. //----------------------数据库操作基类--------------------------------------//  
  41.   
  42.   
  43. /** 
  44.  * 数据库基本操作类,数据库的创建,更新的操作都在这里进行 
  45.  *  
  46.  * @author yizhe 
  47.  * @date 2014-9-11 
  48.  */  
  49. public abstract class DBHelper extends SQLiteOpenHelper {  
  50.   
  51.     static String name = "hk.db"; // 数据库名称  
  52.     static CursorFactory cursorFactory = null;  
  53.     static int version = 1000;// 初始版本:1000  
  54.     Context context;  
  55.   
  56.     protected ContentValues contentValues; // cursor对象转行中介,给子类使用  
  57.   
  58.     protected String tableName;  
  59.   
  60.     protected DBHelper(Context context, String tableName) {  
  61.         super(context, name, cursorFactory, version);  
  62.         this.context = context;  
  63.         this.tableName = tableName;  
  64.     }  
  65.   
  66.     /** 
  67.      * 软件第一次安装的时候会调用,覆盖安装不会调用 
  68.      */  
  69.     public void onCreate(SQLiteDatabase db) {  
  70.         // 所有表的创建过程都在这里进行  
  71.         DBCreate.createDatabase(db);  
  72.     }  
  73.   
  74.     /** 
  75.      * 覆盖安装,当版本号version发生变化的时候,这个方法才会被调用,而且只执行一次 
  76.      */  
  77.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  78.         onCreate(db);  
  79.         // if (oldVersion < 109) {  
  80.         // onCreate(db);  
  81.         // } else {  
  82.         // }  
  83.     }  
  84.   
  85.     /** 
  86.      * 每次成功打开数据库后首先被执行 
  87.      */  
  88.     public void onOpen(SQLiteDatabase db) {  
  89.         super.onOpen(db); // 每次成功打开数据库后首先被执行  
  90.     }  
  91.   
  92.     public void finalize() {  
  93.         close();  
  94.     }  
  95.   
  96.     // --------------------------sql方法---------------------------------//  
  97.     /** 
  98.      * 执行sql,没有返回 
  99.      */  
  100.     public void execSQL(String sql) {  
  101.         System.out.println("==execSQL==" + sql);  
  102.         SQLiteDatabase db = getWritableDatabase();  
  103.         db.execSQL(sql);  
  104.         db.close();  
  105.     }  
  106.   
  107.     /** 
  108.      * 批量执行sql,内部启用事务 
  109.      *  
  110.      * @param list 
  111.      *            sql列表 
  112.      * @return 是否执行成功 
  113.      */  
  114.     public boolean execSQLBatch(ArrayList<String> list) {  
  115.         SQLiteDatabase db = getWritableDatabase();  
  116.         db.beginTransaction();  
  117.         try {  
  118.             for (String sql : list) {  
  119.                 db.execSQL(sql);  
  120.             }  
  121.             db.setTransactionSuccessful();  
  122.         } catch (Exception e) {  
  123.             e.printStackTrace();  
  124.             return false;  
  125.         } finally {  
  126.             db.endTransaction();  
  127.             db.close();  
  128.         }  
  129.         return true;  
  130.     }  
  131.   
  132.     /** 
  133.      * 根据id删除记录 
  134.      *  
  135.      * @param id 
  136.      *            表中必须有"id"字段 
  137.      * @return the number of rows affected if a whereClause is passed in, 0 
  138.      *         otherwise. To remove all rows and get a count pass "1" as the 
  139.      *         whereClause. 
  140.      */  
  141.     public int delete(int id) {  
  142.         SQLiteDatabase db = getWritableDatabase();  
  143.         int result = db.delete(tableName, "id=?", new String[] { id + "" });  
  144.         db.close();  
  145.         return result;  
  146.     }  
  147.   
  148.     /** 
  149.      * 删除: 只需要写where 条件(不带where) 和 参数 
  150.      *  
  151.      * @param whereStr 
  152.      *            例如: "id=?" 
  153.      * @param arr 
  154.      *            例如: new String[] { "123" } 
  155.      * @return 受影响的行 
  156.      */  
  157.     public int delete(String whereStr, String[] arr) {  
  158.         SQLiteDatabase db = getWritableDatabase();  
  159.         int result = db.delete(tableName, whereStr, arr);  
  160.         db.close();  
  161.         return result;  
  162.     }  
  163.   
  164.     /** 
  165.      * 清空表 
  166.      */  
  167.     public void clearTable() {  
  168.         SQLiteDatabase db = getWritableDatabase();  
  169.         db.execSQL("delete from " + tableName);  
  170.         db.close();  
  171.     }  
  172.   
  173.     /** 
  174.      * 通用查询,多用于事务中 
  175.      */  
  176.     public static Cursor Query(SQLiteDatabase db, String sql) {  
  177.         System.out.println("==Query==" + sql);  
  178.         return db.rawQuery(sql, new String[] {});  
  179.     }  
  180.   
  181.     /** 
  182.      * 通用执行sql,,多用于事务中 
  183.      *  
  184.      */  
  185.     public static void execSQL(SQLiteDatabase db, String sql) {  
  186.         System.out.println("==execSQL==" + sql);  
  187.         db.execSQL(sql);  
  188.     }  
  189.   
  190. }  
  191.   
  192. //-------------------------下面是一个具体实现的DEMO-------------------------------//  
  193. /** 
  194.  * dao类,实现基本的数据库操作<br/> 
  195.  * 需要保存到数据库中的对象的属性不能使用基本类型,建议只使用Integer 和 String<br/> 
  196.  * 做为通用对象,从demo创建只要修改tableName即可 
  197.  *  
  198.  * @author yizhe 
  199.  * @date 2012-5-18 
  200.  */  
  201. public class NetTaskBufferDao extends DBHelper {  
  202.     int expiryDays = 10; // 数据缓存有效期  
  203.   
  204.     public NetTaskBufferDao(Context context) {  
  205.         super(context, "NetTaskBuffer");  
  206.     }  
  207.   
  208.     // pojo的整数都需要使用Long类型 或者Integer类型 建议使用Long  
  209.     public void iniContentValues(NetTaskBuffer pojo) {  
  210.         contentValues = new ContentValues();  
  211.         contentValues.put("id", pojo.id);  
  212.         contentValues.put("label", pojo.label);  
  213.         contentValues.put("param", pojo.param);  
  214.         contentValues.put("result", pojo.result);  
  215.         contentValues.put("remark", pojo.remark);  
  216.         contentValues.put("time", pojo.time);  
  217.     }  
  218.   
  219.     public NetTaskBuffer setBaseItem(Cursor cursor) {  
  220.         NetTaskBuffer pojo = new NetTaskBuffer();  
  221.         pojo.id = cursor.getInt(cursor.getColumnIndex("id"));  
  222.         pojo.label = cursor.getString(cursor.getColumnIndex("label"));  
  223.         pojo.param = cursor.getString(cursor.getColumnIndex("param"));  
  224.         pojo.result = cursor.getString(cursor.getColumnIndex("result"));  
  225.         pojo.remark = cursor.getString(cursor.getColumnIndex("remark"));  
  226.         pojo.time = cursor.getLong(cursor.getColumnIndex("time"));  
  227.         return pojo;  
  228.     }  
  229.   
  230.     // --------------------自定义方法--------------------------------//  
  231.     public String getBuffer(String label, String param) {  
  232.         String sql = "select * from " + tableName  
  233.                 + " where label=? and param=? ";  
  234.         NetTaskBuffer obj = getOneAsSQL(sql, new String[] { label, param });  
  235.         if (null == obj) {  
  236.             return null;  
  237.         }  
  238.         Date time = new Date(obj.time);  
  239.         Calendar c = Calendar.getInstance();  
  240.   
  241.         c.add(Calendar.DAY_OF_MONTH, -expiryDays);  
  242.         if (time.compareTo(c.getTime()) < 0) {  
  243.             delete(obj.id);  
  244.             return null;  
  245.         }  
  246.         return obj.result;  
  247.     }  
  248.   
  249.     public String getBuffer(String label, String param, String remark) {  
  250.         String sql = "select * from " + tableName  
  251.                 + " where label=? and param=? and remark=?";  
  252.         NetTaskBuffer obj = getOneAsSQL(sql, new String[] { label, param,  
  253.                 remark });  
  254.         if (null == obj) {  
  255.             return null;  
  256.         }  
  257.         Date time = new Date(obj.time);  
  258.         Calendar c = Calendar.getInstance();  
  259.   
  260.         c.add(Calendar.DAY_OF_MONTH, -expiryDays);  
  261.         if (time.compareTo(c.getTime()) < 0) {  
  262.             delete(obj.id);  
  263.             return null;  
  264.         }  
  265.         return obj.result;  
  266.     }  
  267.   
  268.     public void deleteBuffer(String label) {  
  269.         String whereSql = " label=?";  
  270.         delete(whereSql, new String[] { label });  
  271.     }  
  272.   
  273.     public void deleteBuffer(String label, String param) {  
  274.         String whereSql = " label=? and param=?";  
  275.         delete(whereSql, new String[] { label, param });  
  276.     }  
  277.   
  278.     public void deleteBuffer(String label, String param, String remark) {  
  279.         String whereSql = " label=? and param=? and remark=?";  
  280.         delete(whereSql, new String[] { label, param, remark });  
  281.     }  
  282.   
  283.     // --------------------基础方法---------------------------------//  
  284.   
  285.     /** 
  286.      * 根据sql获取list 
  287.      */  
  288.     public ArrayList<NetTaskBuffer> getListAsSQL(String sql) {  
  289.         SQLiteDatabase db = getReadableDatabase();  
  290.         Cursor cursor = db.rawQuery(sql, new String[] {});  
  291.         ArrayList<NetTaskBuffer> itemList = new ArrayList<NetTaskBuffer>();  
  292.         for (cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()) {  
  293.             itemList.add(setBaseItem(cursor));  
  294.         }  
  295.         cursor.close();  
  296.         db.close();  
  297.         return itemList;  
  298.     }  
  299.   
  300.     /** 
  301.      * 根据ID获取一条记录 
  302.      */  
  303.     public NetTaskBuffer getById(int id) {  
  304.         String sql = "select * from " + tableName + " where id=" + id;  
  305.         return getOneAsSQL(sql, null);  
  306.     }  
  307.   
  308.     /** 
  309.      * 返回结果中的提一条记录 
  310.      */  
  311.     public NetTaskBuffer getOneAsSQL(String sql, String[] arr) {  
  312.         SQLiteDatabase db = getReadableDatabase();  
  313.         Cursor cursor = db.rawQuery(sql, arr);  
  314.         try {  
  315.             if (null != cursor && cursor.getCount() > 0) {  
  316.                 cursor.moveToFirst();  
  317.                 return setBaseItem(cursor);  
  318.             }  
  319.         } finally {  
  320.             cursor.close();  
  321.             db.close();  
  322.         }  
  323.         return null;  
  324.     }  
  325.   
  326.     /** 
  327.      * 保存对象到数据库,自动判断插入还是更像 
  328.      *  
  329.      * @return 返回更新的记录数,或者 插入数据的id,如果返回值<=0表示失败 
  330.      */  
  331.     public long save(NetTaskBuffer pojo) {  
  332.         if (null == pojo.time || 0 == pojo.time) {  
  333.             pojo.time = new Date().getTime();  
  334.         }  
  335.         Long idOrEffectRows = 0l;  
  336.         if (null == pojo.id || pojo.id < 1) {  
  337.             idOrEffectRows = insert(pojo);  
  338.         } else {  
  339.             idOrEffectRows = (long) update(pojo);  
  340.         }  
  341.         return idOrEffectRows;  
  342.     }  
  343.   
  344.     /** 
  345.      * 添加数据,自动插入id 
  346.      *  
  347.      * @return the row ID of the newly inserted row, or -1 if an error occurred 
  348.      */  
  349.     public long insert(NetTaskBuffer pojo) {  
  350.         SQLiteDatabase db = getWritableDatabase();// 获取可写SQLiteDatabase对象  
  351.         iniContentValues(pojo); // ContentValues类似map,存入的是键值对  
  352.         long result = db.insert(tableName, null, contentValues);  
  353.         if (result != -1) {  
  354.             pojo.id = (int) result;  
  355.         }  
  356.         db.close();  
  357.         return result;  
  358.     }  
  359.   
  360.     /** 
  361.      * 根据ID更新记录的,跟插入的很像 
  362.      *  
  363.      * @return the number of rows affected 
  364.      *  
  365.      */  
  366.     public int update(NetTaskBuffer pojo) {  
  367.         SQLiteDatabase db = getWritableDatabase();  
  368.         iniContentValues(pojo); // 初始化键值对  
  369.         int result = db.update(tableName, contentValues, "id=?",  
  370.                 new String[] { pojo.id + "" });  
  371.         db.close();  
  372.         return result;  
  373.     }  
  374.   
  375. }  

安卓标准数据库构建.zip (9.2 KB)

发表评论
用户名: 匿名