Android 将文本更改为数据库值
按下按钮时,我想将文本更改为数据库COLLMN值,我知道这是错误的,但代码如下:Android 将文本更改为数据库值,android,database,sqlite,text,Android,Database,Sqlite,Text,按下按钮时,我想将文本更改为数据库COLLMN值,我知道这是错误的,但代码如下: private void MostraDados() { // TODO Auto-generated method stub final TextView text = (TextView) findViewById(R.id.tvUSUARIO); Button mostrar = (Button) findViewById(R.id.bMostrar); mostra
private void MostraDados() {
// TODO Auto-generated method stub
final TextView text = (TextView) findViewById(R.id.tvUSUARIO);
Button mostrar = (Button) findViewById(R.id.bMostrar);
mostrar.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
db = openOrCreateDatabase("dbtest.db", Context.MODE_PRIVATE, null);
String q = "SELECT * FROM dbtest.db WHERE usuarioorigem='";
text.setText(q);
//text.execSQL("DROP COLUMN IF EXISTS usuarioorigem");
}
});
}
SQL编写不正确。必须从列中选择。然后将查询字符串传递给文本视图。您应该首先回顾如何使用游标查询数据库,以及如何从游标检索所需内容 因此,我将研究如何使用curosr,所有这些都在Android文档中提供,您可能希望在模拟器中尝试API演示,我相信您也可以在那里学习如何使用光标。看这里
在这里,.获取光标后,您可以执行以下操作:
while(c.moveToNext(){
text.setText(c.getString(0))
}
您的代码缺少一些关键部分,例如管理游标和数据库的DatabaseClass
private void MostraDados() {
final TextView text = (TextView) findViewById(R.id.tvUSUARIO);
Button mostrar = (Button) findViewById(R.id.bMostrar);
mostrar.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// our missing a database helper
MyDatabaseClass dbhelper = new MyDatabaseClass();
dbhelper.open();
Cursor result = dbhelper.doMyQuery();
String mystring = result.getString(0);
text.setText(mystring);
dbhelper.close();
}
});
....
public class WorkoutDbAdapter {
....
public Cursor doMyQuery()
{
return this.mDb.query( yourQuery );
}
}
这是你需要的最低限度,即使有了上面的内容,我也遗漏了很多小细节。搜索有关创建和使用数据库的教程
但实际上,您需要返回光标,设置光标的位置或cursor.moveNext(),然后获取可以分配给textField的值
您的源代码缺少对数据库的正确调用和对游标的访问。希望您能找到一些不错的教程,为您充实剩下的内容。您能在我的代码中解释一下吗?嗯,我尝试了一些东西,但在我的setText中仍然出现错误。cursor=db.rawQuery(“从dbtest.db中选择*WHERE usuariorigem”,null,null);usuario.setText(光标);