B4J Question SQLITE dates

powerino

Active Member
Licensed User
Hello everyone, I would like to store dates in a sqlite table (in integer format) and then read the date from DATABASE and display it in my form (possibly also make SQL searches with BETWEEN). I can not find examples in the forum, can anyone tell me how to do it? thank you
 

powerino

Active Member
Licensed User
Hi Erel, of course I know how to write the dates (in my case I set the field as an integer) and I do it as in the code below:

B4X:
        pers.setName(txtNome.Text)
        pers.setLastName(txtCognome.Text)
        pers.setAge(txtEta.Text)
        Dim s As String
            
        s="UPDATE persona SET Attivo='" & 0 & "', DataModifica = '" & DateTime.Now & "' WHERE ID =" & correnteRS & "
        'Log(s)
        Main.sql.ExecNonQuery(s)


Then if I look at the table in the database, I see that in the Modified Date field there is a number and not the date, so I would like to understand how this "conversion" works and how to recover the date from this number, ok?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
s="UPDATE persona SET Attivo='" & 0 & "', DataModifica = '" & DateTime.Now & "' WHERE ID =" & correnteRS & "
First, this query string is incorrect. You should not surround Long value with single quotes.
Second, it is recommend to use Prepared Statement.

I recommend you check Erel’s SQL Tutorial
 
Last edited:
Upvote 0

powerino

Active Member
Licensed User
I solved it using TEXT. I prefer to see the date formatted as I want in the table (rather than Ticks), and I have seen that I can write, read and search. Thanks for your help
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
I solved it using TEXT. I prefer to see the date formatted as I want in the table (rather than Ticks), and I have seen that I can write, read and search. Thanks for your help

I solved it using TEXT...

IMHO
, this is the wrong approach, but you are free to shoot yourself in the foot anytime you wish.

Erel and community members have gone to great lengths to ensure that date types (ticks as long) - works best under all situations - with minimal code (ie. DateUtils).

Eventually, as I did, you shall see....
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Another step... back and forth - forth and back....
Depending on stored format - this can be problematic - as I experienced... (ISO is best - yet you can do what you want - when you don't consider the consequences).

Ticks... or be damned... (yet this (ticks) can give Lyme disease)
 
Last edited:
Upvote 0

powerino

Active Member
Licensed User
I needed to solve the problem quickly. I read on the forum that it's not the ideal solution, and I believe it, but I didn't have time to understand how Ticks work. It may also be that I will not even use sqlite as a database, I will evaluate based on the project that I will have to implement.
 
Upvote 0

powerino

Active Member
Licensed User
I like to display the date in its correct format and not an "incomprehensible" number. Surely there will be a way to display it after transforming the Ticks (in the database table), but I still have to get there
 
Upvote 0

powerino

Active Member
Licensed User
I solved it using TEXT...

IMHO
, this is the wrong approach, but you are free to shoot yourself in the foot anytime you wish.

Erel and community members have gone to great lengths to ensure that date types (ticks as long) - works best under all situations - with minimal code (ie. DateUtils).

Eventually, as I did, you shall see....



hello, thanks for the answer, if you have an example to show, the study, thanks
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I would like to store dates in a sqlite table (in integer format) and then read the date from DATABASE and display it in my form (possibly also make SQL searches with BETWEEN)
tick = count of milliseconds (1/1000 s)
DateTime.Now = number of ticks since 1/1/1970 until current time

A good reason to use number to store date and time is we can compare number to another number. The question is how we compare the value of two strings?
Using DateTime.Now, we can compare this number with another value in database which also a number (without need to convert).
SQL:
strQuery = "SELECT id FROM persona WHERE ExpiredDate < " & DateTime.Now

Second is we eliminate the problem of inconsistent date format like dd/MM/yyyy, yyyy-MM-dd, MM/dd/yyyy etc.

So, it is a personal choice to use TEXT or INTEGER to store the date. As for me, I store both. :)
 
Upvote 0

powerino

Active Member
Licensed User
[QUOTE = "aeric, post: 714575, membro: 74499"]
tick = conteggio dei millisecondi (1/1000 s)
DateTime.Now = numero di tick dall'1 / 1/1970 fino all'ora corrente

Un buon motivo per utilizzare il numero per memorizzare la data e l'ora è che possiamo confrontare il numero con un altro numero. La domanda è: come confrontiamo il valore di due stringhe?
Usando DateTime.Now, possiamo confrontare questo numero con un altro valore nel database che anche un numero (senza la necessità di convertire).
[CODE = sql] strQuery = "SELEZIONA ID DA persona DOVE ExpiredDate <" & DateTime.Now [/ CODE]

In secondo luogo, eliminiamo il problema di un formato data incoerente come gg / MM / aaaa, aaaa-MM-gg, MM / gg / aaaa ecc.

Quindi, è una scelta personale utilizzare TEXT o INTEGER per memorizzare la data. Per quanto mi riguarda, conservo entrambi.:)
[/CITAZIONE]

I use this query to search between 2 date, and it works good
B4X:
s="Select * FROM Persona WHERE strftime('%Y-%m-%d', DataModifica) BETWEEN '" & data1 & "' AND '" & data2 & "';"
 
Upvote 0
Top