获取今天日期的条目

时间:2013-03-20 16:35:15

标签: android sqlite

我正在尝试使用今天的日期获取所有行,并返回我获得的行数。应用程序在加载时崩溃。

方法/ 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

一如既往地感谢您的帮助!

3 个答案:

答案 0 :(得分:3)

  

android.database.sqlite.SQLiteException:near“==”:语法错误(代码   1)

你的logcat说明了一切。问题是 == 这是返回布尔值的运算符,需要关联。

String query = "SELECT * FROM " + smokinDBOpenHelper.INCIDENTS_TABLE
+ " WHERE DATE_COLUMN = DATETIME( ' " + today + " ' )", null);

注意:

我建议您使用占位符而不是硬编码方法。它更干净,更安全,你可以避免出现忘记单引号的问题。

Cursor c = db.rawQuery(query, new String[] {today});

更新

  

android.database.sqlite.SQLiteException:没有这样的列:DATE_COLUMN   (代码1):

这意味着您的DATE_COLUMN在您的数据库中不存在。您使用常量KEY_DATE定义它,因此您需要将查询更改为:

String query = "SELECT * FROM " + smokinDBOpenHelper.INCIDENTS_TABLE
    + " WHERE " + KEY_DATE + " = DATETIME( ' " + today + " ' )", null);

更新2:

我的建议是在没有where子句的情况下执行简单查询,您将看到db:

中是否有记录
String query = "SELECT * FROM " + smokinDBOpenHelper.INCIDENTS_TABLE", null);

答案 1 :(得分:3)

使用'='符号代替'=='符号,因为Sqlite不支持在查询语句中使用'=='符号。 e.g:

    Cursor cursor = db.rawQuery(
        "SELECT * FROM "
        + smokinDBOpenHelper.INCIDENTS_TABLE +
        " WHERE " + 
        "DATE_COLUMN = DATETIME( ' " + today + " ' )", null);

答案 2 :(得分:2)

incidentsWHERE之间缺少空格:

Cursor cursor = db.rawQuery(
            "SELECT * FROM "
            + smokinDBOpenHelper.INCIDENTS_TABLE +
            " WHERE " + 
            "DATE_COLUMN = DATETIME( ' " + today + " ' )", null);

请注意WHERE之前添加的空格。

==也应该是一个=

相关问题