我有一个使用SQLite数据库的Java程序。我需要获取存储在特定列和行中的值,然后将其转换为字符串。
有人能给我一个非常简单和通用的方法吗?我把它想象成类似于"get Name where ID=4“的东西。
发布于 2015-06-12 18:09:43
要获取值或多个值,您需要一个游标,请参见以下函数(例如,该函数返回最适合获取id的第一个值):
public String getSingular_Value_InTransaction(String query){
//Declaration of variables
Cursor a1 = null;
try{
a1 = database.rawQuery(query,null);
a1.moveToFirst();
if(a1.getString(0) != null){
String result = a1.getString(0);
a1.close();
return result;
}
else{
a1.close();
return "";
}
}
catch (NullPointerException ex){
return "";
}
catch (CursorIndexOutOfBoundsException ex){
return "";
}
catch (Exception ex){
Log.e("-- BDD.execute_SelectCommand --","Exception", ex);
return "";
}
}
告诉我我是不是帮了你,还有好的编程!
发布于 2015-06-12 18:07:15
使用这个方法,我想它会很有帮助。
public ArrayList<DbHelper> selectData() {
try {
Integer subCategoryArray[][] = null;
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
ArrayList<DbHelper> ar = new ArrayList<DbHelper>();
String strSQL = "SELECT * FROM " + TABLE_NAME;
Cursor cursor = db.rawQuery(strSQL, null);
if(cursor != null)
{
if (cursor.moveToFirst()) {
subCategoryArray = new Integer[cursor.getCount()][cursor.getColumnCount()];
int j= 0;
do {
subCategoryArray[j][0] = cursor.getInt(0);
subCategoryArray[j][1] = cursor.getInt(1);
subCategoryArray[j][2] = cursor.getInt(2);
subCategoryArray[j][3] = cursor.getInt(3);
subCategoryArray[j][4] = cursor.getInt(4);
subCategoryArray[j][5] = cursor.getInt(5);
subCategoryArray[j][6] = cursor.getInt(6);
subCategoryArray[j][7] = cursor.getInt(7);
subCategoryArray[j][8] = cursor.getInt(8);
subCategoryArray[j][9] = cursor.getInt(9);
subCategoryArray[j][10] = cursor.getInt(10);
subCategoryArray[j][11] = cursor.getInt(11);
subCategoryArray[j][12] = cursor.getInt(12);
DbHelper v= new DbHelper(subCategoryArray[j][0],subCategoryArray[j][1],subCategoryArray[j][2],subCategoryArray[j][3],subCategoryArray[j][4],subCategoryArray[j][5],subCategoryArray[j][6],subCategoryArray[j][7],subCategoryArray[j][8],subCategoryArray[j][9],subCategoryArray[j][10],subCategoryArray[j][11],subCategoryArray[j][12]);
ar.add(v);
j++;
} while (cursor.moveToNext());
}
}
cursor.close();
return ar;
} catch (Exception e) {
return null;
}
}
https://stackoverflow.com/questions/30800422
复制相似问题