如何从sqlite的表中提取数据,并在TextView的android系统中显示呢?并在、数据、系统、sqlite

2023-09-04 05:35:29 作者:无言

我一直在做一些SQLite数据库和我已创建了一个表,并已成功地插入数据,如何获取数据并显示在一个TextView?

 公共类EmployeeTable中{
 公共静态最后弦乐KEY_NAME =EMP_NAME;
 公共静态最后弦乐KEY_DESIGNATION =emp_designation;
 公共静态最后弦乐KEY_ROWID =_id;

 私有静态最后字符串变量=EmployeeTable中;
 私人DatabaseHelper mDbHelper;
 私人SQLiteDatabase MDB;

 私有静态最后弦乐DATABASE_NAME =employee_database;
 私有静态最后弦乐DATABASE_TABLE =员工;
 私有静态最终诠释DATABASE_VERSION = 3;

 / **
  *数据库创建的SQL语句
  * /
 私有静态最后弦乐DATABASE_CREATE =
   创建表+ DATABASE_TABLE +(+ KEY_ROWID +整数主键自动增量,
   + KEY_NAME +文字不为空,+ KEY_DESIGNATION +文字NOT NULL);;

 私人最终语境mCtx;

 私有静态类DatabaseHelper扩展SQLiteOpenHelper {

   DatabaseHelper(上下文的背景下){
     超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
   }

   @覆盖
   公共无效的onCreate(SQLiteDatabase DB){
     Log.i(TAG,创建数据库:+ DATABASE_CREATE);
     db.execSQL(DATABASE_CREATE);
   }`
 

在我的主类以下内容:

 公共类SqliteDBActivity延伸活动{
     / **第一次创建活动时调用。 * /
      私有静态最后字符串变量=EmployeeTable中;
       @覆盖
       公共无效的onCreate(包savedInstanceState){
           super.onCreate(savedInstanceState);
           的setContentView(R.layout.main);
           EmployeeTable中EmployeeTable中=新EmployeeTable中(本);
        employeeTable.open();
        employeeTable.createEmployee(PRASHANT THAKKAR,技术主管);
        employeeTable.createEmployee(Pranay阿南德,技术主管);
        employeeTable.createEmployee(拉杰夫·库马尔,技术主管);
        Log.i(TAG,抓取记录......);
      employeeTable.close();
    }

}
 

解决方案

  this.db = openHelper.getWritableDatabase();

       光标光标= this.db.query(constantValues​​.TABLE_NAME,新的String [] {EMAILID},NULL,NULL,NULL,NULL,NULL);
  如果(cursor.moveToFirst()){
     做 {
        串EMAILID = cursor.getString(0); //在这里,您可以从表中的数据,并存储在字符串,如果它只有一个字符串。
       textview.setText(EMAILID);


     }而(cursor.moveToNext());
  }
  如果(光标=空&安培;!&安培;!cursor.isClosed()){
     cursor.close();
  }
  如果(DB!= NULL)
  {
      db.close();
  }
 
android 从sqlite中获取的数据不一致

}

,如果它有多发性数据,并存储在一个以上的TextView。你必须存储它列表,然后取从列表中的数据,并设置到TextView的如下,

 名单,其中,字符串>名单=新的ArrayList<字符串>();
     光标光标= this.db.query(constantValues​​.TABLE_NAME,新的String [] {EMAILID},NULL,NULL,NULL,NULL,NULL); //这里EMAILID是在表中的字段名称,contantValues​​.TABLE_NAME是表名
    如果(cursor.moveToFirst()){
     做 {
        list.add(cursor.getString(0));

     }而(cursor.moveToNext());
  }
 

然后从列表中取数据,并设置到TextView的。进一步参考检查这链接

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();   
    }

}

解决方案

   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