-1

我创建了一个包含三列的表idname并且discipline。我想找到给定的学生姓名discipline

以下是我的方法:

String findstudent(String disc){
    SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
    String find = "SELECT * FROM  "+ TABLE_STUDENTS + " WHERE "+KEY_DISCIPLINE +" = " +disc ;
    Cursor cursor = sqLiteDatabase.rawQuery(find,null);
    cursor.moveToFirst();
    String found =cursor.getString(1);

    return  found;
}

当我使用它时,应用程序停止工作。

4

4 回答 4

1

试试下面的代码:

String find = "SELECT *<enter code here> 
               FROM  "+ TABLE_STUDENTS + " 
               WHERE "+KEY_DISCIPLINE +" = '" + disc + "'" ;
于 2014-10-31T16:59:24.007 回答
1

我不知道。但是,你可以试试这个方法

Cursor.moveToFirst();

do{

//get the data

String found =cursor.getString(cursor.getColumnIndexOrThrow("COLUMN NAME"));

}while(Cursor.moveNext);
于 2013-08-16T10:48:54.883 回答
0

尝试这个:

String findstudent(String disc){

    Cursor cursor;

    cursor = this.db.query(TABLE_STUDENTS,null, KEY_DISCIPLINE +" = "+ disc, null, null, null, null,null );

    cursor.moveToFirst();

    String found =cursor.getString(0);

    return  found;
}

希望能帮助到你!!

于 2013-08-16T10:22:22.850 回答
0
public Cursor findstudent(String disc){
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
String[] find = new String[]{ col1,col2,col3};//all fields in db in need
String order=col1;
Cursor c=db.query(TABLE_STUDENTS, find, KEY_DISCIPLINE +" = " +disc, null, null, null, order);
 c.moveToFirst();
    db.close();
    return c;
}

在活动中包括:

DataBaseHandler db = new DataBaseHandler(this);
try {
        Cursor c = db.displayName(your_disc);

        startManagingCursor(c);

        if (!c.moveToFirst()) {
            System.out.println("!Move " + posnumber);

        } else {
            String idname = c.getString(c
                    .getColumnIndex(DataBaseHandler.KEY_NAME));
            System.out.println("null:");
            System.out.println("Move " + idname);
            return true;
        }
        c.close();
        stopManagingCursor(c);
    } catch (Exception ex) {
    }
于 2013-08-16T10:33:00.003 回答