在开发Android 应用经常会使用数据库,其一般都是使用SQLite数据库进行开发。 操作数据库所需要都在android.database.sqlite 软件包中提供。
首先我们要创建表
package com.manager.czh.toolbartest;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SQLDBhelper extends SQLiteOpenHelper {
private static final String DB_NAME="TEST";
private static final int VERSION=1;
private final String CREATE_TABLE="CREATE TABLE"+DB_NAME+
"(_id integer primary key autoincrement,"
+"name text,"
+"class text)";
private final String DROP_TABLE="DROP TABLE IF EXISTS"+DB_NAME;
/**
*
* @param context
*/
public SQLDBhelper(Context context) {
super(context, DB_NAME, null, VERSION);
}
/**
* 当数据库第一次被创建时调用
* @param db
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
/**
* 当版本号不同时调用
* @param db
* @param oldVersion
* @param newVersion
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE);
}
}
在建表完成后,说一下数据库中四大操作”增删改查”。
插入数据
public long insert (String table, String nullColumnHack, ContentValues values)
第一个参数是表名,第二个参数是为了防止当values为空时插入数据失败而设置的,第三个则是要插入的数据。
看看源码中注释就可以理解第二个参数了。
/**
* Convenience method for inserting a row into the database.
*
* @param table the table to insert the row into
* @param nullColumnHack optional; may be <code>null</code>.
* SQL doesn't allow inserting a completely empty row without
* naming at least one column name. If your provided <code>values</code> is
* empty, no column names are known and an empty row can't be inserted.
* If not set to null, the <code>nullColumnHack</code> parameter
* provides the name of nullable column name to explicitly insert a NULL into
* in the case where your <code>values</code> is empty.
* @param values this map contains the initial column values for the
* row. The keys should be the column names and the values the
* column values
* @return the row ID of the newly inserted row, or -1 if an error occurred
*/
public long insert(String table, String nullColumnHack, ContentValues values) {
try {
return insertWithOnConflict(table, nullColumnHack, values, CONFLICT_NONE);
} catch (SQLException e) {
Log.e(TAG, "Error inserting " + values, e);
return -1;
}
}
大概是说:由于SQL不允许插入空行,因此为了防止出错,需要将某一列的值设为null然后再进行插入操作。而nullColumnHack即为列名。
删除和更新操作
public int delete(String table, String whereClause, String[] whereArgs)
public int update(String table, ContentValues values, String whereClause, String[] whereArgs)
table为表名,whereClause表示where表达式 如上面的name =?,whereArgs为占位符实际值,即 ?(s) 的实际值。
而update只是比删除多了个ContentValues用于更新数据。
查询操作
query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit, CancellationSignal cancellationSignal)
query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
table:表名
conlumns:列名数组。
selection:where后面的条件语句。
selectionArgs:占位符实际值,即 ? 的实际值。
groupBy:分组的列名。
having:分组条件。
orderBy:指定排序的列名。
limit:指定分页参数。
distinct:“true”表示过滤重复值 “false”表示不过滤重复值。