Android 中的数据库操作

 

1创建数据库

继承SQLiteOpenHelper(抽象类) 。实现一下两个方法

abstract void

onCreate(SQLiteDatabase db)

Called when the database is created for the first time.

abstract void

onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

Called when the database needs to be upgraded.

 

2 数据的操作。(增删改查)

synchronized SQLiteDatabase

getReadableDatabase()

Create and/or open a database.

synchronized SQLiteDatabase

getWritableDatabase()

Create and/or open a database that will be used for reading and writing.

获得可读或可写的数据库。

通过 SQLiteDatabase 类进行数据操作。

常用方法如下:

void

close()

Close the database.

int

delete(String table, String whereClause, String[] whereArgs)

Convenience method for deleting rows in the database.

void

execSQL(String sql)

Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.

void

execSQL(String sql, Object[] bindArgs)

Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.

long

insert(String table, String nullColumnHack, ContentValues values)

Convenience method for inserting a row into the database.

long

insertOrThrow(String table, String nullColumnHack, ContentValues values)

Convenience method for inserting a row into the database.

boolean

isOpen()

boolean

isReadOnly()

return whether the DB is opened as read only.

static SQLiteDatabase

openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags, DatabaseErrorHandler errorHandler)

Open the database according to the flags OPEN_READWRITE OPEN_READONLY CREATE_IF_NECESSARY and/or NO_LOCALIZED_COLLATORS.

static SQLiteDatabase

openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags)

Open the database according to the flags OPEN_READWRITE OPEN_READONLY CREATE_IF_NECESSARY and/or NO_LOCALIZED_COLLATORS.

static SQLiteDatabase

openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler)

Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY, errorHandler).

static SQLiteDatabase

openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory)

Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY).

static SQLiteDatabase

openOrCreateDatabase(File file, SQLiteDatabase.CursorFactory factory)

Equivalent to openDatabase(file.getPath(), factory, CREATE_IF_NECESSARY).

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.

Cursor

query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)

Query the given table, returning a Cursor over the result set.

Cursor

query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)

Query the given URL, returning a Cursor over the result set.

Cursor

queryWithFactory(SQLiteDatabase.CursorFactory cursorFactory, boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)

Query the given URL, returning a Cursor over the result set.

Cursor

rawQuery(String sql, String[] selectionArgs)

Runs the provided SQL and returns a Cursor over the result set.

Cursor

rawQueryWithFactory(SQLiteDatabase.CursorFactory cursorFactory, String sql, String[] selectionArgs, String editTable)

Runs the provided SQL and returns a cursor over the result set.

static int

releaseMemory()

Attempts to release memory that SQLite holds but does not require to operate properly.

long

replace(String table, String nullColumnHack, ContentValues initialValues)

Convenience method for replacing a row in the database.

long

replaceOrThrow(String table, String nullColumnHack, ContentValues initialValues)

Convenience method for replacing a row in the database.

 

 

你可能感兴趣的:(sql,android,sqlite)