1

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

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

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

4

3 回答 3

4

android.database.sqlite.SQLiteException:靠近“==”:语法错误(代码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 子句的简单查询,您将查看数据库中是否有记录:

String query = "SELECT * FROM " + smokinDBOpenHelper.INCIDENTS_TABLE", null);
于 2013-03-20T16:41:18.720 回答
3

使用 '=' 符号代替 '==' 符号,因为 Sqlite 在 Query 语句中不支持 '==' 符号。例如:

    Cursor cursor = db.rawQuery(
        "SELECT * FROM "
        + smokinDBOpenHelper.INCIDENTS_TABLE +
        " WHERE " + 
        "DATE_COLUMN = DATETIME( ' " + today + " ' )", null);
于 2013-03-20T16:44:43.440 回答
2

incidents您在and之间缺少空格WHERE

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

注意之前添加的空格WHERE

==应该是单身=

于 2013-03-20T16:37:49.913 回答