Android SQLiteDatabase使用注意點
1、建立或刪除資料表:
如:
SQLiteDatabase test = SQLiteDatabase.openDatabase(LOG_DBPATH, null, SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY); String strSql = "create table if not exists " + LOG_DBTAB_NAME + "(id integer primary key AUTOINCREMENT, time text)"; test.execSQL(strSql);
基本流程是:開啟資料庫,執行SQL語句進行各項操作,關閉資料庫
SQLite資料庫開啟標識 標識型別 說明
SQLiteDatabase. CREATE_IF_NECESSARY 按需建立
SQLiteDatabase. NO_LOCALIZED_COLLATORS 不使用本地化校驗
SQLiteDatabase. OPEN_READONLY 只讀方式
SQLiteDatabase. OPEN_READWRITE 讀寫方式
2、往資料庫表中 新增列
/** *新增列 */ public void addColumnByBySQL(String tableName, String columnName) { try { booleancheckColumnExists = checkColumnExists(tableName, columnName); if (!checkColumnExists) { String addColumnSQL = "alter table " + tableName + " add column " + columnName + " varchar"; db.execSQL(addColumnSQL); Log.d("addColumnByBySQL", addColumnSQL); } } catch(Exception e) { e.printStackTrace(); } } /** *判斷表中是否存在列 * @param tableName 表名 * @param columnName 列名 * @return */ public boolean checkColumnExists(String tableName, String columnName) { boolean result = false; Cursor cursor = null; try { cursor = db.rawQuery("select * from sqlite_master where name = ? and sql like ?", new String[]{tableName, "%" + columnName + "%"}); result = null != cursor && cursor.moveToFirst(); } catch (Exception e) { Log.e("tag", "checkColumnExists2..." + e.getMessage()); } finally { if (null != cursor && !cursor.isClosed()) { cursor.close(); } } return result; }
3、建立資料庫索引
/** * 通過sql建立索引 * @param indexName 索引名稱 * @param tableName 表名 * @param columnNameStr 表中需要新增索引的列表,可以是 多個列 * @return */ private String addIndexBySQL(String indexName, String tableName, String columnNameStr) { String sqlStr = ""; sqlStr = " create index if not exists " + indexName + " on " + tableName + "(" + columnNameStr + ")"; return sqlStr; }
例子:
/** * 新增索引 */ public void addIndexOnTable() { try { List<String> sqlStrs = new ArrayList<>(); // JTYS_KHQYB 新增索引cardNum sqlStrs.add(addIndexBySQL("index_khqyb_creater", "JTYS_KHQYB", "cardNum")); // JTYS_FWMYD 新增索引(creater,satisfiedState) sqlStrs.add(addIndexBySQL("index_fwmyd_creater", "JTYS_FWMYD", "creater,satisfiedState")); for (int i = 0, size = sqlStrs.size(); i < size; i++) { db.execSQL(sqlStrs.get(i)); Log.e("addIndexOnTable", sqlStrs.get(i)); } } catch (Exception e) { e.printStackTrace(); } }
4、對於資料庫中某個列名含有多個,拼接並需要查詢單個值的數量時 SQL這樣寫
(','||POPULATION_TYPE||',') 為 預設為 列 POPULATION_TYPE 預設左右都新增 ,,
匹配的時候 ,type, 匹配
例子 :
POPULATION_TYPE 結果值中有 2 2,3 1,2 三個結果值時,預設所有結果值都被加上,,
如 2 變成 ,2,
2,3 變成 ,2,3,
1,2 變成 ,1,2,
/**
* 查詢類別 1:一般人群,2:兒童,3:孕產婦,4:老年人,5:重精神病,6:高血壓,7:糖尿病,8:冠心病,9:腦卒中 10 殘疾人預設值1
*/
public int getCountByJDRType(String str_jdrno, String str_type) {
int count = 0;
try {
String strCountByJDRTypeSql = "select count(ID) from ARCHIVE_BASEINFO where CREATER='" + str_jdrno
+ "' and CREATED_BY='" + str_jdrno + "' and (','||POPULATION_TYPE||',') like '%," + str_type
+ ",%'";
Cursor cursor = db.rawQuery(strCountByJDRTypeSql, null);
Log.w("sql---查詢疾病型別種類" + str_type, strCountByJDRTypeSql);
cursor.moveToFirst();
long cout_long = cursor.getLong(0);
count = (int) cout_long;
cursor.close();
} catch (Exception e) {
e.printStackTrace();
count = 0;
}
return count;
}