0

当我执行搜索方法时,它给了我运行时错误

error:- 05-04 14:04:00.227: E/AndroidRuntime(4559): Caused by: 
android.database.sqlite.SQLiteException: no such column: ww (code 1): , 
while compiling: SELECT DISTINCT faculty, deparment, name, officeNumber, 
phoneNumber, emailAddress, officeHour FROM teacherInfo WHERE name=ww

当用户输入姓名时,我需要搜索教师姓名并显示有关该教师的所有信息,例如姓名,教师,部门,电话,电子邮件,办公号码,办公时间

这是 DBAdapter.java 中的 getRecord 方法

public Cursor getRecord(String n1) throws SQLException 
{       
    Cursor mCursor =db.query(true,tableName , new String[] {facultyc,
                deparmentc, namec, officeNumberc,Phonec ,emailAddressc,officeHourc},namec + "=" + n1, null, null, null, null, null);


    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

以及 Information.java 中的这种搜索方法

public void search(){
    db.open();
    Cursor c = db.getRecord(n);
    if (c.moveToFirst()){
        t1.setText(c.getString(0));
        t2.setText(c.getString(1));
        t3.setText(c.getString(2));
        t4.setText(c.getString(3));
        t5.setText(c.getString(4));
        t6.setText(c.getString(5));
        t7.setText(c.getString(6));

    }

    else
        Toast.makeText(this, "this Dr not found", Toast.LENGTH_LONG).show();
    db.close();
}

DBAdapter.java 中的这个 Oncreate 方法

public void onCreate(SQLiteDatabase db) 
DATABASE_CREATE =
        "create table if not exists teacherInfo (teacherNumber INTEGER primary key autoincrement," 
        + "name TEXT not null, faculty TEXT,deparment TEXT,officeNumber INTEGER,officeHour TEXT, emailAddress TEXT,phoneNumber TEXT, location INTEGER );";
            {
                try {
                    db.execSQL(DATABASE_CREATE);    
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
4

4 回答 4

2
Cursor mCursor =db.query(true,tableName , new String[] {facultyc,
            deparmentc, namec, officeNumberc,Phonec ,emailAddressc,officeHourc},namec + "= ?", new String[] { n1 }, null, null, null, null);

字符串必须用引号引起来。

于 2013-05-04T14:37:14.037 回答
2

问题是您错过了字符串的引号!

更好地使用这样的字符串参数:

public Cursor getRecord(String n1) throws SQLException 
{       
    Cursor mCursor =db.query(true,tableName , new String[] {facultyc,
                deparmentc,namec,officeNumberc,Phonec,emailAddressc,officeHourc},
                namec + "= ? ", 
                new String[] {ww}, null, null, null, null);


    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

通过这种方式,您可以避免任何错误,并且字符串特殊字符也会自动转义!

于 2013-05-04T14:37:21.453 回答
0

您输入了错误的查询。将其更改为 ->

Cursor mCursor =db.query(true,tableName , new String[] {facultyc,
                deparmentc, namec, officeNumberc,Phonec ,emailAddressc,officeHourc},"name = " + n1, null, null, null, null, null);
于 2013-05-04T14:38:49.777 回答
0

您的查询未经过清理。它应该是:

SELECT DISTINCT faculty, deparment, name, officeNumber, 
    phoneNumber, emailAddress, officeHour FROM teacherInfo WHERE name="ww"
于 2013-05-04T14:36:25.807 回答