我无法按名字和姓氏对数据库中的条目进行排序。另外,我希望能够根据某个学校或兴趣进行选择。我的数据库有五个字段:名字、姓氏、学校、电子邮件、兴趣。这是我的代码:
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
}
}
}