我正在尝试获取今天日期的所有行,并返回我得到的行数。应用程序在加载时崩溃。
方法/SQLite 查询
public int getTodaysCount() {
SQLiteDatabase db = smokinDBOpenHelper.getWritableDatabase();
GregorianCalendar gc = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String today = sdf.format(gc.getTime());
Cursor cursor = db.rawQuery("SELECT * FROM " + smokinDBOpenHelper.INCIDENTS_TABLE
+ " WHERE " + KEY_DATE + " = DATETIME( ' " + today + " ' )", null);
if (cursor.getCount() == 0 || !cursor.moveToFirst()) {
throw new SQLException("No entries found");
}
return cursor.getCount();
}
来自日志的错误
03-20 12:26:25.913: E/AndroidRuntime(677): Caused by:
android.database.sqlite.SQLiteException: near "==": syntax error (code 1): , while
compiling: SELECT * FROM incidentsWHERE DATE_COLUMN == DATETIME( ' 2013-03-20 ' )
错误日志似乎告诉我我不允许使用 == 。那么,如果是这种情况,我该如何执行此操作?<=
和>=
?_ 还有一种方法可以像常规query
一样执行此查询rawQuery
吗?
例子:
db.query(SmokinDBOpenHelper.INCIDENTS_TABLE, new String[]
{KEY_ID, KEY_DATE}, KEY_DATE.equals(now) , null, null, null, null);
编辑
新日志错误消息:
03-20 12:56:22.103: E/AndroidRuntime(1153): Caused by:
android.database.sqlite.SQLiteException: no such column: DATE_COLUMN (code 1): , while
compiling: SELECT * FROM incidents WHERE DATE_COLUMN = DATETIME( ' 2013-03-20 ' )
事故表
public static final String KEY_ID = "_id";
public static final String KEY_LOCATION = "location";
public static final int LOCATION_COLUMN = 1;
public static final String KEY_DATE = "date";
public static final int DATE_COLUMN = 2;
private SmokinDBOpenHelper smokinDBOpenHelper;
public MySmokinDatabase (Context context) {
smokinDBOpenHelper = new SmokinDBOpenHelper(context, SmokinDBOpenHelper.DATABASE_NAME,
null, SmokinDBOpenHelper.DATABASE_VERSION);
}
private static class SmokinDBOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "smokin.db";
private static final String INCIDENTS_TABLE = "incidents";
private static final int DATABASE_VERSION = 1;
//SQL Statement to create a new database.
private static final String DATABASE_CREATE = "create table " +
INCIDENTS_TABLE + " (" +
KEY_ID + " integer primary key autoincrement, " +
KEY_LOCATION + " text not null, " +
KEY_DATE + " text not null);";
// Constructor
public SmokinDBOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
//Called when no database exists in disk and the helper class needs
//to create a new one.
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE);
}
//Called when there is a database version mismatch meaning that the version
//of the database on disk needs to be upgraded to the current version.
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
// Log the version upgrade.
Log.w("TaskDBAdapter", "Upgrading from version " +
_oldVersion + " to " +
_newVersion + ", which will destroy all old data");
// Upgrade the existing database to conform to the new version. Multiple
// previous versions can be handled by comparing _oldVersion and _newVersion
// values.
// The simplest case is to drop the old table and create a new one.
_db.execSQL("DROP TABLE IF EXISTS " + INCIDENTS_TABLE);
// Create a new one.
onCreate(_db);
}
}
最新的错误信息
03-20 13:33:03.483: E/AndroidRuntime(1342): Caused by: android.database.SQLException:
No entries found
插入方法
public void smokedHandler(View view) {
Spinner spinner = (Spinner) findViewById(R.id.location_spinner);
String s = spinner.getSelectedItem().toString();
String d = model.getDates();
mySmokinDatabase.insertSmokinValues(s, d);
refreshView();
}
public long insertSmokinValues(String s, String d) {
SQLiteDatabase db = smokinDBOpenHelper.getWritableDatabase();
ContentValues newSmokinValues = new ContentValues();
newSmokinValues.put(KEY_DATE, s);
newSmokinValues.put(KEY_LOCATION, d);
return db.insert(SmokinDBOpenHelper.INCIDENTS_TABLE, null, newSmokinValues);
}
public String getDates() {
GregorianCalendar gc = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
dates = sdf.format(gc.getTime());
return dates;
}
最后一点是,当我现在单击按钮时,存储在我的数据库中的值是s: Home
d: 2013-3-20 - 14:09
一如既往地感谢您的帮助!