條件資料庫Android:sqllite的簡單使用

來源:互聯網
上載者:User

SQLite分析

SQLite是輕量級的、嵌入式的、關係型資料庫,現在已經在iPhone、Android等手機系統中應用,SQLite可移植性好,很輕易應用,很小,高效而且牢靠。SQLite嵌入到應用它的應用程式中,它們共用雷同的進程空間,而不是單獨的一個進程。從外部看,它並不像一個RDBMS,但在進程內部,它倒是完整的,自包括的資料庫引擎。

在android中當須要操作SQLite資料庫的時候須要失掉一個SQLiteOpenHelper對象,而SQLiteOpenHelper是一個抽象類別,使用者須要繼承這個類,並實現該類中的一些方法。

1、繼承SQLiteOpenHelper之後就擁有了以下兩個方法:

◆getReadableDatabase() 創立或者開啟一個查詢資料庫

◆getWritableDatabase()創立或者開啟一個可寫資料庫

◆他們都市返回SQLiteDatabase對象,使用者通過失掉的SQLiteDatabase對象進行後續操作

2、同時使用者還可以覆蓋以下回呼函數,再對資料庫進行操作的時候回調以下方法:

◆onCreate(SQLiteDatabase):在資料庫第一次創立的時候會調用這個方法,一般我們在這個方法裡邊創立資料庫表。

◆onUpgrade(SQLiteDatabase,int,int):當資料庫須要修改的時候,Android系統會主動的調用這個方法。一般我們在這個方法裡邊刪除資料庫表,並建立新的資料庫表,當然是否還須要做其他的操作,完整取決於應用程式的需求。

◆onOpen(SQLiteDatabase):這是當開啟資料庫時的回呼函數,一般也不會用到。

須要注意

1、在SQLiteOepnHelper的子類當中,必須有以下該建構函式

複製代碼 代碼如下:public DatabaseHelper(Context context, String name, CursorFactory factory,
int version) {
//必須通過super調用父類當中的建構函式
super(context, name, factory, version);
} 為了便利,也可以創立其它的建構函式,含二個參數或者三個參數的。

2、函數public void onCreate(SQLiteDatabase db)是在調用getReadableDatabase()或者是getWritableDatabase()第一次創立資料庫的時候執行,實際上是在第一次失掉SQLiteDatabse對象的時候,才會調用這個方法.

public void onCreate(SQLiteDatabase db) {
System.out.println("create a Database");
//execSQL函數用於執行SQL語句
db.execSQL("create table user(id int,name varchar(20))");
}

上面是寫好的例子,可以參考下

複製代碼 代碼如下: public class DBHelper {

private static DatabaseHelper mDbHelper;
private static SQLiteDatabase mDb;

private static final String DATABASE_NAME = "life";

private static final int DATABASE_VERSION = 1;

private Context mCtx;

private static class DatabaseHelper extends SQLiteOpenHelper {

DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
//創立表結構
db.execSQL("CREATE TABLE life_history (id INTEGER PRIMARY KEY AUTOINCREMENT,type TEXT, city TEXT, company TEXT, pucNum TEXT, pucName TEXT);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

public DBHelper(Context ctx) throws SQLException {
this.mCtx = ctx;
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
}

/**
* 關閉資料來源
*/
public void closeConnection() {
if (mDb != null && mDb.isOpen())
mDb.close();
if (mDbHelper != null)
mDbHelper.close();
}

/**
* 插入資料 參數
* @param tableName 表名
* @param initialValues 要插入的列對應值
* @return
*/
public long insert(String tableName, ContentValues initialValues) {

return mDb.insert(tableName, null, initialValues);
}

/**
* 刪除資料
* @param tableName 表名
* @param deleteCondition 條件
* @param deleteArgs 條件對應的值(如果deleteCondition中有“?”號,將用此數組中的值替換,一一對應)
* @return
*/
public boolean delete(String tableName, String deleteCondition, String[] deleteArgs) {

return mDb.delete(tableName, deleteCondition, deleteArgs) > 0;
}

/**
* 更新資料
* @param tableName 表名
* @param initialValues 要更新的列
* @param selection 更新的條件
* @param selectArgs 更新條件中的“?”對應的值
* @return
*/
public boolean update(String tableName, ContentValues initialValues, String selection, String[] selectArgs) {
return mDb.update(tableName, initialValues, selection, selectArgs) > 0;
}

/**
* 取得一個列表
* @param distinct 是否去重複
* @param tableName 表名
* @param columns 要返回的列
* @param selection 條件
* @param selectionArgs 條件中“?”的參數值
* @param groupBy 分組
* @param having 分組過濾條件
* @param orderBy 排序
* @return
*/
public Cursor findList(boolean distinct, String tableName, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {

return mDb.query(distinct, tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
}

/**
* 取得單行記錄
* @param tableName 表名
* @param columns 擷取的列數組
* @param selection 條件
* @param selectionArgs 條件中“?”對應的值
* @param groupBy 分組
* @param having 分組條件
* @param orderBy 排序
* @param limit 資料區間
* @param distinct 是否去重複
* @return
* @throws SQLException
*/
public Cursor findOne(boolean distinct, String tableName, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) throws SQLException {

Cursor mCursor = findList(distinct, tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);

if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;

}

/**
* 執行SQL(帶參數)
* @param sql
* @param args SQL中“?”參數值
*/
public void execSQL(String sql, Object[] args) {
mDb.execSQL(sql, args);

}

/**
* 執行SQL
* @param sql
*/
public void execSQL(String sql) {
mDb.execSQL(sql);

}

/**
* 判斷某張表是否存在
* @param tabName 表名
* @return
*/
public boolean isTableExist(String tableName) {
boolean result = false;
if (tableName == null) {
return false;
}

try {
Cursor cursor = null;
String sql = "select count(1) as c from sqlite_master where type ='table' and name ='" + tableName.trim() + "'";
cursor = mDb.rawQuery(sql, null);
if (cursor.moveToNext()) {
int count = cursor.getInt(0);
if (count > 0) {
result = true;
}
}

cursor.close();
}
catch (Exception e) {
}
return result;
}

/**
* 判斷某張表中是否存在某欄位(注,該方法無法判斷表是否存在,因此應與isTableExist一起應用)
* @param tabName 表名
* @param columnName 列名
* @return
*/
public boolean isColumnExist(String tableName, String columnName) {
boolean result = false;
if (tableName == null) {
return false;
}

try {
Cursor cursor = null;
String sql = "select count(1) as c from sqlite_master where type ='table' and name ='" + tableName.trim() + "' and sql like '%" + columnName.trim() + "%'";
cursor = mDb.rawQuery(sql, null);
if (cursor.moveToNext()) {
int count = cursor.getInt(0);
if (count > 0) {
result = true;
}
}

cursor.close();
}
catch (Exception e) {
}
return result;
}

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.