获取时间和日期并将其插入数据库

时间:2016-04-08 11:13:50

标签: android database sqlite date time

我有这个数据库代码,我想在输入数据库时​​插入数据的日期和时间。当我点击一个按钮时显示它。 此代码不起作用,任何人都可以看看代码。

public class DataBase extends SQLiteOpenHelper {public static final String Database_Name = "WILLIAM.db";  
    public static final String Table_Name = "William_Table"; 
    public static final String Temp_col = "Temp";
    public static final String Pulse_col = "Pulse";  
    public static final String Motion_col = "Motion";
    public static final String Time_col= "Time ";
    public static final String Date_col= "Date ";
    public static final String ID_col = "ID";
 public static final String Create_Query = "CREATE TABLE " +
        Table_Name + " (" + Temp_col + " Text,"
        + Pulse_col + " Text," +
        Motion_col + " Text," +
        Time_col + " integer," +
        Date_col + " Text" + " )";

public DataBase(Context context) {super(context,Database_Name, null, 1);
   // SQLiteDatabase db=this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {db.execSQL(Create_Query);

    //SQLiteDatabase db =this.getWritableDatabase();
    ContentValues values=new ContentValues();
    values.put(Date_col, getDateTime());
    long id = db.insert(Table_Name, null, values);


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + Table_Name);
    onCreate(db);

}

public  boolean insertdata (String Temp,String Pulse ,String Motion  ){

 SQLiteDatabase db =this.getWritableDatabase();

    ContentValues contentValues=new ContentValues();
   // contentValues.put(ID_col,ID);
    contentValues.put(Temp_col,Temp);
    contentValues.put(Pulse_col,Pulse);
    contentValues.put(Motion_col, Motion);
    //contentValues.put("Date_col", dateFormat.format(date));

    long result= db.insert(Table_Name,null,contentValues);
    if (result==-1)
        return false;
    else
      return true;
}

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


public Cursor getAllData(){SQLiteDatabase db =this.getWritableDatabase();
    Cursor res =db.rawQuery("select * from "+ Table_Name,null );
    return res;
}
}

1 个答案:

答案 0 :(得分:0)

使用您的getDateTime()功能:

contentValues.put("Date_col", getDateTime());