android - 如何查看特定日期的所有条目,SQLite - Android

标签 android sqlite listview date insert

我是数据库新手,请多多包涵。我试图将我的数据库设置为创建条目时,它在 ListView 中显示为创建日期(例如,“2011 年 12 月 23 日”),然后您可以单击它并查看您拥有的所有内容那天创建的。这是我目前所拥有的:

public class DbAdapter
{
    private static final String TAG = "DbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private static final String DATABASE_CREATE =
        "create table entry (_id integer primary key autoincrement, "
        + "date text UNIQUE, " +
        "velocity integer not null, " +
        "type text not null, " +
        "xCoord real not null, " +
        "yCoord real not null, " +
        "color integer not null);";

    private static final String DATABASE_NAME = "data";
    private static final String DATABASE_TABLE = "entry";
    private static final int DATABASE_VERSION = 2;

    private final Context mCtx;

    private SQLiteStatement insertStmt;
    private static final String INSERT = "insert into " + DATABASE_TABLE + "(date) values (?)";

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS entry");
            onCreate(db);
        }
    }

    public DbAdapter(Context ctx)
    {
        this.mCtx = ctx;
    }

    public DbAdapter open() throws SQLException
    {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        this.insertStmt = this.mDb.compileStatement(INSERT);
        return this;
    }

    public void close()
    {
        mDbHelper.close();
    }

    public long addEntry(String date, int velocity, String type, float xCoord, float yCoord, int color)
    {       
        ContentValues initialValues = new ContentValues();
        initialValues.put("date", date);
        initialValues.put("velocity", velocity);
        initialValues.put("type", type);
        initialValues.put("xCoord", xCoord);
        initialValues.put("yCoord", yCoord);
        initialValues.put("color", color);

        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

    public boolean deleteEntry(long rowId)
    {
        return mDb.delete(DATABASE_TABLE, "_id" + "=" + rowId, null) > 0;
    }

    public Cursor fetchAllEntries()
    {
        return mDb.query(DATABASE_TABLE, new String[] {"_id", "date", "velocity",
                "type", "xCoord", "yCoord", "color"}, null, null, null, null, null);
    }

    public Cursor fetchEntry(long rowId) throws SQLException
    {
        Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {"_id", "date",
                    "velocity", "type"}, "_id" + "=" + rowId, null,
                    null, null, null, null);
        if (mCursor != null)
        {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    public Cursor fetchInfo(long rowId) throws SQLException
    {
        Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {"_id", "velocity", "xCoord", "yCoord", "color"}, "_id" + "=" + rowId, null, null, null, null, null);

        if(mCursor != null)
        {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
}

这对于我创建的第一个条目来说工作正常,但我无法为那一天创建更多条目,它给我一个错误提示:

12-23 09:22:16.460: E/Database(272): Error inserting velocity=17 yCoord=121.0 xCoord=344.0 color=3 type=UNFINISHED date=December 23, 2011
12-23 09:22:16.460: E/Database(272): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
12-23 09:22:16.460: E/Database(272):    at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
12-23 09:22:16.460: E/Database(272):    at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
12-23 09:22:16.460: E/Database(272):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549)
12-23 09:22:16.460: E/Database(272):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
12-23 09:22:16.460: E/Database(272):    at com.x17.projects.strikezone.DbAdapter.addEntry(DbAdapter.java:96)
12-23 09:22:16.460: E/Database(272):    at com.x17.projects.strikezone.Tab1$2.onClick(Tab1.java:121)
12-23 09:22:16.460: E/Database(272):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
12-23 09:22:16.460: E/Database(272):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 09:22:16.460: E/Database(272):    at android.os.Looper.loop(Looper.java:123)
12-23 09:22:16.460: E/Database(272):    at android.app.ActivityThread.main(ActivityThread.java:4627)
12-23 09:22:16.460: E/Database(272):    at java.lang.reflect.Method.invokeNative(Native Method)
12-23 09:22:16.460: E/Database(272):    at java.lang.reflect.Method.invoke(Method.java:521)
12-23 09:22:16.460: E/Database(272):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-23 09:22:16.460: E/Database(272):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-23 09:22:16.460: E/Database(272):    at dalvik.system.NativeStart.main(Native Method)

我知道发生此错误是因为我试图将一个值插入到已有值的行中。所以我的问题是,如何在创建日期内插入多个值。我有一个没有被使用的插入语句,因为我不知道在哪里以及如何使用它,我不确定它是否正确。由于我是新手,所以只要指出正确的方向就会有所帮助。

提前致谢。

更新: 我从所有列中删除了“not null”并将我的插入语句更改为:

private static final String INSERT = "INSERT INTO " + DATABASE_TABLE + "(date, velocity, type, xCoord, yCoord, color) VALUES (?,?,?,?,?,?)";

我正在插入这样的值:

public long addEntry(String date, int velocity, String type, float xCoord, float yCoord, int color)
    {
        this.insertStmt.bindString(1, date);
        this.insertStmt.bindLong(2, velocity);
        this.insertStmt.bindString(3, type);
        this.insertStmt.bindDouble(4, xCoord);
        this.insertStmt.bindDouble(5, yCoord);
        this.insertStmt.bindLong(6, color);

        return this.insertStmt.executeInsert();
    }

我仍然收到相同的“错误代码 19:约束失败”错误。

12-23 11:36:03.343: E/AndroidRuntime(418): FATAL EXCEPTION: main
12-23 11:36:03.343: E/AndroidRuntime(418): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
12-23 11:36:03.343: E/AndroidRuntime(418):  at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
12-23 11:36:03.343: E/AndroidRuntime(418):  at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:81)
12-23 11:36:03.343: E/AndroidRuntime(418):  at com.x17.projects.strikezone.DbAdapter.addEntry(DbAdapter.java:86)
12-23 11:36:03.343: E/AndroidRuntime(418):  at com.x17.projects.strikezone.Tab1$2.onClick(Tab1.java:120)
12-23 11:36:03.343: E/AndroidRuntime(418):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
12-23 11:36:03.343: E/AndroidRuntime(418):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 11:36:03.343: E/AndroidRuntime(418):  at android.os.Looper.loop(Looper.java:123)
12-23 11:36:03.343: E/AndroidRuntime(418):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-23 11:36:03.343: E/AndroidRuntime(418):  at java.lang.reflect.Method.invokeNative(Native Method)
12-23 11:36:03.343: E/AndroidRuntime(418):  at java.lang.reflect.Method.invoke(Method.java:521)
12-23 11:36:03.343: E/AndroidRuntime(418):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-23 11:36:03.343: E/AndroidRuntime(418):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-23 11:36:03.343: E/AndroidRuntime(418):  at dalvik.system.NativeStart.main(Native Method)

我做错了什么?

最佳答案

我怀疑“日期文本 UNIQUE”对您来说是个问题,如果您删除 UNIQUE,那么您应该能够插入具有相同日期的多个条目——如果我对您的理解正确的话。

问题 2:如果您将数据库中的字段设置为“not null”,那么您必须在“insert”语句中为这些字段指定值。目前,您仅在 insert 语句中设置日期字段。因此,您要么需要在“insert”语句中为所有字段指定值,要么需要删除表创建中的“not null”规范。

要获取 ListView 的日期,您需要执行某种SELECT 查询,可能按日期字段分组,因此每个日期只能获取一行。这样的事情应该有效:

SELECT * from table GROUP BY date

关于android - 如何查看特定日期的所有条目,SQLite - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8617342/

相关文章:

android - 应用程序更新后重复的 Intent 。警报是否存在?

java - 主 Activity 回收器 View 上未设置行布局

android - Play 商店有新版本的应用程序,但没有显示更新

android - sqlite数据库更新

Python-Flask - 如何在不指定目录的情况下访问 pythonanywhere.com 上的 sqlite database.db?

ios - 在 NSFetchRequestController 中使用带有复杂 NSPredicate 的 NSFetchRequest

Android:ListView 推出它下面的所有其他内容

android - 从源代码为 android 构建 libxml2 作为静态库

java - 如何根据设备有效地更改布局

android - 从数组中获取字符串