2017-05-02 16 views
0

sqliteデータベースにdatetime値を挿入する際に問題が発生しました。日時の値をandroid sqliteに挿入

私は2つの日付ピッカーを持っていますが、私は日付を選ぶことができますが、その後、データベースに挿入すると、理由は分かりませんが、2つの日付の行に現在の日付があります。 datepickerで選択した日付を挿入しますか?

私のデータベースでは、これらの列をDATETIMEとして宣言しました。

public String getDate_debut() { 
    SimpleDateFormat dateFormat = new SimpleDateFormat(
      "dd-MM-yyyy HH:mm:ss"); 
    Date date = new Date(); 
    return dateFormat.format(date); 
} 

public void setDate_debut(String date_debut) { 
    this.date_debut = date_debut; 
} 

public String getDate_fin() { 
    SimpleDateFormat dateFormat = new SimpleDateFormat(
      "dd-MM-yyyy HH:mm:ss"); 
    Date date = new Date(); 
    return dateFormat.format(date); 
} 

public void setDate_fin(String date_fin) { 
    this.date_fin = date_fin; 
} 

は、ここで私は1つの日付ピッカーの日付を取得する方法は次のとおりです。

は、ここに日付のための私のget-セッタークラスです。私は文字列を書式設定する方法についてはわかりません。書式を設定する必要がある場合や、文字列として追加するだけでよいのかどうかはわかりません。

private DatePickerDialog.OnDateSetListener datepickerdernier 
     = new DatePickerDialog.OnDateSetListener() { 
    @Override 
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { 

     year_x2 = year; 

     //les DatePicker 
     month_x2 = month +1; 
     day_x2 = dayOfMonth; 
     datefin = (TextView)findViewById(R.id.TVDatePickerDernier); 
     datefin.setText(year_x2+"-"+month_x2+"-"+day_x2); 
    } 
}; 


      String date2 = datefin.getText().toString(); 
      //im not sure about the following lines 
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 

      Date date_dernier = dateFormat.parse(date2); 

そして最後に、私はそれを挿入します。

Cours c = new Cours(); 
         c.setBranche_cours(selectedBranche);    
         c.setDate_fin(date2); //should i set the string ?   
         dbhelper.Open(); 
         dbhelper.insertCours(c); 

がどのように私は私のデシベルに私は、現在の日付を選択していない日付を挿入することができますか?

@UPDATE - **挿入された日付を更新するにはどうすればよいですか?

ここに私のsqliteの方法だ..私は別のアクティビティを持っている、と私は私が前に選択した日付を変更したいが、私はできませんよ。

public void updateCours(Date olddatedebut, Date newdatedebut, Date olddatedernier, Date newdatedernier) 
{ 
Open(); 

db.execSQL("UPDATE "+TABLE_COURS+" set "+COLONNE_DATEPREMIER+"=date('"+newdatedebut+"') where "+COLONNE_DATEPREMIER+"=date('"+olddatedebut+"')"); 
db.execSQL("UPDATE "+TABLE_COURS+" set "+COLONNE_DATEDERNIER+"=date('"+newdatedernier+"') where "+COLONNE_DATEDERNIER+"=('"+olddatedernier+"')"); 
} 

そして、どのように私はにことを渡すが、私の活動の私の方法:

 //this is the new date of the 2nd datepicker 
         String datedernier = convertDateFormat(datenew2, "yyyy-MM-dd", "dd-MMM-yyyy"); 
         //this is the new date of the 1st datepicker 
         String datepremier = convertDateFormat(datenew1, "yyyy-MM-dd", "dd-MMM-yyyy"); 
         String date_debutold= intent.getExtras().getString("date_debut"); 
         String date_finold=intent.getExtras().getString("date_fin"); 
         //this is the current date recorded in my database from my datepicker 
         String date_debut1= convertDateFormat(date_debutold, "yyyy-MM-dd", "dd-MMM-yyyy"); 
         //this is the current date recorded in my darabase from my 1st datepicker 
         String date_fin1= convertDateFormat(date_finold, "yyyy-MM-dd", "dd-MMM-yyyy"); 

         //nouvelledatedebut 
         Date date_premier= new Date(datepremier); 
         Date date_dernier = new Date(datedernier); 
         Date date_premier2 = new Date(date_debut1); 
         Date date_fin2 = new Date(date_fin1); 
         dbhelper.Open(); 
         dbhelper.updateCours(selected_brancheold,selectedBranchenew,date_premier2,date_premier,date_fin2,date_dernier, 

答えて

0

この

public static String convertDateFormat(String date, String curFormat, String desFormat){ 
    String newFormat = null; 
    Date frmtDate = null; 
    try { 
     SimpleDateFormat sdf = new SimpleDateFormat(curFormat); 

     frmtDate = sdf.parse(date); 
     SimpleDateFormat formatter = new SimpleDateFormat(desFormat); 
     newFormat = formatter.format(frmtDate); 

    } catch (Exception e) { 
     newFormat = date; 
    } 
    return newFormat; 
} 
を試してみてくださいあなたはgetterメソッドにいくつかのロジックを作り、 date_fin属性の日付を上書きします new Date()を、設定されているので、

サンプル

String result = convertDateFormat(date2, "yyyy-MM-dd", "dd-MMM-yyyy"); 
c.setDate_fin(result); 
+0

申し訳ありませんが、私の活動でそのメソッドを作成しますか? – David

+0

あなたのアクティビティが静的である必要がない場合 'private String convertDateFormat' – sajan

+0

私はあなたが私に与えたものを試しましたが、それでも現在の日付を挿入します.. – David

2

厥。 insertCoursを作成すると、このメソッドは挿入しようとしているオブジェクトのgetメソッドをすべて見つけようとします。この変更してみてください。この

public String getDate_fin() { 
    return this.date_fin; 
} 

public String getDate_fin() { 
    SimpleDateFormat dateFormat = new SimpleDateFormat(
      "dd-MM-yyyy HH:mm:ss"); 
    Date date = new Date(); 
    return dateFormat.format(this.date_fin); 
} 

をあなたはまだ日(文字列)にフォーマットを追加したい場合は、あなたはまだgetterメソッドにそれを作ることができますが、私はしないでくださいそれをお勧めします。

NULL:

+0

私はdatetime値の代わりにStringを使うと思ったが、後で2つの日付を比較する必要があります。そして3つのリストビューがあります。それを文字列として挿入しても可能ですか? – David

+0

また、右の列のタイプを変更する必要があることを意味しますか?日時の代わりに文字列? – David

+0

@David、StringまたはDate/Datetime/Calendarとして属性を使用します。 Tostodoraが述べたように、SQLiteにはdatetime型がありません。したがって、DBに挿入するときには、日付をString(あなたのやり方)や整数/数値として使うことができます。 ストレートフォワードのオプションは、属性の日付がストリング型のもので、他のものと日付を比較する2つの必要がある場合は、Dateオブジェクトに変換します。 – niarb

0

( データベースエンジンによってまたは操作)のSQLiteデータベースに格納された各値は、次のストレージ・クラスの1つを有します。値はNULL値です。

INTEGER。値は符号付き整数で、値の大きさに応じて1,2,3,4,6、または8 バイトに格納されます。

REAL。値は8バイトのIEEE 浮動小数点数として格納された浮動小数点値です。

TEXT。値は、データベース文字コード (UTF-8、UTF-16BEまたはUTF-16LE)を使用して格納されたテキスト文字列です。

BLOB。値はデータの塊であり、入力されたとおりに保存されます。

DATETIMEはありません。 SQliteをテキストとして保存します。 1日を追加することはできません。それを読んで解析する必要があります。あなたがそれを保管するときも同じことが起こります。あなたはそれを解析する必要があります。

希望しました。

+0

だから私は文字列値として格納する必要がありますか? – David

+0

私はいつもTEXTとして保存します。これをdatetimeとしてcreate tableに宣言すると、このhttps://sqlite.org/datatype3.htmlに基づいて数値として格納されます。 編集:TEXTとして保存しますが、それを読んだらDATEに解析してください。あなたのJavaコードでDATEとして扱われるべきです。 – Tostadora

+0

ありがとう、私はそれを変更します! – David

関連する問題