SQLite用法
SQLiteDataBase是数据库类,SQLiteOpenHelper是用来管理数据库的辅助类 一,SQLiteOpenHelper 类 其中: getReadableDatabase()得到可读的数据库,返回SQLiteDatabase对象,然后通过对象进行数据库操作。 getWritableDatabase()得到可写的数据库,返回SQLiteDatabase对象,然后通过对象进行数据库操作。 onCreate(SQLiteDatabase db)在第一次创建数据库时调用。 onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)在更改数据库时都会调用。 onOpen(SQLiteDatabase db)当数据库打开时调用。 close()关闭数据库。 二,SQLiteDatabase类 这个类提供了对数据库的一些基本操作: insert() delete() update() query() 。。。。。等等。 代码: SQLiteOpenHelper 的子类: [java] view plain copy
有了这个类后,我们就可以对数据库进行增、删、改、查操作了。
删除数据: [java] view plain copy
修改数据: [java] view plain copy
查询数据: 1、SQLiteDataBase对象的query()接口: publicCursor query (Stringtable,String[]columns,Stringselection,String[]selectionArgs, StringgroupBy,Stringhaving,StringorderBy,Stringlimit) Query the given table,returning a Parameters | table | The table name to compile the query against.(要查询的表名.)columns | A list of which columns to return. Passing null will return allcolumns,which is discouraged to prevent reading data from storagethat isn't going to be used.(想要显示的列,若为空则返回所有列,不建议设置为空,如果不是返回所有列)selection | A filter declaring which rows to return,formatted as an SQL WHEREclause (excluding the WHERE itself). Passing null will return allrows for the given table.(where子句,声明要返回的行的要求,如果为空则返回表的所有行。)selectionArgs | You may include ?s in selection,which will be replaced by thevalues from selectionArgs,in order that they appear in theselection. The values will be bound as Strings.(where子句对应的条件值)groupBy | A filter declaring how to group rows,formatted as an SQL GROUP BYclause (excluding the GROUP BY itself). Passing null will cause therows to not be grouped.(分组方式,若为空则不分组.)having | A filter declare which row groups to include in the cursor,if rowgrouping is being used,formatted as an SQL HAVING clause(excluding the HAVING itself). Passing null will cause all rowgroups to be included,and is required when row grouping is notbeing used.(having条件,若为空则返回全部(不建议))orderBy | How to order the rows,formatted as an SQL ORDER BY clause(excluding the ORDER BY itself). Passing null will use the defaultsort order,which may be unordered.(排序方式,为空则为默认排序方式)limit | Limits the number of rows returned by the query,formatted as LIMITclause. Passing null denotes no LIMIT clause.(限制返回的记录的条数,为空则不限制)table | the table to insert the row into(要插入数据的表的名称)nullColumnHack | optional; may bevalues | this map contains the initial column values for the row. The keysshould be the column names and the values the columnvalues(一个ContentValues对象,类似一个map.通过键值对的形式存储值。)table | the table to update in(要更新的表名)values | a map from column names to new column values. null is a valid valuethat will be translated to NULL.(一个ContentValues对象,类似一个map.通过键值对的形式存储值。) whereClause whereArgs |
the optional WHERE clause to apply when updating. Passing null willupdate all rows.(可选的where语句)table | the table to delete from whereClause whereArgs |
the optional WHERE clause to apply when deleting. Passing null willdelete all rows.(可选的where语句)
---|