Sqlite query & rawQuery

前端之家收集整理的这篇文章主要介绍了Sqlite query & rawQuery前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

场景:

今天在写代码的时候被责令重构,凡是使用rawQuery的地方一律改为使用query,原因无他,可防sql注入,并且由于query的参数一段段都是分开的,所以推荐使用Query。


比如我要查询数据库里某张表最后一条状态为onGoing状态的数据代码如下:

  • rawQuery
  1. public Visit getLastData() {
  2. //todo
  3. Cursor cursor = database.rawQuery("SELECT * FROM " + table +
  4. " where status = "+Visit.STATUS_ONGOING+
  5. " ORDER BY android_id DESC LIMIT 1",null);
  6. cursor.moveToFirst();
  7. return cursorToEntity(cursor);
  8. }


  1.  
自己拼接sql语句,一不小心就会写错.
  • Query
  1. /**
  2. * get last ongoing visit in database.
  3. * @return
  4. */
  5. public Visit getLastOngoingVisit() {
  6. String[] columns = null;
  7. String selection = VisitDB.Visit.COLUMN_STATUS + "=?";
  8. String[] selectionArgs = {String.valueOf(Visit.STATUS_ONGOING)};
  9. String orderby = VisitDB.Visit.COLUMN_ANDROID_ID+" DESC";
  10. String limit = "1";
  11. Cursor cursor = database.query(table,columns,selection,selectionArgs,null,orderby,limit);
  12. cursor.moveToFirst();
  13. return cursorToEntity(cursor);
  14. }

这样似乎很清晰吧

关于query各个参数说明

  1. /**
  2. * Query the given table,returning a {@link Cursor} over the result set.
  3. *
  4. * @param table The table name to compile the query against.
  5. * @param columns A list of which columns to return. Passing null will
  6. * return all columns,which is discouraged to prevent reading
  7. * data from storage that isn't going to be used.
  8. * @param selection A filter declaring which rows to return,formatted as an
  9. * sql WHERE clause (excluding the WHERE itself). Passing null
  10. * will return all rows for the given table.
  11. * @param selectionArgs You may include ?s in selection,which will be
  12. * replaced by the values from selectionArgs,in order that they
  13. * appear in the selection. The values will be bound as Strings.
  14. * @param groupBy A filter declaring how to group rows,formatted as an sql
  15. * GROUP BY clause (excluding the GROUP BY itself). Passing null
  16. * will cause the rows to not be grouped.
  17. * @param having A filter declare which row groups to include in the cursor,* if row grouping is being used,formatted as an sql HAVING
  18. * clause (excluding the HAVING itself). Passing null will cause
  19. * all row groups to be included,and is required when row
  20. * grouping is not being used.
  21. * @param orderBy How to order the rows,formatted as an sql ORDER BY clause
  22. * (excluding the ORDER BY itself). Passing null will use the
  23. * default sort order,which may be unordered.
  24. * @param limit Limits the number of rows returned by the query,* formatted as LIMIT clause. Passing null denotes no LIMIT clause.
  25. * @return A {@link Cursor} object,which is positioned before the first entry. Note that
  26. * {@link Cursor}s are not synchronized,see the documentation for more details.
  27. * @see Cursor
  28. */
对于sqlite的limit跟MysqL还是有区别的,有个offset关键字,可忽略多少条记录进行查询多少条数据,具体的不赘述,自己实验使用吧。

猜你在找的Sqlite相关文章