2016-04-21 92 views
-1

我已经经历了很多教程和解答,但是我并没有完全理解如何从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. 

我的大括号在我的程序中完全关闭。 请帮我解决这个问题.. 在此先感谢。

+0

您的'getHealth_Pojo'工作正常吗? *如何从sqlite数据库中检索单列*,您已经为此编写了代码,您试图检索哪一行? –

+0

getHealth_Pojo方法括号没有正确关闭:)如果它的实际代码:)请正确关闭它们,并调用一切应该没问题的方法:D –

+0

我想检索health_center专栏@ Shree Krishna –

回答

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); 
+0

嗨,我有大约120列检索。你能给出一种可能的方式来检索和显示结果。 –

+0

如果你想选择特定的列,然后添加到该数组,否则传递null和光标将具有所有列。 – androidnoobdev

0

为了取回SQLiteDatabase任何表中的一列,你需要使用查询(在SQLiteDatabase类)功能。这个函数有很多重载的表单。你可以使用适合你需要的那个。对于这个答案,我们将使用query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)方法。

查看上面的方法,第二个参数是String[] columns。在这里你需要传递你想要检索的列。如果您传递null,则会检索指定表中满足查询的所有列。如果你想检索只有一列,指定如下图所示,在该函数的第二个参数的列名:

query(mytable, new String[] {col_1}, null, null, null, null, null, null); 

在上面的例子中,mytable的是我的表的名称和COL_1是我想列从mytable中检索。

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

+0

感谢您的回复,我有120列检索。请给我一个最好的方式来显示... –

+0

你想同时检索和显示多少列? 120或更少? –

相关问题