0

I have been working on some sqlite database and i have created one table,and have successfully inserted data,How to fetch the data and display in a textview?

 public class EmployeeTable{     
 public static final String KEY_NAME = "emp_name";        
 public static final String KEY_DESIGNATION = "emp_designation";        
 public static final String KEY_ROWID = "_id";      

 private static final String TAG = "EmployeeTable";    
 private DatabaseHelper mDbHelper;    
 private SQLiteDatabase mDb;  

 private static final String DATABASE_NAME = "employee_database";    
 private static final String DATABASE_TABLE = "employee";    
 private static final int DATABASE_VERSION = 3;

 /**
  * Database creation sql statement
  */  
 private static final String DATABASE_CREATE =
   "create table " + DATABASE_TABLE + " (" + KEY_ROWID + " integer primary key    autoincrement, "
   + KEY_NAME +" text not null, " + KEY_DESIGNATION + " text not null);";

 private final Context mCtx;

 private static class DatabaseHelper extends SQLiteOpenHelper {

   DatabaseHelper(Context context) {
     super(context, DATABASE_NAME, null, DATABASE_VERSION);
   }

   @Override
   public void onCreate(SQLiteDatabase db) {
     Log.i(TAG, "Creating DataBase: " + DATABASE_CREATE);
     db.execSQL(DATABASE_CREATE);
   }`

following in my main class:

    public class SqliteDBActivity extends Activity {  
     /** Called when the activity is first created. */  
      private static final String TAG = "EmployeeTable";  
       @Override  
       public void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.main);  
           EmployeeTable employeeTable = new EmployeeTable(this);  
        employeeTable.open();  
        employeeTable.createEmployee("Prashant Thakkar", "Tech Lead");  
        employeeTable.createEmployee("Pranay anand", "Tech Lead");  
        employeeTable.createEmployee("rajeev kumar", "Tech Lead");  
        Log.i(TAG, "Fetching record...");  
      employeeTable.close();   
    }

}
0

2 Answers 2

3
   this.db = openHelper.getWritableDatabase();

       Cursor cursor = this.db.query(constantValues.TABLE_NAME, new String[] { "emailid" },null, null, null, null, null);
  if (cursor.moveToFirst()) {
     do {
        String emailid=cursor.getString(0); // Here you can get data from table and stored in string if it has only one string.
       textview.setText(emailid);


     } while (cursor.moveToNext());
  }
  if (cursor != null && !cursor.isClosed()) {
     cursor.close();
  }
  if(db!=null)
  {
      db.close();
  }

}

if it has mutiple data and stored in more than one textview. you have to store it list and then take the data from list and set to the textview as follows,

     List<String> list = new ArrayList<String>();
     Cursor cursor = this.db.query(constantValues.TABLE_NAME, new String[] { "emailid"},null, null, null, null, null); // here emailid is the field name in the table and contantValues.TABLE_NAME is the table name 
    if (cursor.moveToFirst()) {
     do {
        list.add(cursor.getString(0)); 

     } while (cursor.moveToNext());
  }

then take the data from list and set to the textview. for further reference check the this link

Sign up to request clarification or add additional context in comments.

Comments

1
private SQLiteDatabase database; DatabaseHelperClass dbHelper = new ExternalDbOpenHelper(this, nameofyourdatabase);

    database = dbHelper.openDataBase(); String qry = "select * from table WHERE yourcolumn"; String stringfortextview;


    Cursor c = database.rawQuery(qry, null);

    c.moveToFirst();
    if (!c.isAfterLast())
    {
        do
        {
            stringfortextview = c.getString(4);
        }
        while (c.moveToNext());
    }
    c.close(); TextView tv = (TextView)findViewById(R.id.textView); tv.setText(stringfortextview);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.