2013-07-30 24 views
5

にsqliteのデータベースからint型私は、このコードGET文字列、日付、時間とAndroid

void addRemAction(RemActions remaction) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(KEY_ACTOINS_TITLE, remaction.getTitle()); // Action title 
    values.put(KEY_ACTIONS_MSG, remaction.getMsg()); // action msg 



    values.put(KEY_ACTIONS_DATE, dateFormat.format(new Date())); // action date 
    values.put(KEY_ACTIONS_TIME, remaction.getTime()); // action time 
    values.put(KEY_ACTIONS_PLACE_LONG, remaction.getPlong()); // action place long 
    values.put(KEY_ACTIONS_PLACE_LAT, remaction.getPlat()); // action place lat 

    // Inserting Row 
    db.insert(TABLE_ACTIONS, null, values); 
    db.close(); // Closing database connection 

を使用してsqliteのデータベースに、文字列、日付、時刻、およびint型値を挿入し、これはそれ

を取得するために私のコードです
RemActions getRemAction(int id) { 
    SQLiteDatabase db = this.getReadableDatabase(); 

    Cursor cursor = db.query(TABLE_ACTIONS, new String[] {KEY_ACTOINS_TITLE, KEY_ACTIONS_MSG, KEY_PLACES_NAME, KEY_ACTIONS_DATE, KEY_ACTIONS_TIME}, KEY_ACTIONS_ID + "=?", 
      new String[] { String.valueOf(id) }, null, null, null, null); 
    if (cursor != null) 
     cursor.moveToFirst(); 

    RemActions remActions = new RemActions(cursor.getString(0), 
      cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(4)); 
    // return remActions 
    return remActions; 

RemActionクラスのコンストラクタの4番目の引数がjava.util.dateクラスのDate型であるため、my ideがエラーを表示しています。

私の質問は、「どうやってカーソルを日付形式で取得するのですか?」です。要求

+1

SO.close dbへの歓迎とgetRemAction()メソッドのカーソルは、u例外を与えます。 – TheFlash

+0

また、 'cursor.getDate(1)'を使って日付を取得することもできます。しばらく使用していないが、このようなものでなければならない。http://www.coderanch.com/t/445539/JDBC/databases/retrieve-Date-JDBC-sql –

+0

@TheDarkKnight JDBCを使用せず、SQLiteにgetDateはありません。 – m0skit0

答えて

11

SQLiteで日付/時刻の値を保存することはできませんので、最初にミリ秒に変換して保存する必要があります。

Date c = new Date(System.currentTimeMillis()); 
long milliseconds = c.getTime(); 
//To get current time 

あなたが戻って日付形式にミリ秒を変更する必要がある場合:

Date c = new Date(cursor.getLong(4)); 

を次にあなたがSimpleDateFormatを使用して日付をフォーマットすることができます。

+0

あなたの日付はjava.util.Dateですか? –

+1

'変換できない型。 Longをjava.util.Dateにキャストできません。 – Zenadix

+0

@Zenadix私は彼女が 'new Date(cursor.getLong(4))'を意味すると思います。 – TWiStErRob

1

に追加のコードを提供するために喜んB病気

あなたはDate#getTime()として、この値を格納しているので、あなたはnew Date(cursor.getLong(4))でそれを回復することができます。

さらに、実行していないCursorを終了する必要があります。

関連する問題