要实现Android对本地数据库的访问,一个方法是用SQLiteDatabase类来实现,但是使用这个类,需要自己创建数据库,不便于管理.所以Android又封装了一个SQLiteOpenHelper的类,它可以帮助你创建和管理数据库.
首先要继承这个类,复写他的onCreate()和onUpgrade()两个抽象方法
1. 如果数据库不存在,就会调用onCreat(),不会调用onUpgrade();
2. 如果数据库存在,但是版本不一样就调用onUpgrade(),不会调用onCreate();
3. 如果数据库存在,版本一样不会调用onCreate(), sonUpgrade();
4. 当调用getWritableDatabase,getReadableDatabase如果数据库没有打开,就调用onOpen方法,如果打开了就不调onOPen;
5. 数据库的表的创建一般都在SQLiteOpenHelper的onCreat()中,表字段升级,都会在onUpgrade()处理;
既然SQLiteOpenHelper类中不方便执行具体的增删改差操作,可以用一个Adapter来实现
顺便发个广告 (求大神指导!!!! )
package com.example.db;
import java.io.File;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
public class MyOpenHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "DATA.db";
public static final String DB_TABLE = "patient";
private static final String DB_CREATE = "create table if not exists "
+ DB_TABLE
+ "(patient_id varchar(20) primary key,file_loc varchar(20),create_time varchar(20),content varchar);";
public MyOpenHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
db.execSQL("DROP table if exists " + DB_TABLE);
onCreate(db);
}
}
由于SQLiteOpenHelper. getWritableDatabase ()和SQLiteOpenHelper. getReadableDatabase()返回SQLiteDatabase对象,我们就可以对这个数据库进行操作.
public long insert (String table, String nullColumnHack, ContentValues values)
Convenience method for inserting a row into the database.
Parameters
table the table to insert the row into
表名
nullColumnHack optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty.
这个参数需要传入一个列名。SQL标准并不允许插入所有列均为空的一行数据,所以当传入的initialValues值为空或者为0时,用nullColumnHack参数指定的列会被插入值为NULL的数据,然后再将此行插入到表中。
values this map contains the initial column values for the row. The keys should be the column names and the values the column values
ContentValues.put()方法存放键值对
Returns
the row ID of the newly inserted row, or -1 if an error occurred
返回当前插入行的id号
public int delete (String table, String whereClause, String[] whereArgs)
Convenience method for deleting rows in the database.
Parameters
table the table to delete from
表名
whereClause the optional WHERE clause to apply when deleting. Passing null will delete all rows.
Where 语句
whereArgs You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings.
Where语句的参数代替?
Returns
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.
即返回的是受影响的行数(是符合删除条件的),
0 除外(即删除操作失败)。
public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit, CancellationSignal cancellationSignal)
Query the given URL, returning a Cursor over the result set.
distinct true if you want each row to be unique, false otherwise.列出唯一的
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. Where子句
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. Where子句的参数
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. Groupby子句
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. Having子句
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. Orderby子句
limit Limits the number of rows returned by the query, formatted as LIMIT clause. Passing null denotes no LIMIT clause. 返回的行数
cancellationSignal A signal to cancel the operation in progress, or null if none. If the operation is canceled, then OperationCanceledException will be thrown when the query is executed.
Returns
A Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized, see the documentation for more details.
public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
Convenience method for updating rows in the database.
Parameters
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. Where子句
whereArgs You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings. Where子句参数
Returns
the number of rows affected 受影响的行