Reputation: 75
I have gone through lot of tutorials and SO answers but I didn't Understand exactly how to retrieve single column from sqlite database. I am using pojo class also. I will attach my sample code please help me out of here... This is my POJO class:
String health_center;
public void setHealth_center(String health_center)
{
this.health_center = health_center;
}
public String getHealth_center()
{
return health_center;
}
This is DBHelper class:
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);
}
This is My Activity Class:
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));
This is my ViewHealth Class:
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.
All my Braces are closed perfectly in my program. Please help me to solve this problem.. Thanks in advance.
Upvotes: 0
Views: 2171
Reputation: 1261
To retrieve a single column of any table from the SQLiteDatabase, you need to use query() function of the SQLiteDatabase class. This function has many overloaded forms. You can use the one that suits your need. For this answer we will use query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
method.
See the method above, the 2nd argument is String[] columns
. Here you need to pass the columns which you want to retrieve. If you pass null, all the columns from the specified table which satisfy the query will be retrieved. If you want to retrieve only one column, specify the column name in the 2nd argument in the function as shown below:
query(mytable, new String[] {col_1}, null, null, null, null, null, null);
In the example above, mytable is my table's name and col_1 is the column which I want to retrieve from mytable.
NOTE: The argument for Column names only accepts String[]. So you will always need to create a String[] object even if you want to retrieve only one column from the table
Upvotes: 1
Reputation: 6067
Try this code
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);
Upvotes: 0