2014-12-28 65 views
-1

您好我正在使用SQLite,並且我已經將所有數據都插入到數據庫中,我如何檢索它?
這是我的插入方法如何從數據庫中獲取所有值

public void insert(String username, String password, String email) { 
    ContentValues values = new ContentValues(); 
    values.put(UsersSQLiteHelper.COLUMN_UNAME, username); 
    values.put(UsersSQLiteHelper.COLUMN_PASS, passward); 
    values.put(UsersSQLiteHelper.COLUMN_EMAIL, email); 
    long insertId = database.insert(UsersSQLiteHelper.TABLE_USERS, null,values); 
} 

回答

1
public List<YOUROBJECT> getAllInstruments() { 
    // create a list 
    List<YOUROBJECT> list = new ArrayList< YOUROBJECT>(); 

    Cursor cursor = database.query(UsersSQLiteHelper.TABLE_USERS, 
    allColumns, null, null, null, null, null); 

    // check if cursor has columns 
    if (cursor.getColumnCount() > 0) { 
    // read all cursor values 
    while (cursor.moveToNext()) { 
     YOUROBJECT newObject = new YOUROBJECT(); 
     // now you have to get the values from the cursor and add them to your object 
     // like this 
     String userName= cursor.getString(cursor.getColumnIndex(COLUMN_UNAME)); 
     // COLUMN_UNAME is the name of the column in your database that you want the value of 

     // now add the values to the Instrument object 
     newObject.setUserName(userName); 

     // now add the object to the list 
     list.add(newObject); 
    } 
} 

cursor.close(); 
return list; //finally return the list 

}

+0

感謝您的幫助 – user2897452

+1

歡迎您 –

相關問題