Closed hannesa2 closed 10 years ago
it looks to me like current_timestamp is actually an integer, if you use sqlite studio to:
select current_timestamp
The result is a timestamp like 2013-10-10 13:00:00
or something ilke that, so it looks like there is a cast happening that casts current_timestamp to an integer since your column type is an integer therefore truncating the timestamp.
From the sqlite documentation (http://www.sqlite.org/lang_createtable.html):
There is a load of date time functions in the sqlite docs, one of which might do what you want:
SELECT strftime('%s','now');
Check out the whole list here http://www.sqlite.org/lang_datefunc.html
Just to clarify try this:
create table tab1 (
_id integer primary key autoincrement,
created integer default strftime('%s','now') )
I get
no viable alternative at input 'strftime'
maybe 'strftime' should be added to grammar ?
Sorry please try this
create table tab1 (
_id integer primary key autoincrement,
created integer default (strftime('%s','now') )
);
Sorry
create table ftab1 (
_id integer primary key autoincrement,
created integer default (strftime('%s','now')),...
causes
android.database.sqlite.SQLiteException:
default value of column [created] is not constant (code 1):
during create table
ah wow thanks its possible sqlite does not support functions :(
I tested it with SQLiteStudio, there it works. I've no idea what's wrong in android
I tried this:
migration {
create table t1 (
id integer primary key,
time integer default (strftime('%s', 'now')),
txt text);
}
and then this:
T1.newBuilder().setTxt("a").insert();
T1.newBuilder().setTxt("b").insert();
T1.newBuilder().setTxt("c").insert();
T1.newBuilder().setTxt("d").insert();
I now see the data in the database after pulling it off the device, try that see if it works.
interesting how do you create your table. during runtime, I see this with your table
android.database.sqlite.SQLiteException: default value of column [time] is not constant (code 1): , while compiling: create table t1 ( id integer primary key, time integer default ( strftime ( "%s" , "now" ) ), txt text )
but I don't want to annoy you with this, I can handle it with manual set of values
Not sure what version of Android are you running this on?
Sent from Samsung Mobile
-------- Original message --------
From: hannesa2 notifications@github.com
Date:
To: robotoworks/mechanoid mechanoid@noreply.github.com
Cc: Ian Warwick fluxtah@hotmail.com
Subject: Re: [mechanoid] current_timestamp (#194)
interesting how do you create your table. during runtime, I see this with your table
android.database.sqlite.SQLiteException: default value of column [time] is not constant (code 1): , while compiling: create table t1 ( id integer primary key, time integer default ( strftime ( "%s" , "now" ) ), txt text ) — Reply to this email directly or view it on GitHub.
Android 4.1.2 do you need a testapp ?
Yes please I cannot replicate this, I was thinking it could be due to sqlite versions but I think this has always been supported.
here its is: https://github.com/hannesa2/mechanoid.issue.194
I found 2 issues
My testapp https://github.com/hannesa2/mechanoid.issue.194 with your recent version 0.2.2.201309200743 still gets this error
default value of column [time] is not constant
Unable to create application com.example.mech194.TestApp: android.database.sqlite.SQLiteException:
default value of column [time] is not constant (code 1): , while compiling: create table fp ( _id integer primary key autoincrement, time integer default ( strftime ( "%s" , "now" ) ), rId integer, s_id integer )
and T1Record is not generated
@hannesa2 can you try the following and paste me your mechdb source and generated code, I want to check to see if our code generates differently:
mechdb
migration {
create table qux (
time integer default ( strftime ( '%s' , 'now' ) )
);
}
generated
db.execSQL(
"create table qux ( " +
"time integer default ( strftime ( \"%s\" , \"now\" ) ) " +
") "
);
@hannesa2 its ok I found the issue now it seems the code generator generates incorrect quotes, it should preserve single quotes but it replaces them with double, but not all the time so its completely inconsistent, this is what it should generate, but it does not:
db.execSQL(
"create table t1 ( " +
"id integer primary key, " +
"time integer default ( strftime ( '%s' , 'now' ) ), " +
"txt text " +
") "
);
Instead it generates this which seems to fail with the sql error as you found:
db.execSQL(
"create table t1 ( " +
"id integer primary key, " +
"time integer default ( strftime ( \"%s\" , \"now\" ) ), " +
"txt text " +
") "
);
I will try look into this to see why its happening.
Should be fixed, please try latest version
Thank you very much !
'Timestamp' now working perfect.
Just for your information: The secondary issue "T1Record is not generated" is still alive I updated the testapp, but for me the primary issue about 'timestamp' is solved the other I just found by accident
ah cheers will look into that
Ok I see now why T1Record is not generated because the table definition does not have an _id :) just change the id to _id and it will work.
thank you for the hint !
I define my time fields like
but when I read the automatic generated "created" value, I get only '2013' instead of 1378239788
Any idea what's wrong here ?