2011-11-16 13 views
0

일부 sqlite 데이터베이스에서 작업하고 하나의 테이블을 만들고 데이터를 성공적으로 삽입했습니다. 데이터를 가져와 텍스트 뷰에 표시하는 방법은 무엇입니까?sqlite 테이블에서 데이터를 가져와 android의 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); 
    }` 

내 메인 클래스에 다음

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

} 

답변

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

}

그것이하여 여러개의 데이터가 하나 이상의 텍스트 뷰에 저장합니다. 당신은

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

다음 목록에서 데이터를 가져 와서 텍스트 뷰 설정, 그것을 목록을 저장 한 후 다음과 같이 목록에서 데이터를 가져 와서 텍스트 뷰에 설정해야합니다. 추가 참조를 위해 this 링크를 확인하십시오.

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); 
관련 문제