/** * 删除,表名不能使用占位符 * * @author: huylee * @time: 2015-8-20下午10:18:01 * @param sql 删除语句(参数使用占位符) * @param args 占位符参数 */ @SuppressLint("NewApi") public void updateOrDelete(String sql, Object[] args) { SQLiteStatement statement = mSQLiteDataBase.compileStatement(sql); try { if (args != null) { for (int i = 0; i < args.length; i++) { bindArgs(statement, i + 1, args[i]); } } DBLog.debug(sql, args); statement.executeUpdateDelete(); } finally { statement.close(); } }
/** * 插入一条记录,返回该记录的rowId Author: hyl Time: 2015-8-17上午11:55:55 * * @param sql * @param args * @return 插入失败返回-1,成功返回rowId */ public long insert(String sql, Object[] args) { long rowId = -1; SQLiteStatement statement = this.mSQLiteDataBase.compileStatement(sql); try { if (args != null) { for (int i = 0; i < args.length; i++) { bindArgs(statement, i + 1, args[i]); } } rowId = statement.executeInsert(); DBLog.debug(sql, args); } finally { statement.close(); } return rowId; }
/** * 执行绑定语句 Author: hyl Time: 2015-8-16下午8:26:34 * * @param sql * @param whereArgs * @return */ public Cursor query(String sql, String[] whereArgs) { DBLog.debug("{SQL:" + sql + ",PARAMS:" + whereArgs + "}"); return this.mSQLiteDataBase.rawQuery(sql, whereArgs); }
/** * 删除(对于表名需要动态获取的,此方法非常适合) * * @author: huylee * @time: 2015-8-20下午10:22:32 * @param tableName 要删除的数据表 * @param whereClause where后面的条件句(delete from XXX where XXX),参数使用占位符 * @param whereArgs where子句后面的占位符参数 */ public void delete(String tableName, String whereClause, String[] whereArgs) { DBLog.debug( "{SQL:DELETE FROM " + tableName + " WHERE " + whereClause + ",PARAMS:" + whereArgs + "}"); mSQLiteDataBase.delete(tableName, whereClause, whereArgs); }
/** * 执行指定无返回值的单条SQL语句,如建表、创建数据库等 Author: hyl Time: 2015-8-14下午11:05:03 * * @param sql */ public void execSQL(String sql) { DBLog.debug(sql); this.mSQLiteDataBase.execSQL(sql); }