所以我使用SQLite为我的Android应用程序创建数据库,出于某种原因,它认为我的专栏" START_DATE_COLUMN"不存在(我的日志语句为列索引返回-1)。这很奇怪,因为当我在SQLite浏览器中查看.db文件时,列中包含我添加的所有条目。
这是我的DatabaseHelper类的代码:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper implements BaseColumns {
public static final int DATABASE_VERSION = 3;
public static final String DATABASE_NAME = "JobEntries.db";
public static final String TABLE_NAME = "jobs";
public static final String NAME_COLUMN = "name";
public static final String START_DATE_COLUMN = "startDate";
private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " + TABLE_NAME + " " +
"(" + _ID + " INTEGER PRIMARY KEY, "
+ NAME_COLUMN
+ " TEXT, "
+ START_DATE_COLUMN
+ " TEXT)";
private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + TABLE_NAME;
public DatabaseHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public boolean insertJob(JobEntry job){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(this.NAME_COLUMN, job.getClientName());
contentValues.put(this.START_DATE_COLUMN, job.getStartDate());
db.insert(this.TABLE_NAME,null,contentValues);
return true;
}
public Cursor getDataByDate(String startDate){
SQLiteDatabase db = this.getReadableDatabase();
Cursor result = db.rawQuery("SELECT " + NAME_COLUMN + " FROM " +TABLE_NAME+" WHERE " +START_DATE_COLUMN+ " =" +startDate+"",null);
result.moveToFirst();
Log.d("Column Index ", Integer.toString(result.getColumnIndex(START_DATE_COLUMN)));
return result;
}
}
答案 0 :(得分:0)
startDate
是一个文本
尝试在您的查询whereArgs中添加单引号' ,并将START_DATE_COLUMN
添加到您选择的列中,如:
Cursor result = db.rawQuery("SELECT " + NAME_COLUMN + ", " + START_DATE_COLUMN + " FROM " +TABLE_NAME+" WHERE " +START_DATE_COLUMN+ " = '" +startDate+ "' ",null);
或
Cursor result = db.query(TABLE_NAME, new String[] { NAME_COLUMN, START_DATE_COLUMN }, START_DATE_COLUMN + " = ? ", new String[] { startDate }, null, null, null );
答案 1 :(得分:-1)
这是您引用的日志语句吗?
Log.d("Column Index ", Integer.toString(result.getColumnIndex(START_DATE_COLUMN)));
如果是这样,它返回-1,因为您没有在先前在方法中构建的SELECT语句中选择START_DATE_COLUMN。这将是修改SELECT语句以包含该列的一种方法:
Cursor result = db.rawQuery("SELECT " + NAME_COLUMN + ", " + START_DATE_COLUMN + " FROM " +TABLE_NAME+" WHERE " +START_DATE_COLUMN+ " =" +startDate+"",null);
答案 2 :(得分:-1)
这个db.rawQuery("SELECT " + NAME_COLUMN + " FROM " +TABLE_NAME+" WHERE " +START_DATE_COLUMN+ " =" +startDate+"",null);
会产生这样的结果。你得到-1,因为你只查询来自NAME_COLUMN的数据,
+-------------------+
| name |
+-------------------+
| Android Developer |
| Python Developer |
| Django Developer |
+-------------------+
如果您还想包含START_DATE_COLUMN,那么我们可以将查询更改为"SELECT * FROM " +TABLE_NAME+" WHERE " + START_DATE_COLUMN + " =" +startDate+""
或"SELECT " + NAME_COLUMN + ", " + START_DATE_COLUMN + " FROM " +TABLE_NAME+" WHERE " +START_DATE_COLUMN+ " =" +startDate+""
,这将产生:
+-------------------+--------------+
| Name | startDate |
+-------------------+--------------+
| Android Developer | October 16th |
| Python Developer | October 17th |
| Django Developer | October 18th |
+-------------------+--------------+
现在,Integer.toString(result.getColumnIndex(START_DATE_COLUMN))
不会返回-1。