[关闭]
@ZeroGeek 2015-10-21T09:06:33.000000Z 字数 1294 阅读 542

SQLite用法整理

每周主题


介绍

目前Android中最新SQLite为3.4.0版本。

相关类说明

类名 说明
SQLiteClosable An object created from a SQLiteDatabase that can be closed.
SQLiteCursor A Cursor implementation that exposes results from a query on a SQLiteDatabase.
SQLiteDatabase Exposes methods to manage a SQLite database.
SQLiteOpenHelper A helper class to manage database creation and version management.
SQLiteProgram A base class for compiled SQLite programs.
SQLiteQuery Represents a query that reads the resulting rows into a SQLiteQuery.
SQLiteQueryBuilder This is a convience class that helps build SQL queries to be sent to SQLiteDatabase objects.
SQLiteStatement Represents a statement that can be executed against a database.

正确使用事务

SQLiteDatabase db; // db的声明

  1. db.beginTransaction();
  2. try {
  3. ...
  4. db.setTransactionSuccessful();
  5. } finally {
  6. db.endTransaction();
  7. }

正确查询

  1. private String getCategoryNameById(long id){
  2. String querySql = "select name from t_category where categoryPOID = ? and depth = 2";
  3. String[] args = {
  4. String.valueOf(id),
  5. };
  6. Cursor c = null;
  7. String name = "";
  8. try {
  9. c = db.rawQuery(querySql, args);
  10. if(c.moveToFirst()){
  11. name = c.getString(c.getColumnIndex("name"));
  12. }
  13. } finally {
  14. closeCursor(c);
  15. }
  16. return name;
  17. }
  18. /**
  19. * 释放游标
  20. */
  21. protected void closeCursor(Cursor c) {
  22. if (c != null) {
  23. if (!c.isClosed()) {
  24. c.close();
  25. c = null;
  26. }
  27. }
  28. }

http://androiddoc.qiniudn.com/reference/android/database/sqlite/package-summary.html
http://androiddoc.qiniudn.com/reference/android/database/sqlite/SQLiteDatabase.html

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注