如何使用SQLite数据库按Android中的名字和姓氏进行排序?

时间:2013-05-28 00:09:55

标签: android android-sqlite sqlite

我无法按名字和名字对数据库中的条目进行排序。此外,我希望能够根据某所学校或兴趣进行选择。我的数据库有五个字段:名字,姓氏,学校,电子邮件,兴趣。这是我的代码:

    public class StudentRecruit {

public static final String MYDATABASE_NAME = "MY_DATABASE";
 public static final String MYDATABASE_TABLE = "MY_TABLE";
 public static final int MYDATABASE_VERSION = 1;
 public static final String KEY_CONTENT = "Content";
 public static final String lastName = "lastName";
 public static final String firstName = "firstName";
 public static final String school = "school";
 public static final String email = "email";
 public static final String intrest = "intrest";

 private SQLiteHelper sqLiteHelper;
 private SQLiteDatabase sqLiteDatabase;
 private static final String SCRIPT_CREATE_DATABASE =
  "create table " + MYDATABASE_TABLE + " ("+firstName +" text not null, "+lastName+" text not null, "+school+" text not null, "+email+" text not null, "+intrest+" text not null,  );";




 private Context context;

 public StudentRecruit(Context c){
  context = c;
 }

 public StudentRecruit openToRead() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  sqLiteDatabase = sqLiteHelper.getReadableDatabase();
  return this; 
 }

 public StudentRecruit openToWrite() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  sqLiteDatabase = sqLiteHelper.getWritableDatabase();
  return this; 
 }

 public void close(){
  sqLiteHelper.close();
 }

 public double insert(String content, String content2, String content3,String content4,String content5){

  ContentValues contentValues = new ContentValues();

  contentValues.put(firstName, content);
  contentValues.put(lastName, content2);
  contentValues.put(school, content3);
  contentValues.put(email, content4);
  contentValues.put(intrest, content5);


  return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);

 }

 public int deleteAll(){
  return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
 }

/* public Cursor searchAll() {
     return sqLiteDatabase.query(MYDATABASE_TABLE, null, intrest, null, lastName, null, school);
 }*/


 public String queueAll(){
      String[] columns = new String[]{firstName};
      Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, 
        null, null, null, null, null);
      String result = "";

      int index_CONTENT = cursor.getColumnIndex(firstName);
      for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){
       result = result + cursor.getString(index_CONTENT) + "\n";

      }


      return result;

     }

 public class SQLiteHelper extends SQLiteOpenHelper {

    public SQLiteHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        // TODO Auto-generated method stub
        db.execSQL(SCRIPT_CREATE_DATABASE);
        Cursor d = sqLiteDatabase.rawQuery("SELECT * from " + MYDATABASE_TABLE + " ORDER BY " + lastName + " ASC" , null); // here I have edited space before ORDER BY word starts. Please note this
        Log.d("query",">>>>"+ "SELECT * from " + MYDATABASE_TABLE + " ORDER BY " + lastName + " ASC"); // check this query is going to right way or not using local database
                if(d != null){
                     if(d.getCount() > 0){  // to check you get one or more data 
                       d.moveToFirst();    
                         do{
                                 int sort = d.getColumnIndex(lastName);
                                 int sort2 = d.getColumnIndex(firstName);
                                 String lastName = d.getString(sort);
                                 String firstName = d.getString(sort2);
                                 //System.out.println("GOT STUDENT " +  lastName + " LIKES " + intrest);
                           } while (d.moveToNext());
                      }
                }


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub


}

 }

}

1 个答案:

答案 0 :(得分:0)

好的,首先:

private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("+firstName +" text not null, "+lastName+" text not null, "+school+" text not null, "+email+" text not null, "+intrest+" text not null,  );";

对我来说似乎不对,你的SQL查询以','结束:

private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("+firstName +" text not null, "+lastName+" text not null, "+school+" text not null, "+email+" text not null, "+intrest+" text not null);";

你说排序有麻烦,有什么麻烦? 有没有例外?
你得到任何输出?


但我想我知道你的问题在哪里:

您正在从SQLiteOpenHelper的onCreate方法查询数据库,此方法仅适用于数据库,仅适用于行db.executeSQL(SCRIPT_CREATE_DATABASE);

您必须使用queryAll()方法

查询数据库