在android中从SQLite数据库请求多个单元格
我试图从数据库中的两个不同单元调用数据,然后将它们组合起来并在活动中打印出来。
我正在使用下面的代码:
public Cursor getGermanDescription(String id) {
String[] args = { id };
return (getReadableDatabase()
.rawQuery(
"SELECT _id,Column1,Column2 FROM Databasing_Details WHERE _id=?",
args));
与上述我只获取Column1的内容,但不是Column2。 我将字符串ID传递给另一个活动。
我的光标适配器是:
@Override public void onCreate(Bundle savedInstanceState){try {super.onCreate(savedInstanceState); 的setContentView(R.layout.main);
//Get our passed variable from our intent's EXTRAS
passedVar=getIntent().getStringExtra(ListViewTwo.ID_EXTRA);
//this is our ListView element, obtained by id from our XML layout
ListView myListView = (ListView)findViewById(R.id.list_view);
String string = passedVar;
int passedInt = Integer.parseInt(string);
if (passedInt==1) { passedVar1 = true;
}
//create our database Helper
dbDescriptionHelper=new DatabaseHelper(this);
//a set of results from a database query
ourCursor=dbDescriptionHelper.getGermanDescription(passedVar);
//tell android to start managing the cursor,
startManagingCursor(ourCursor);
//create our adapter
adapter=new SlangAdapter(ourCursor);
//set the adapter!!!
myListView.setAdapter(adapter);
} catch (Exception e) {
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
e.printStackTrace();
}
return;
}
class SlangAdapter extends CursorAdapter {
SlangAdapter(Cursor c) {
super(ListViewFinal.this, c);
}
@Override
public void bindView(View row, Context ctxt,
Cursor c) {
DescriptionHolder holder=(DescriptionHolder)row.getTag();
holder.populateFrom(c, dbDescriptionHelper);
}
@Override
public View newView(Context ctxt, Cursor c,
ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.main_row, parent, false);
DescriptionHolder holder=new DescriptionHolder(row);
row.setTag(holder);
return(row);
}
}
static class DescriptionHolder {
private TextView name=null;
DescriptionHolder(View row) {
name=(TextView)row.findViewById(R.id.row);
}
void populateFrom(Cursor c, DatabaseHelper r) {
name.setText(r.getName(c));
}
}
请有人指出我要出错的地方。
好的,我找到了答案。
我的语法是不正确的,我需要为我的光标写的是:
public Cursor getGermanDescription(String id) {
String[] args = { id };
return (getReadableDatabase()
.rawQuery(
"SELECT _id, ObjectDescriptionGerman ||'nn'|| ObjectDescription FROM Databasing_Details WHERE _id=?",
args));
}
|| 需要用来代替“,”或“与”。 我也在我的两个返回值之间插入了换行符,所以我不需要在数据库中执行此操作。
链接地址: http://www.djcxy.com/p/49643.html