当我执行搜索方法时,它给出了我运行时错误
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();
}
}
答案 0 :(得分:2)
Cursor mCursor =db.query(true,tableName , new String[] {facultyc,
deparmentc, namec, officeNumberc,Phonec ,emailAddressc,officeHourc},namec + "= ?", new String[] { n1 }, null, null, null, null);
字符串必须在引号中。
答案 1 :(得分:2)
问题是你错过了字符串的引号!
最好像这样使用String参数:
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;
}
通过这种方式,您可以避免任何错误,并且字符串特殊字符也会自动转义!
答案 2 :(得分:0)
您的查询未经过清理。它应该是:
SELECT DISTINCT faculty, deparment, name, officeNumber,
phoneNumber, emailAddress, officeHour FROM teacherInfo WHERE name="ww"
答案 3 :(得分:0)
您输入的查询错误。将其更改为 - >
Cursor mCursor =db.query(true,tableName , new String[] {facultyc,
deparmentc, namec, officeNumberc,Phonec ,emailAddressc,officeHourc},"name = " + n1, null, null, null, null, null);