2011-12-23 90 views
2

我是新来的数据库,所以只是忍耐与我。我试图让我的数据库设置为在创建条目时将其作为创建日期(例如,2011年12月23日)在ListView中显示,然后您可以单击该数据库并查看您拥有的所有内容在这一天创造了这是我到目前为止有:如何在特定日期查看所有条目,SQLite - Android

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) 

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

在此先感谢。

更新: 我删除了所有列“非空”,并改变了我的INSERT语句:

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) 

我在做什么错?

回答

2

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

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

要获得列表视图的日期,您需要执行某种SELECT查询,可能按日期字段进行分组,因此每个日期只能获得一行。像这样的东西应该工作:

SELECT * from table GROUP BY date 
+0

我试过取出独特的,但我仍然得到相同的错误。我只想列出listView中的日期一次。因此,例如,我不希望“2011年12月23日”显示在我为该日期创建的每个条目的listView中。相反,我想通过单击该日期来查看当天创建的所有条目。 – user990230 2011-12-23 15:03:49

+0

然后,您的插入查询仍然出现错误?可能是因为你没有指定你的字段的值 - 并且你创建了这个表,使得这些字段不能为空。我将编辑我的答案,以反映这... – 2011-12-23 15:48:06

+0

我刚刚更新了我的问题,感谢您的帮助。 – user990230 2011-12-23 16:45:00