2017-04-08 54 views
0

你好,我傳遞一些數據到sqlLite數據庫,我想分別發送當前的日期和時間。我對編程相當陌生,所以任何幫助都很棒。我檢查了其他數據正確存儲。將當前日期和時間分別傳遞到sqlite

 public class DBHelper extends SQLiteOpenHelper { 

    public static final String DATABASE_NAME = "vas_podatki.db"; 
    public static final String TABLE_NAME = "podatki"; 
    public static final String COL_1 = "ID"; 
    public static final String COL_2 = "JAKOST"; 
    public static final String COL_3 = "DAN"; 
    public static final String COL_4 = "URA"; 


    public DBHelper(Context context) { 
     super(context, DATABASE_NAME, null, 1); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("create table "+ TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, JAKOST INTEGER, DAN CURRENT_DATE , URA CURRENT_TIME)"); 

    } 

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

    } 

    public boolean insertData(String jakost){ 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(COL_2,jakost); 
     long reuslt = db.insert(TABLE_NAME,null,contentValues); 
     if (reuslt == -1) 
      return false; 
     else 
      return true; 
    } 
} 
+0

是否有特定的原因兒子你需要分開存放他們嗎?如果您只存儲了UTC時間戳,那麼您可以根據需要在代碼中計算出日期和時間。 – Jahnold

回答

0
private void addVacantEntry(SQLiteDatabase db) { 
     db.execSQL("CREATE TABLE " + MyDatabaseColums.VacantEntry.TABLE_NAME + " (" 
       + MyDatabaseColums.VacantEntry.V_id + " INTEGER PRIMARY KEY, " 
       + MyDatabaseColums.VacantEntry.Date + " NUMERIC);"); 

,如果你想保存在SQLite的日期時間,屬性類型將NUMERIC

和存儲它獲取日期時間在

喜歡這個

private long date_in_long() { 

      Calendar c = Calendar.getInstance(); 
      c.set(Syear, Smonth, Sday, Shoure, Sminute); 
      long startDate = c.getTimeInMillis(); 

      return startDate; 
     } 
+0

查詢中的列拼寫不會包含'n',或者這是正確的? –

+0

更多解釋請在您的查詢中加入 – Elsunhoty

+0

添加方法 –