我正在学习SQLite数据库,当我传递两个值Date
和Name
时,所有数据都从我的数据库中检索并显示在我的用户界面EditBox上。
当我在编辑框中添加Date
和Name
值并单击“显示”按钮时,模拟器很遗憾地停止并显示以下错误:
Logcat错误 - :08-03 23:08:06.730:E / AndroidRuntime(1099): android.database.sqlite.SQLiteException:没有这样的列:Xyz(代码1): ,同时编译:SELECT日期,名称,工资,津贴来自工资 名称= Xyz和日期= 03-08-2015
以下是代码:
// MainActivity.java
Display.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String SdateTime=SDate.getText().toString();
String SnameTime=SName.getText().toString();
//long l=Long.parseLong(n);
HotOrNot hon=new HotOrNot(MainActivity.this);
hon.open();
String returnName=hon.getName(SdateTime,SnameTime);
String returnDate=hon.getDate(SdateTime,SnameTime);
String returnSalary=hon.getSalary(SdateTime,SnameTime);
String returnAllowance=hon.getAllowance(SdateTime,SnameTime);
hon.close();
Date.setText(returnDate);
Name.setText(returnName);
Salary.setText(returnSalary);
Allowance.setText(returnAllowance);
}
});
// HotOrNot.java-->Database class
public class HotOrNot {
public static final String KEY_DATE = "date";
public static final String KEY_NAME = "name";
public static final String KEY_SALARY = "salary";
public static final String KEY_ALLOWANCE = "allowance";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "MegaLight";
private static final String TABLE_SALARY = "salary";
private static final int DATABASE_VERSION = 2;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
/*private static final String CREATE_TABLE_SALARY =
"create table salary (date integer not null, "
+ " name text not null, salary integer not null, allowance integer not null);";
*/
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context,DATABASE_NAME, null,DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_TABLE_SALARY = "CREATE TABLE " + TABLE_SALARY+ "("
+ KEY_DATE + " TEXT, "
+ KEY_NAME + " TEXT, "
+ KEY_SALARY + " INTEGER, "
+ KEY_ALLOWANCE + " INTEGER "
+ ");";
db.execSQL(CREATE_TABLE_SALARY);
Log.d(TAG,"salary Table Create Successfull");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS" + TABLE_SALARY);
onCreate(db);
}
}
public HotOrNot(Context c){
ourContext=c;
}
public HotOrNot open(){
ourHelper=new DbHelper(ourContext);
ourDatabase=ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String date, String name, String salary,
String allowance) {
// TODO Auto-generated method stub
ContentValues cv=new ContentValues();
cv.put(KEY_DATE, date);
cv.put(KEY_NAME, name);
cv.put(KEY_SALARY, salary);
cv.put(KEY_ALLOWANCE, allowance);
return ourDatabase.insert(TABLE_SALARY,null,cv);
}
public Cursor getAllContacts() {
/*String[] columns=new String[]{KEY_DATE, KEY_NAME,KEY_SALARY,KEY_ALLOWANCE};
Cursor c=ourDatabase.query(TABLE_SALARY, columns, null, null, null,null,null);
String result=" ";
int iDate=c.getColumnIndex(KEY_DATE);
int iName=c.getColumnIndex(KEY_NAME);
int iSalary=c.getColumnIndex(KEY_SALARY);
int iAllowance=c.getColumnIndex(KEY_ALLOWANCE);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
result=result+c.getString(iDate) + "" + c.getString(iName) + "" + c.getString(iSalary) + "" + c.getString(iAllowance) + "\n";
}
return result;
*/
// TODO Auto-generated method stub
return ourDatabase.query(TABLE_SALARY, new String[] {KEY_DATE, KEY_NAME,KEY_SALARY,KEY_ALLOWANCE}, null, null, null, null, null);
}
public String getName(String SdateTime,String SnameTime) {
// TODO Auto-generated method stub
String[] columns=new String[]{KEY_DATE,KEY_NAME,KEY_SALARY,KEY_ALLOWANCE};
Cursor c=ourDatabase.query(TABLE_SALARY,columns,KEY_NAME + "=" + SnameTime + " AND " + KEY_DATE + "=" + SdateTime,null,null,null,null);
// Cursor c=ourDatabase.rawQuery("select*from salary where name="+ SnameTime,new String() {"AND date="+SdateTime});
if(c!=null){
c.moveToFirst();
String name=c.getString(1);
return name;
}
return null;
}
public String getDate(String SdateTime,String SnameTime) {String[] columns=new String[]{KEY_DATE,KEY_NAME,KEY_SALARY,KEY_ALLOWANCE};
Cursor c=ourDatabase.query(TABLE_SALARY,columns,KEY_NAME + "=" + SnameTime + " and " + KEY_DATE + "=" + SdateTime,null,null,null,null);
if(c!=null){
c.moveToFirst();
String date=c.getString(0);
return date;
}
// TODO Auto-generated method stub
return null;
}
public String getSalary(String SdateTime,String SnameTime) {
String[] columns=new String[]{KEY_DATE,KEY_NAME,KEY_SALARY,KEY_ALLOWANCE};
Cursor c=ourDatabase.query(TABLE_SALARY,columns,KEY_NAME + "=" + SnameTime + " and " + KEY_DATE + "=" + SdateTime,null,null,null,null);
if(c!=null){
c.moveToFirst();
String salary=c.getString(2);
return salary;
}
// TODO Auto-generated method stub
return null;
}
public String getAllowance(String SdateTime,String SnameTime) {
String[] columns=new String[]{KEY_DATE,KEY_NAME,KEY_SALARY,KEY_ALLOWANCE};
Cursor c=ourDatabase.query(TABLE_SALARY,columns,KEY_NAME + "=" + SnameTime + " and " + KEY_DATE + "=" + SdateTime,null,null,null,null);
if(c!=null){
c.moveToFirst();
String allowance=c.getString(3);
return allowance;
}
// TODO Auto-generated method stub
return null;
}
}
答案 0 :(得分:1)
您需要围绕Xyz的单引号,否则sqlite会认为您指的是具有该名称的列。
String selection = KEY_NAME + "= '" + name + "'";
或者,您可以在查询字符串中使用?
并在selectionArgs
参数中传递Xyz。
String selection = KEY_NAME + " = ?";
String[] selectionArgs = new String[] {name};
在这种情况下,您不需要单引号,因为参数在语句准备好后绑定。