Android学习笔记---SQLite数据库 api

来源:互联网 发布:守望先锋亚服淘宝 编辑:程序博客网 时间:2024/04/20 21:48
一、SQLiteOpenHelper:


public abstract class SQLiteOpenHelper extends Object; 
抽象类,不能被实例化(不能被new);
子类继承父类,并必须重写抽象方法;
方法抽象,类必须抽象;
子类可以不重写方法,但是自己也得定义为抽象类,让子类实现。 


构造方法:
SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory, int version)
创建一个帮助对象to create, open, and/or manage a database



方法:
void close()
关闭任何一个打开的数据库对象;


SQLiteDatabase getReadableDatabase()
创建或打开一个数据库;


SQLiteDatabase getWritableDatabase()
创建或打开一个将被用于读和写的数据库;



abstract void onCreate(SQLiteDatabase db)
当数据库第一次被创建时调用;



void onOpen(SQLiteDatabase db)
当数据库已经被打开时调用;


abstract void onUpgtade(SQLiteDatabase db, int oldVersion, int newVersion) 
当数据库需要被更新时调用;





二、SQLiteDatabase:


public class SQLiteDatabase extends SQLiteClosable;
暴露方法去管理一个SQLite数据库


static interface SQLiteDatabase.CursorFactory
当调用查询时用于允许返回cursor子类


static SQLiteDatabase openDatabase(String path, QLiteDatabase.CursorFactory factory, int flags)
static SQLiteDatabase openOrCreateDatabase(File file, SQLiteDatabase.CursorFactory factory)
static SQLiteClosable openOrCreateDatabase(String path,SQLiteDatabase.CursorFactory factory)


事务:
void beginTransaction()
开始一个事务;
boolean inTransaction()
如果有一个事务等待期间返回true;
void setTransactionSuccessful()
将目前事务标记为成功;
void endTransaction()
结束一个事务;


void close()
关闭一个数据库


void execSQL(String sql)
例:创建一个表
String sql = "CREATE TABLE [" + TABLE_NAME + "]([id] INTEGER PRIMARY KEY;"
void execSQL(String sql, Object[] bindArgs)
执行一个不是查询的SQL语句


int getVersion()
得到数据库版本


boolean isOpen()


增、删、改、查:
增:
long insert(String table, String nullColumnHack, ContentValues values)
用于给数据库插入一行数据的便利方法
Convenience method for inserting a row into the database;
参数:
table - the table to insert the row into
nullColumnHack - SQL doesn't allow inserting a completely empty row, so if initialValues is empty this column will explicitly be assigned a NULL value
values - this map contains the initial column values for the row. The keys should be the column names and the values the column values 
返回:
the row ID of the newly inserted row, or -1 if an error occurred


删:
int delete(String table, String whereClause, String[] whereArgs)
删除多行
Convenience method for deleting rows in the database;
参数:
table - the table to delete from
whereClause - the optional WHERE clause to apply when deleting. Passing null will delete all rows. 
返回:
the number of rows affected if a whereClause is passed in, 0 otherwise. To remove all rows and get a count pass "1" as the whereClause.


改:
int update(String table, ContentValues values, String whereClause,String[] whereArgs) ??[]多个数据??如何传?
在数据库中更新多行数据
参数:
table - the table to update in
values - a map from column names to new column values. null is a valid value that will be translated to NULL.
whereClause - the optional WHERE clause to apply when updating. Passing null will update all rows. 
返回:
the number of rows affected


查:
Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy,String limit)
查询给定的表,返回结果集上的游标
Query the given table, returning a Cursor over the result set.
参数:
table - The table name to compile the query against.
columns - A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data from storage that isn't going to be used.
selection - A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.
selectionArgs - You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.
groupBy - A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped.
having - A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used.
orderBy - How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.
limit - Limits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause. 
返回:
A Cursor object, which is positioned before the first entry

















0 0