SQlite creating a database with a timestamp column and adding a value in

前端 未结 2 1753
悲&欢浪女
悲&欢浪女 2021-01-04 17:56

I am trying to create a database that called rawData. The db will hava a column for the id, a foreign user id (_id from another table), data and finally a timestamp.

相关标签:
2条回答
  • 2021-01-04 18:37

    This is a running example of DAO with SQlite and Date in Java:

    First create column MY_DATE TIMESTAMP in your TABLE and populate like this:

    PreparedStatement st = connection.prepareStatement("INSERT INTO ......");
    st.setDate(1, new java.sql.Date(lettura.getData().getTime()));
    

    And to retrieve data first i get date in String type:

    String dateStr = rs.getString(1);
    Date myDate = new java.util.Date(Long.parseLong(dateStr));
    
    0 讨论(0)
  • 2021-01-04 18:41

    The documentation says:

    SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

    • TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
    • REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
    • INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

    Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.

    If you need only seconds precision, use integers in Unix Time format. Otherwise, use floating-pointer numbers for fractional seconds.

    0 讨论(0)
提交回复
热议问题