① 「android query 」模糊查詢怎麼使用
關於Android中 Cursor 的query加入模糊查詢的條件,有如下方式:
1.使用這種query方法%號前不能加',以下為示例代碼:
Cursor c_test = mDatabase.query(tab_name, new String[]{tab_field02}, tab_field02+" LIKE ? ",
new String[] { "%" + str[0] + "%" }, null, null, null);
2.使用這種query方法%號前必須加',以下為示例代碼 :
Cursor c_test=mDatabase.query(tab_name, new String[]{tab_field02},tab_field02+" like '%" + str[0] + "%'", null, null, null, null);
3.使用這種方式必須在%號前加' ,以下為示例代碼 :
String current_sql_sel = "SELECT * FROM "+tab_name +" where "+tab_field02+" like '%"+str[0]+"%'";
Cursor c_test = mDatabase.rawQuery(current_sql_sel, null);
② Android 中資料庫查詢方法query() 中的selectionArgs 參數只能在編譯之前確定,這怎麼實現動態查詢
網上找來的
Android 中涉及資料庫查詢的地方一般都會有一個 query() 方法,而這些 query 中有大都(全部?)會有一個參數 selectionArgs,比如下面這個 android.database.sqlite.SQLiteDatabase.query():
view plain to clipboardprint?
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
selection 參數很好理解,就是 SQL 語句中 WHERE 後面的部分,即過濾條件, 比如可以為 id=3 AND name='Kevin Yuan' 表示只返回滿足 id 為 3 且 name 為 "Kevin Yuan" 的記錄。
再實際項目中像上面那樣簡單的「靜態」的 selection 並不多見,更多的情況下要在運行時動態生成這個字元串,比如
view plain to clipboardprint?
public doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"id=" id " AND name='" name "'", // selection
//...... 更多參數省略
);
}
public doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"id=" id " AND name='" name "'", // selection
//...... 更多參數省略
);
}
在這種情況下就要考慮一個字元轉義的問題,比如如果在上面代碼中傳進來的 name 參數的內容裡面有單引號('),就會引發一個 "SQLiteException syntax error .... "。
手工處理轉義的話,也不麻煩,就是 String.replace() 調用而已。但是 Android SDK 為我們准備了 selectionArgs 來專門處理這種問題:
view plain to clipboardprint?
public void doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"id=" id " AND name=?", // selection
new String[] {name}, //selectionArgs
//...... 更多參數省略
);
// ...... 更多代碼
}
public void doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"id=" id " AND name=?", // selection
new String[] {name}, //selectionArgs
//...... 更多參數省略
);
// ...... 更多代碼
}
也就是說我們在 selection 中需要嵌入字元串的地方用 ? 代替,然後在 selectionArgs 中依次提供各個用於替換的值就可以了。在 query() 執行時會對 selectionArgs 中的字元串正確轉義並替換到對應的 ? 處以構成完整的 selection 字元串。 有點像 String.format()。
不過需要注意的是 ? 並不是「萬金油」,只能用在原本應該是字元串出現的地方。比如下面的用法是錯誤的:
view plain to clipboardprint?
public void doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"? = " id " AND name=?", // selection XXXX 錯誤!? 不能用來替換欄位名
new String[]{"id", name}, //selectionArgs
//...... 更多參數省略
);
// ...... 更多代碼
}
③ android資料庫 怎麼查找id=4 用db.query()這個函數怎麼寫
Cursor cursor = sqLiteDatabase.query(true, Table,new String[]{需要查找的列}, id + "= 4" , null, null, null, null, null);
④ android 如何連接資料庫
這種方式通常連接一個外部的資料庫,第一個參數就是資料庫文件,這個資料庫不是當前項目中生成的,通常放在項目的Assets目錄下,當然也可以在手機內,如上面參數那個目錄,前提是那個文件存在且你的程序有訪問許可權。
另一種使用資料庫的方式是,自己創建資料庫並創建相應的資料庫表,參考下面的代碼:
public class DatabaseHelper extends SQLiteOpenHelper {
//構造,調用父類構造,資料庫名字,版本號(傳入更大的版本號可以讓資料庫升級,onUpgrade被調用)
public DatabaseHelper(Context context) {
super(context, DatabaseConstant.DATABASE_NAME, null, DatabaseConstant.DATABASE_VERSION);
}
//資料庫創建時調用,裡面執行表創建語句.
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createVoucherTable());
}
//資料庫升級時調用,先刪除舊表,在調用onCreate創建表.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DatabaseConstant.TABLE_NAME);
onCreate(db);
}
//生成 創建表的SQL語句
private String createVoucherTable() {
StringBuffer sb = new StringBuffer();
sb.append(" CREATE TABLE ").append(DatabaseConstant.TABLE_NAME).append("( ").append(「ID」)
.append(" TEXT PRIMARY KEY, ")
.append(「USER_ID」).append(" INTEGER, ").append(「SMS_CONTENT」).append(" TEXT ) ");
return sb.toString();
}
} 繼承SQLiteOpenHelper並實現裡面的方法.
之後:
//得到資料庫助手類
helper
=
new
DatabaseHelper(context);
//通過助手類,打開一個可讀寫的資料庫連接
SQLiteDatabase
database
=
helper.getReadableDatabase();
//查詢表中所有記錄
database.query(DatabaseConstant.TABLE_NAME,
null,
null,
null,
null,
null,
null);
⑤ Android用query怎麼進行多條件查詢
SQLiteDatabase 給我提供的方法很不實用,還是建議樓主自己寫sql語句,參數想怎麼傳都可以
例如:Cursor c = db.rawQuery("select * from user where username=? and password = ?",
new Stirng[]{"用戶名","密碼"});
如果你非要調用SQLiteDatabase的query方法,那可以這樣
db.query("表名", new String[]{"欄位1,欄位2"}, "條件1=? and 條件2=?", new String[]{"條件1的值,條件2的值"},null,null,null)
⑥ android怎麼查詢資料庫中的
public
void
query(char
word)
{
string[]
columns
=
{
"id",
"cnword",
"bh"
};//
表的column名稱
//
獲取資料庫實例,根據實際情況修改
sqlitedatabase
database
=
sqlitedatabase.openorcreatedatabase(
"/data/data/com.example.cx/databases/ghydb.db",
null);
cursor
cursor
=
null;
try
{
cursor
=
database.query("hanzibihua",
columns,
"cnword=?",
new
string[]
{
string.valueof(word)
},
null,
null,
null);
while
(null
!=
cursor
&&
cursor.movetonext())
{//
資料庫中一個漢字如果存有多個,循環獲取
cursor.getstring(cursor.getcolumnindex("bh"));//
這里獲取筆畫
}
}
catch
(exception
e)
{
e.printstacktrace();
//
todo:
handle
exception
}
finally
{
if
(null
!=
cursor)
{
cursor.close();
}
}
}