我已经经历了很多教程和解答,但是我并没有完全理解如何从sqlite数据库检索单列。我也使用pojo类。我会附上我的样本代码,请帮我离开这里...... 这是我的POJO类:如何从android中的sqlite数据库中检索单列数据并在TextView中显示
String health_center;
public void setHealth_center(String health_center)
{
this.health_center = health_center;
}
public String getHealth_center()
{
return health_center;
}
这是DBHelper类:
private static String TABLE_HEALTH = "health";
String health_center = "health_center";
public long addHealthCenter(Health_Pojo cse)
{
database = getWritableDatabase();
values = new ContentValues();
values.put(health_center, cse.getHealth_center());
long count = database.insert(TABLE_HEALTH, null, values);
if(count != -1)
{
Log.d("count",""+count);
}
database.close();
return count;
}
Health_Pojo getHealth_Pojo(String username)
{
Health_Pojo cse = null;
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
Cursor cursor = database.query(TABLE_HEALTH,null,loginUsername+"=?",new String[]{username},null,null,null);
if (cursor != null && cursor.getCount() > 0)
{
cursor.moveToFirst();
cse = new Health_Pojo();
cse.setHealth_center(cursor.getString(0));
@Override
public void onCreate(SQLiteDatabase db) {
String query = " create table " + TABLE_HEALTH + " (" + health_center + " text " + ") " ;
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(" drop table if exists " + TABLE_HEALTH);
onCreate(db);
}
这是我的活动课:
public class HealthAcitivty extends Activity {
EditText health_center;
Button save;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.health_acitivty);
/*Edit Text*/
health_center= (EditText) findViewById(R.id.healthCenter);
findViewById(R.id.cs_save_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Health_Pojo obj = new Health_Pojo();
obj.sethealth_center(health_center.getText().toString());
DBHelper db = new DBHelper(HealthActivity.this);
long count = db.addHealthCenter(obj);
startActivity(new Intent(HealthAcitivty.this, ViewHealth.class));
这是我ViewHealth类:
public class ViewHealth extends AppCompatActivity {
TextView tv;
SQLiteDatabase sqLiteDatabase;
Button update, save;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_health);
update= (Button) findViewById(R.id.update);
save = (Button) findViewById(R.id.save);
DBHelper db = new DBHelper(this);
//Here I want show the health_center name in textView But I didn't understand how to display.
我的大括号在我的程序中完全关闭。 请帮我解决这个问题.. 在此先感谢。
您的'getHealth_Pojo'工作正常吗? *如何从sqlite数据库中检索单列*,您已经为此编写了代码,您试图检索哪一行? –
getHealth_Pojo方法括号没有正确关闭:)如果它的实际代码:)请正确关闭它们,并调用一切应该没问题的方法:D –
我想检索health_center专栏@ Shree Krishna –