如何从android中的sqlite数据库中检索单列数据并在TextView中显示它

时间:2016-04-21 06:53:50

标签: java android sqlite

我已经完成了很多教程和SO答案,但我并没有完全理解如何从sqlite数据库中检索单列。我也在使用pojo课程。我会附上我的示例代码,请帮助我离开这里... 这是我的POJO课程:

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.

我的所有大括号在我的程序中完全关闭。 请帮我解决这个问题.. 提前致谢。

2 个答案:

答案 0 :(得分:0)

试试此代码

String[] colums = new String[]{"id"}; // add column names which you want to get from db
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
Cursor cursor = database.query(TABLE_HEALTH,colums,loginUsername+"=?",new String[]{username},null,null,null);

答案 1 :(得分:0)

要从SQLiteDatabase检索任何表的单个列,您需要使用SQLiteDatabase类的query()函数。此函数有许多重载形式。您可以使用适合您需要的那个。对于此答案,我们将使用public override void HandleAction (UIApplication application, string actionIdentifier, NSDictionary remoteNotificationInfo, Action completionHandler) { .... } 方法。

参见上面的方法,第二个参数是query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)。在这里,您需要传递要检索的列。如果传递null,将检索指定表中满足查询的所有列。如果只想检索一列,请在函数的第二个参数中指定列名,如下所示:

String[] columns

在上面的示例中,mytable是我的表名,col_1是我要从mytable中检索的列。

注意:列名的参数只接受String []。因此,即使您只想从表中检索一列,您也总是需要创建一个String []对象