2013-05-28 24 views
0

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

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 


} 

} 

}

+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);"; 

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


但我想我知道你的问题是:

您quering从SQLiteOpenHelper的onCreate方法数据库,这种方法只适用于数据库中,只有线db.executeSQL(SCRIPT_CREATE_DATABASE);

您必须从queryAll()方法中查询您的数据库

相关问题