Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert time stamp into an SQLite database column? Using the function time('now')?

I am working on an android app and I am creating a database called HealthDev.db that has a table called rawData that has 4 columns: _id, foreignUserId, data, timeStamp

I have worked with the program sqlite3 in the bash shell and have figured out that I can have a time stamp column with the following column schema parameter: timeStamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP

so when I created the table I used: create table rawData(_id integer primary key autoincrement, foreignUserId integer, data real, timeStamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP);

This worked fine in the bash.

Then I practiced in the sqlite3 and know that when inserting into the timeStamp column and using the function time('now') as a value to store it actually stores a time stamp in the form HH:MM:SS in Universal Coordinated Time.

So now translating that into java for the android app, I used the following code below. This way the table automatically generates about 20 rows when the onCreate is called. This is just for testing if I am passing the time('now') correctly in java.

        // Below are variables to the database table name and the 
// database column names.
public static final String TABLE_RAW_DATA = "rawData";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_FOREIGN_USER_ID = "foreignUserId";
public static final String COLUMN_DATA = "data";
    public static final String COLUMN_TIME_STAMP = "timeStamp";

// Database creation sql statement.
private static final String DATABASE_CREATE = "create table "
    + TABLE_RAW_DATA 
    + "(" 
    + COLUMN_ID + " integer primary key autoincrement, " 
    + COLUMN_FOREIGN_USER_ID + " integer, " 
    + COLUMN_DATA + " real, " 
    + COLUMN_TIME_STAMP + " TIMESTAMP DEFAULT CURRENT_TIMESTAMP"
    + ");";

// initializes the columns of the database given by passing the DATABASE_CREATE
// sql statement to the incoming database.
public static void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
    // For testing

    ContentValues contentValues = new ContentValues();
    System.out.println("The database is open? " + database.isOpen());
    for (int i = 0; i < 20; i++)
    {
        contentValues.put( COLUMN_FOREIGN_USER_ID, 8976);
        contentValues.put( COLUMN_DATA, Math.random()*100 );
        contentValues.put( COLUMN_TIME_STAMP, " time('now') " );

        database.insert( TABLE_RAW_DATA, null, contentValues );

        //contentValues = new ContentValues();

    }

  }

After running this code in an eclipse emulator I then pulled the database file from the file explorer in DDMS view mode for eclipse android projects. Then I opened the database in a bash shell and then selected all the columns from the table rawData to show it on the shell. I noticed that the time('now') was treated as a string and not a function. To prove that the time('now') function worked I manually inserted a new row using time('now') for the timeStamp value. Then re selected all the columns to show them again. It successfully printed the time stampe as HH:MM:SS.

I am thinking there might be a difference in the enviroments? The bash shell recognizes the function time('now'), which was written in c right?, because I have the sqlite3 program in the bash? Yet in eclipse when I use a SQL database and use the insert it treats the time('now') as a string. Keep in mind I am working in a Windows 7 os. I am accessing the bash as a client (SSH Secure Shell) from my school which is the host.

My main question is it possible to code it so that way it recognizes the time('now') function?

like image 683
fgharo91 Avatar asked May 31 '13 19:05

fgharo91


People also ask

How do I store time stamps in SQLite?

Date and Time Datatype. 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.

How do I insert the current date and time in SQLite?

First, create a new table named datetime_real . Second, insert the “current” date and time value into the datetime_real table. We used the julianday() function to convert the current date and time to the Julian Day. Third, query data from the datetime_real table.

What is timestamp in SQLite?

The unixepoch() function returns a unix timestamp - the number of seconds since 1970-01-01 00:00:00 UTC. The unixepoch() always returns an integer, even if the input time-value has millisecond precision. The strftime() routine returns the date formatted according to the format string specified as the first argument.


1 Answers

Since the default for the column is CURRENT_TIMESTAMP, what if you leave out entirely this line:

contentValues.put( COLUMN_TIME_STAMP, " time('now') " );

Won't it now insert the current timestamp into that column by default?

like image 99
mharper Avatar answered Oct 17 '22 05:10

mharper