dimanche 28 février 2016

How to add date in sqlite database android studio

I have to two date variables in my bean class

public class bean {

private Date dos;
private Date doe;

}

In my DAO class // columns in my sqlite database table

public class TaskDAO extends SQLiteOpenHelper {

public static final String  C_DOS = "d1";
public static final String  C_DOE = "d2";

@Override public void onCreate(SQLiteDatabase db) {

    StringBuilder sb = new StringBuilder();
    sb.append("CREATE TABLE "+TBNAME);
    sb.append(" ("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, ");
    sb.append(C_DOS+" INTEGER,");
    sb.append(C_DOE+" INTEGER)");

    String sql = sb.toString();
    db.execSQL(sql);
}

void addbean(bean b)

{
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(C_DOS,b.getdos);
    values.put(C_DOE,b.getdoe);

    database.insert(TBNAME,null,values);
}

Aucun commentaire:

Enregistrer un commentaire