2013-11-27 88 views
0

这是我的SQLite类文件如何从数据库中获取多列值光标对象

public class MySqlite extends SQLiteOpenHelper { 

    private static int DATABASE_VERSION = 1; 
    private static String DATABASE_NAME = "student_info"; 

    public MySqlite(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase sqlite) { 
     // TODO Auto-generated method stub 
     String query = "CREATE TABLE student_datatable(first_name Text, last_name Text, gender Text, street Text, city Text, contact Text)"; 
     sqlite.execSQL(query); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase sqlite, int arg1, int arg2) { 
     // TODO Auto-generated method stub 
     sqlite.execSQL("DROP TABLE IF EXISTS student_datatable"); 
     onCreate(sqlite); 
    } 

    public void addrecord(String firstname, String lastname, String radiovalue, 
      String street, String city, String contact) { 
     SQLiteDatabase sqlite = this.getWritableDatabase(); 
     ContentValues cv = new ContentValues(); 
     cv.put("first_name", firstname); 
     cv.put("last_name", lastname); 
     cv.put("gender", radiovalue); 
     cv.put("street", street); 
     cv.put("city", city); 
     cv.put("contact", contact); 

     sqlite.insert("student_datatable", null, cv); 
     sqlite.close(); 
    } 

    public String searchrecord(String firstname) 
    { 
     SQLiteDatabase sql = this.getReadableDatabase(); 
     String lastnamefromtable = null; 
     String param[] = new String[1]; 
     param[0] = firstname; 
     Cursor c = sql.rawQuery(
       "Select * from student_datatable where first_name=?", param); 
     if (c.moveToFirst()) { 
      do { 
       lastnamefromtable = c.getString(c.getColumnIndex("last_name")); 

      } while (c.moveToNext()); 
     } 
     return lastnamefromtable; 
    } 

    public void deleterecord(String firstname) { 
     String Table_name = "student_info"; 
     String column_name = "first_name"; 
     SQLiteDatabase sql = this.getWritableDatabase(); 
     String lastnamefromtable = null; 
     String param[] = new String[1]; 
     param[0] = firstname; 
     sql.execSQL("DELETE FROM student_info where first_name=?", param); 
    } 

} 

在这里,我根据名字只是名字取,但我想取其它的值也如性别,街道,城市&接触从数据库光标和光标到我的以下活动

public class Searchrecord extends Activity { 

    EditText firstname; 
    TextView getlastname; 
    MySqlite mysql; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.searchrecord); 
     firstname=(EditText) findViewById(R.id.editText1); 
     getlastname=(TextView) findViewById(R.id.textView2); 
     mysql=new MySqlite(getApplicationContext()); 
    } 

    public void search(View v) 
    { 
     if(firstname.getText().toString().equals("")) 
     { 
      Toast.makeText(getApplicationContext(), "Please Provide value", Toast.LENGTH_SHORT).show(); 
     } 
     else 
     { 
      String value=mysql.searchrecord(firstname.getText().toString()); 
      getlastname.setText(value); 
     } 

    } 

} 
+0

因为你没有检索到的姓氏,查询返回的所有列和你的代码只读取1。 –

回答

1

请尽量follwing代码

 public ArrayList<String> searchrecord(String firstname) 
    { 
    ArrayList<String> allitems=new ArrayList<String>(); 
    SQLiteDatabase sql = this.getReadableDatabase(); 
    String lastnamefromtable = null; 
    String param[] = new String[1]; 
    param[0] = firstname; 
    Cursor c = sql.rawQuery(
     "Select * from student_datatable where first_name=?", param); 
    if (c.moveToFirst()) { 
    do { 
     lastnamefromtable = c.getString(c.getColumnIndex("last_name")); 
     allitems.add(lastnamefromtable) 

    } while (c.moveToNext()); 
    } 
    return allitems; 
} 

inMain活动

ArrayList<String> allitems=db.searchrecord(String firstname); 
iterate for loop 
0

以同样的方式替换你的方法,如下面,

public Bundle searchrecord(String firstname) 
{ 
    SQLiteDatabase sql = this.getReadableDatabase(); 
    Bundle cursorData = new Bundle(); 
    String param[] = new String[1]; 
    param[0] = firstname; 
    Cursor c = sql.rawQuery(
      "Select * from student_datatable where first_name=?", param); 
    if (c.moveToFirst()) { 
     do { 

      bundle.putString("last_name", c.getString(c.getColumnIndex("last_name")); 
      bundle.putString("gender", c.getString(c.getColumnIndex("gender")); 
      bundle.putString("street", c.getString(c.getColumnIndex("street")); 
      bundle.putString("city", c.getString(c.getColumnIndex("city")); 
      bundle.putString("contact", c.getString(c.getColumnIndex("contact")); 

     } while (c.moveToNext()); 
    } 
    return cursorData; 
} 

public void search(View v) 
{ 
    if(firstname.getText().toString().equals("")) 
    { 
     Toast.makeText(getApplicationContext(), "Please Provide value", Toast.LENGTH_SHORT).show(); 
    } 
    else 
    { 
     Bundle bundle=mysql.searchrecord(firstname.getText().toString()); 
     getlastname.setText(bundle.get("last_name")); 
     String gender=bundle.get("gender"); 
     String contact=bundle.get("contact"); 
     String street=bundle.get("street"); 
     String city=bundle.get("city"); 

    } 

}