2011-12-23 53 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

相關問題