[sqlite] it seems sqlite3_prepare_v2 disappeared for me

2007-10-23 Thread Rafi Cohen
Hi, I've written an application in C that integrates sqlite-3.3.12. Up
to now for months everything was running fast and smooth.
Now, I installed sqlite3 and compiled my application on a different
computer.
The installations were ok, but upon activating my application, it quits
with the error:
...symbol lookup error.undefined symbol: sqlite3_prepare_v2. What
can be the reason for that? I never had that error on my other computer,
both running linux susse 9.3, by the way.
It's the first time I see this one. I need help to resolve this problem.
Thanks, Rafi.


[sqlite] what do I compile with -dthreadsafe=1

2007-04-17 Thread Rafi Cohen
Hi, when I read the article about sqlite and multithread in cvstrac, I
understood that I need to compile my source files using sqlite functions
with -DTHREADSAFE=1.
Now, due to a pproblem I had in my project I re-readad this article and
began to think I should compile sqlite source files using
-DTHREADSAFE=1, which I did not do yet.
So I'm confused. Where do i use this? compile sqlite sources? my
sources? both?
In case of sqlite sources, where do I put this -DTHREADSAFE=1? in a
specific makefile? in all makefiles? Please help.
I have linux and using sqlite 3.3.12.
Thanks, Rafi.


[sqlite] handling empty table in a callback function to sqlite3_exec

2007-04-15 Thread Rafi Cohen
Hi, I'm querying a table by writing the query in sqlite3_exec with a
callback function that does some operation on the resulting row.
Everything is fine when the table has even one row, but when it is
empty, my program halts with "segmentation fault". I can add a dummy row
to the table to prevent this, but is there a nicer solution to this
issue?
Thanks, Rafi.


[sqlite] FW: invoking sqlite3 command as a superuser

2007-03-22 Thread Rafi Cohen
Hi, 2 days passed since I've sent this message to the list, but I
received no replies so far.
As I thought that may be not everybody received it, I resend it one more
time.
I also feel that my message might be unclear, so I also juggest to try
it and give "steps to reproduce" below.
Before this, I would like to emphasize that I'm using linux susse 9.3
here and I did not try this on any other system, but I hope some of you
can shed some light on this problem and -- even better -- offer some
posible solution.
Steps to reproduce:
1. login as a regular user with your own username.
2. create a small file (let's call it import.txt) with some rows similar
to the construct of a table you'll create in the next step.
3. type: sqlite3 test (test being the database).
4. in sqlite3, enter: create table tbl(with appropriate column list);
5. Then enter: .import import.txt tbl in order to insert the rows into
tbl that you wrote in step 2.
6. Enter: select * from tbl; you'll probably see the rows you just
inserted through the file, as expected.
7. enter: delete from tbl; in order to reinsert those lines again next
time.
8. exit sqlite3 (ctrl-d).
9. Now, at the shell command line enter the command to be a super user:
su, ENTER, password (if any, probably there is. (I have this on this
system, since I'm working remotely on a company's computer from home and
it is, of course, multiuser system).
10. Now, invoke again sqlite3 test.
11. enter: .import import.txt tbl
12. Then enter: select * from tbl; this time, at least in my case, I get
nothing, as like the table is empty, although .import did not output any
error.
This is, from my point of view, a very strange and anoying behavior.
Now, if you read on, you'll understand why I need this way of usage.
Yes, I may solve this avoiding use of .import, but this way seems neat
and quicker.
I hope somebody will lead me towards a solution, or at least explain
this strange behavior.
Sorry for the lengthy message, thanks, Rafi.
-Original Message-
From: Rafi Cohen [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, March 20, 2007 8:06 PM
To: 'sqlite-users@sqlite.org'
Subject: inv ok i ng sqlite3 command as a superuser


Hi, for various reasons, I preffer to invoke my application as the
superuser.
This, in turn, invokes sqlite3 command to apply a .import command from
file to table.
Although .import does not return any error, the table remains empty.
So, I manually entered to sqlite3 and applied the .import command. Most
surprisingly, as a superuser it does just nothing, as ignored.
But when I do just the same as a regular user, it works with no problem.
So, first, can anybody confirm this or reffer me to other reasons for
this? And second, does anybody have any solution for this? As I said, I
have to run my application as a superuser as it has to open and
communicate with serial ports.
Thanks, Rafi.


[sqlite] involing sqlite3 command as a superuser

2007-03-20 Thread Rafi Cohen
Hi, for various reasons, I preffer to invoke my application as the
superuser.
This, in turn, invokes sqlite3 command to apply a .import command from
file to table.
Although .import does not return any error, the table remains empty.
So, I manually entered to sqlite3 and applied the .import command. Most
surprisingly, as a superuser it does just nothing, as ignored.
But when I do just the same as a regular user, it works with no problem.
So, first, can anybody confirm this or reffer me to other reasons for
this? And second, does anybody have any solution for this? As I said, I
have to run my application as a superuser as it has to open and
communicate with serial ports.
Thanks, Rafi.


[sqlite] multithread problem

2007-03-20 Thread Rafi Cohen
Hi, I probably asked about this before, but as I'm having some problems
with my implementation, I would like to ask more clear questions.
The main part of the application does not modify the database, but
quries it occasionally (select).
The other thread is responsible for the changes (import from a file to a
table, insert, update).
1. Should I open the database explicitly in the amin part and also in
the thread?
2. should I create the tables in the thread or can I create them in the
main thread and modify them in the other?
My current situation is that I open the database and create the tables
in the main thread.
When the other thread attempts to import data from a file to a table, no
error is reported, but still the table is empty after the import.
Thanks, Rafi.


[sqlite] a problem trying to invoke sqlite3 commands from a C application

2007-03-19 Thread Rafi Cohen
Hi, I'm calling system from a C application in order to invoke sqlite3
database, then continue with commands: .separator "," and .import file
table, all in a single string as argument to system.
sqlite3 is indeed invoked with the correct database, but the problem is
that the .import command is reported as part of .separator argument.
There is just a blank between the 2 commands. Should there be a
different separation betrween the 2 commands? May they be executed one
after the other in a single call to system or should I call system twice
for each command?
Thanks, Rafi.


[sqlite] 2 questions concerning select statement

2007-03-19 Thread Rafi Cohen
Hi, 1. When I prepare a select statement for later execution, may I use
a question mark instead of a table and later "bind" different table
names with the same cxonstruct according rto some condition?
for example, sqlite3_prepare_v2(db, "select * from ? where.",...);
2. In "order by" clause may I use a column which is part of the table
but nor part of the result?
select com1, col2 from tbl order by col1, col3;
col3 is part of the table but not of the result.
Thanks, Rafi.


RE: [sqlite] Format of csv file RE: [sqlite] date/time implementation question

2007-03-15 Thread Rafi Cohen
Dennis, without knowing that we are talking here about non-standard
format of the csv file, my client, for whom I'm preparing this project
decided to change the file extension to .txt in order not to confuse
with the standard format of .csv.
Anyway, the time for this project is beginning to be tight so I'll still
try today to implement the sql approach, but if I fail, at least for
this version, I'll proceed with the first approach.
Now, I understand that .import accepts any kind of file, just under
condition that each line represents a valid row of the table and the
separator is either the default one or the one given explicitly by the
.separator command.
What is the default separator? I could not discover this by looking into
the code.
My idea is to read the file sent by the client within the C application,
check what needs to be checked for validation and store the relevant
parts of it formatted appropriately in another file and then import from
this file into a table and proceed as you suggested yesterday.
Do you see any problem with this approach or have anhy other suggestion?
Concerning separator, I can add the .separator "," command into the
script before .import, but I may also use the default, if I know what it
is.
I tried this manually in sqlite3 and I know that coma is not the default
separator as .import failed.
But after .separator "," .import succeeded and I could indeed select the
rows from the table.
I hope this signs a success for me to implement this approach in my C
code as you suggested.
Thanks, Rafi.

-Original Message-
From: Dennis Cote [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, March 14, 2007 11:11 PM
To: sqlite-users@sqlite.org
Subject: Re: [sqlite] Format lf csv file RE: [sqlite] date/time
implementation question


Rafi Cohen wrote:
> Hi Dennis, the first approach is clear now and I may proceed with it.
>   
Good to hear.
> The second approach is interesting and chalenging, but leaves some 
> issues to clarify and in case I find solutions to those issues I well 
> may adopt it. 1. format of csv file: I have no idea how this csv file 
> is created and which database engine is used. I do know that I receive

> such a file once or twice a day.
> While reading the file onto the list of structures, I also validate
the
> consistency of the data in the file.
> Before the data lines (starting with "d,") there is a header line
> "hdr,". This line contains the sequential number of the file, number
of
> records in the file and a checksum on a specific field of the data
(say
> coumn 2).
> As I knew nothing up to now about .import, I wonder if there is a way
to
> include those checings in the second approach?
>   
Firstly, this does not sound like a standard CSV format file (see 
http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm ).

Secondly, your validation checks can not be added to the normal csv 
.import command using the sqlite shell, but they could be added to a 
customized copy of the import routine that you add to your own code. It 
might also be possible to convert some of the validation tests to SQL 
check constraints on the table columns as well.
> 2. The deletion of the future dates is incorrect. On the contrary, in 
> the first approach, I re-examine the remaining structures each half a 
> minute until any of them becomes past date, then I process it just 
> like any other past date structures and then free it. In case a new 
> .csv file arrives, I add the new list of structures to the remaining 
> ones and continue to examine them every half a minute. I could do the 
> same with the sql3_exec statement in the second approach, but I need 
> another approach for the case of the future records. I hope you have 
> satisfying answers for those 2 issues and then I'll be glad to proceed

> with the second approach.
>   
In that case you could split the imported data into two tables using the

date test. And then process only the table that contains the old 
records. The future records would remain in a second table. This second 
table would probably be the same one you import your new csv file 
records into. The processing of the old records would proceed as before.

//assumes table imported contains the imported records
//select records to process based on date and time
create table process as
select * from imported where  (date || ' ' || time) <= 
datetime('now');
delete from imported where id in (select id from process);

You could also skip the concatenation by splitting the data and time 
test if you replace the condition above with

date <= date('now' and time <= time('now')

Its probably a case of six of one or half a dozen of the other.

There really are a lot of different possibilities for processing the 
dat

[sqlite] Format lf csv file RE: [sqlite] date/time implementation question

2007-03-14 Thread Rafi Cohen
Hi Dennis, the first approach is clear now and I may proceed with it.
The second approach is interesting and chalenging, but leaves some
issues to clarify and in case I find solutions to those issues I well
may adopt it.
1. format of csv file: I have no idea how this csv file is created and
which database engine is used. I do know that I receive such a file once
or twice a day.
While reading the file onto the list of structures, I also validate the
consistency of the data in the file.
Before the data lines (starting with "d,") there is a header line
"hdr,". This line contains the sequential number of the file, number of
records in the file and a checksum on a specific field of the data (say
coumn 2).
As I knew nothing up to now about .import, I wonder if there is a way to
include those checings in the second approach?
2. The deletion of the future dates is incorrect. On the contrary, in
the first approach, I re-examine the remaining structures each half a
minute until any of them becomes past date, then I process it just like
any other past date structures and then free it.
In case a new .csv file arrives, I add the new list of structures to the
remaining ones and continue to examine them every half a minute.
I could do the same with the sql3_exec statement in the second approach,
but I need another approach for the case of the future records.
I hope you have satisfying answers for those 2 issues and then I'll be
glad to proceed with the second approach.
Thanks, Rafi.

-Original Message-
From: Dennis Cote [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, March 14, 2007 9:15 PM
To: sqlite-users@sqlite.org
Subject: Re: [sqlite] date/time implementation question


Rafi Cohen wrote:
> Dennis, I really appreciate your patience and willingness to help. 
> Unfortunately, this still did not bring me to the expected solution. I

> will give you a small algorithm of what I need to do and I'm sure 
> after this you'll know how to assist me. 1. I read a .csv file into a 
> linked list of structures. 2. I examine eacch structure one after the 
> other: Compare the datetime filed of the structure aginst the current 
> date. If bigger (future), I skup to the next structure.
> If smaller or equal, I check if a row with the same id field already
> exists in the table:
> Select * from tbl where id = id-in-struct.
> If no such row exists, I insert a row according to the structure's
> field, otherwise, based on another criteria I EITHER UPDATE THE row
with
> a new value on the second column or delete the row.
> 3. After this process, I free the structure from the linked list and
> move to the next structure.
> Because of the last section, I thought I need to make the date
> comparison in C, but I may be wrong here.
> However, if the comparison is made thru sqlite, how can I know if
indeed
> an insert, update or delete was processed so that I can free the
> structures?
> All the sql statements I use above are, of course, prepared statements
> which I execute with wqlite3_step for the fields of each structure.
> I hope I'm clear, this time.
>   
Rafi,

I think I have followed your description and I agree that you could do 
this by iterating in C as you have described. If you want to do that and

all you need is an expedient way to get a date and time string for the 
current time for the comparison in step 2 you have a couple of
approaches.

One simple way is to let sqlite do it for you by executing a single SQL 
statement "select datetime('now')". This will return a ISO date and time

string that you can compare with the concatenated strings that are in 
your structure. The other is to use the standard C library routines to 
build an ISO formatted date and time string. These two examples are 
shown below in a mix of C an pseudo code comments (you have to fill in 
the blanks).

Get current date and time from sqlite:

sqlite3_stmt* get_now;
sqlite3_prepare(db, "select datetime'now'", -1, &get_now, NULL);  

char now_datetime[20];
sqlite3_step(get_now);
strcpy(now_datetime, sqlite3_column_text(get_now, 0));

// read csv into list of structures

//for each structure in the list 
char rec_datetime[20];
strcpy(rec_datetime, a_struct.date);
strcat(rec_datetime, " ");
strcat(rec_datetime, a_struct.time);

if (strcmp(rec_datetime, now_datetime) <= 0) {
//if row with matching id exists in table
//if record should be deleted
//delete record
//else
//update record based on structure
//else
//insert a new record into table   
}

//free the structure
   
 Get current date and time from C library: 

char now_datetime[20];
time_t now = time(NULL);
stru

RE: [sqlite] date/time implementation question

2007-03-14 Thread Rafi Cohen
Dennis, I really appreciate your patience and willingness to help.
Unfortunately, this still did not bring me to the expected solution. I
will give you a small algorithm of what I need to do and I'm sure after
this you'll know how to assist me.
1. I read a .csv file into a linked list of structures.
2. I examine eacch structure one after the other:
Compare the datetime filed of the structure aginst the current date. If
bigger (future), I skup to the next structure.
If smaller or equal, I check if a row with the same id field already
exists in the table:
Select * from tbl where id = id-in-struct.
If no such row exists, I insert a row according to the structure's
field, otherwise, based on another criteria I EITHER UPDATE THE row with
a new value on the second column or delete the row.
3. After this process, I free the structure from the linked list and
move to the next structure.
Because of the last section, I thought I need to make the date
comparison in C, but I may be wrong here.
However, if the comparison is made thru sqlite, how can I know if indeed
an insert, update or delete was processed so that I can free the
structures?
All the sql statements I use above are, of course, prepared statements
which I execute with wqlite3_step for the fields of each structure.
I hope I'm clear, this time.
Thanks, Rafi.

-Original Message-
From: Dennis Cote [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, March 14, 2007 5:21 PM
To: sqlite-users@sqlite.org
Subject: Re: [sqlite] date/time implementation question


Rafi Cohen wrote:
> Thanks Denis for your detailed explanation. My needs differ from the 
> example you gave. I need to compare the date on each row with the 
> current date, if the rowdate has passed then I either insert or update

> this row on a specific table, otherwise I leave this row for a future 
> check. So, the comparison has to be made in C and not sqlite, I think.
> Now, suppose I brought the rowdate to the format "-mm--dd
HH:MM:SS".
> In order to call strcmp() to compare with the current date, I need to
> bring the current date to the same format. How can I do this, or you
> have other ideas to make this comparison?
>   
Rafi,

There is no need for C in case you have given. A simple insert of 
selected data should do.

insert or ignore into over_due
select id from schedule
where (due_date || ' ' || due_time) < datetime('now');

This uses one of sqlite's builtin date and time functions (see 
http://www.sqlite.org/cvstrac/wiki?p=DateAndTimeFunctions ) to format 
the current time into an ISO format that can be compared directly with 
the concatenated fields from your existing table. The id of any records 
that meet the condition are inserted into the over_due table (or ignored

if they already exist in that table).

HTH
Dennis Cote




-
To unsubscribe, send email to [EMAIL PROTECTED]

-



-- 
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.446 / Virus Database: 268.18.11/721 - Release Date:
3/13/2007 4:51 PM



-
To unsubscribe, send email to [EMAIL PROTECTED]
-



[sqlite] results from a prepared select statement

2007-03-14 Thread Rafi Cohen
Hi, I read the documentation but still miss this, so basic information.
I have a prepared select statement which should be executed in a later
state using sqlite3_step in my application.
For a very simple example, suppose the statement is: select * from tbl
where col1 = ?.
Now my question is how do I know, after sqlite3_step, if I got results
at all. I would like my application to have this information before
trying to retrieve the results by using sqlite3_column* functions. From
the other side, if I retrieve those results in a loop, I would like to
now how many rows I need to retrieve?
sorry, so basic but I still miss it.
Thanks, Rafi.


RE: [sqlite] date/time implementation question

2007-03-14 Thread Rafi Cohen
Thanks Denis for your detailed explanation. My needs differ from the
example you gave.
I need to compare the date on each row with the current date, if the
rowdate has passed then I either insert or update this row on a specific
table, otherwise I leave this row for a future check.
So, the comparison has to be made in C and not sqlite, I think.
Now, suppose I brought the rowdate to the format "-mm--dd HH:MM:SS".
In order to call strcmp() to compare with the current date, I need to
bring the current date to the same format. How can I do this, or you
have other ideas to make this comparison?
Thanks, Rafi.

-Original Message-
From: Dennis Cote [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, March 13, 2007 7:15 PM
To: sqlite-users@sqlite.org
Subject: Re: [sqlite] date/time implementation question


Rafi Cohen wrote:
> Hi, I hope this question is not off-topic for this list.
> I'm recieveing, occasionally, a .csv file from a source which I need 
> to process accordcing to some criteria and either insert, delet or 
> update it's rows into a database. One of it's criterias is date 
> comparison. In other words, proceeding differently when the date/time 
> mentioned on a specific row is a past date or future date comparing 
> with the current date/time. My problem is the format in which I 
> recieve the date and time: "HH:MM:SS, --mm-dd".
> This means separate fileds, one for time and one for date.
> If the format was: "-mm-dd HH:MM:SS", I could call strptime to
stor
> this in a struct tm, then cakk mktime to get this in a time_t varialbe
> and then call difftime with the current date/time to make the
comparison
> (I'm using C as programmikng language on Linux).
> So, my question is: should I unify the 2 strings into one to obtain
the
> second format and then proceed as I explained above, or is there a way
> to compare the date with the current date separately and the time with
> the current time separately in case the dates are equal?
> If the conclusion will be to uify the strings into one, should I
create
> a table with a single date/time column or still keep the 2 fileds
> separately in my sql table?
> Thanks, Rafi.
>
>   
Rafi,

It depends. :-)

It depends on what you think is more important, execution speed, 
database size, simplicity of coding, etc...

Assuming the date format you gave has a typo and there is really only 
one '-' between the year and month in your existing fields, you should 
be able to do your date comparisons in sqlite. Use concatenation to 
build a single date and time string. With your date and time in ISO 
format you can then use string comparisons as date comparisons

select
case when (date || ' ' || time) < ?limit_date
then field_one
else field_two
end
from my_table


HTH
Dennis Cote


-
To unsubscribe, send email to [EMAIL PROTECTED]

-



-- 
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.446 / Virus Database: 268.18.10/720 - Release Date:
3/12/2007 7:19 PM



-
To unsubscribe, send email to [EMAIL PROTECTED]
-



[sqlite] date/time implementation question

2007-03-13 Thread Rafi Cohen
Hi, I hope this question is not off-topic for this list.
I'm recieveing, occasionally, a .csv file from a source which I need to
process accordcing to some criteria and either insert, delet or update
it's rows into a database. One of it's criterias is date comparison.
In other words, proceeding differently when the date/time mentioned on a
specific row is a past date or future date comparing with the current
date/time.
My problem is the format in which I recieve the date and time:
"HH:MM:SS, --mm-dd".
This means separate fileds, one for time and one for date.
If the format was: "-mm-dd HH:MM:SS", I could call strptime to stor
this in a struct tm, then cakk mktime to get this in a time_t varialbe
and then call difftime with the current date/time to make the comparison
(I'm using C as programmikng language on Linux).
So, my question is: should I unify the 2 strings into one to obtain the
second format and then proceed as I explained above, or is there a way
to compare the date with the current date separately and the time with
the current time separately in case the dates are equal?
If the conclusion will be to uify the strings into one, should I create
a table with a single date/time column or still keep the 2 fileds
separately in my sql table?
Thanks, Rafi.


RE: [sqlite] use of sqlite in a multithread C application

2007-03-02 Thread Rafi Cohen
Thanks Denis, this helped a lot and actually that's what I was thinking
to do.
Another question: may I prepare all statements tha need to be prepared
in one thread, but for part of them apply the execution process
(bind-step-reset) I n the second thread or whould it be best to prepare
statements in the same thread I intend to execute them later?
Thanks, Rafi.

-Original Message-
From: Dennis Cote [mailto:[EMAIL PROTECTED] 
Sent: Thursday, March 01, 2007 7:48 PM
To: sqlite-users@sqlite.org
Subject: Re: [sqlite] use of sqlite in a multithread C application


Rafi Cohen wrote:
> Hi, I read the good article on this subject and also the api 
> refference in the documentation and still feel that I need to ask the 
> following question. My application has 2 threads. The main one needs 
> to retrieve data thru select commands but does not change the 
> database, while the other thread will change the database quite often.
> My questions are: should I open the database in each thread separately
> or can I open it once for the whole program? in the second case, does
it
> matter inh which thread I open it? last, if the main thread happens to
> retrieve data while the other thread is in a transaction changing the
> database, I would prefer the main thread wait till the end of the
> transaction and retrieve the most updated data. How do you suggest to
> implement it?
> looping on sqlite3_busy_handler should be the way to go?
> Thanks, Rafi.
>
>   
Rafi,

For your access you would be best off to have the main thread open the 
connection and pass a reference to it to the other thread (or you could 
use a global connection variable). Then use a mutex to have the threads 
block until they can get exclusive access to the connection. Once they 
have the mutex they can do their read or write with no concern for 
interruption from the other thread (it will block on the mutex if it 
tries to start a database access).

HTH
Dennis Cote


-
To unsubscribe, send email to [EMAIL PROTECTED]

-



-- 
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.446 / Virus Database: 268.18.5/706 - Release Date:
2/28/2007 4:09 PM



-
To unsubscribe, send email to [EMAIL PROTECTED]
-



[sqlite] use of sqlite in a multithread C application

2007-03-01 Thread Rafi Cohen
Hi, I read the good article on this subject and also the api refference
in the documentation and still feel that I need to ask the following
question.
My application has 2 threads. The main one needs to retrieve data thru
select commands but does not change the database, while the other thread
will change the database quite often.
My questions are: should I open the database in each thread separately
or can I open it once for the whole program? in the second case, does it
matter inh which thread I open it? last, if the main thread happens to
retrieve data while the other thread is in a transaction changing the
database, I would prefer the main thread wait till the end of the
transaction and retrieve the most updated data. How do you suggest to
implement it?
looping on sqlite3_busy_handler should be the way to go?
Thanks, Rafi.


RE: [sqlite] how to insert and select a date column

2007-03-01 Thread Rafi Cohen
Hi Denis, thank you very much for the briliant explanation and the
refference you pointed.
This clearly explains my options here. 
Just another small question to complete this subject: in case of
prepared insert statements, assuming the date is stored as string as in
your example, do I use sqlite3_bind_text with a variable pointing to a
string containing the date I want to insert? In other words, is this a
string just like any other string I insert into the table?
Thanks, Rafi.

-Original Message-
From: Dennis Cote [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, February 28, 2007 5:28 PM
To: sqlite-users@sqlite.org
Subject: Re: [sqlite] how to insert and select a date column


Rafi Cohen wrote:
>  
> Hi, I apologize in advance for the probably very basic question as I'm

> a newbie to sqlite and integrating sql in C applications. One of my 
> tables in my database requires a date column. I understand from a 
> tutorial not related to C/C++ api that insering date columns is not 
> trivial and triggers need to be used. As I am not knowledgeable about 
> triggers, I would like to ask how can I insert a date column in a C 
> application and later retrieve this column or compare it with current 
> date in a select statement? I need to insert just any date in this 
> column and not only the current date.
> If somebody can send me an example off list to implement this, I'll
much
> appreciate it.
> Thanks, Rafi.
>
>   
Rafi,

SQLite does not support SQL standard date and time types. It does 
however have a very flexible set of date and time functions that use 
strings (i.e. -MM-DD and/or HH:MM:SS etc), integer numbers (i.e. 
unix epoch times, seconds since 1970-01-01), or floating point numbers 
(i.e julian day numbers) which can all be stored in the database. These 
function also allow these formats to be easily converted from one type 
to another, in particular, from integer to string or floating point to 
string for display. These numeric formats make date comparisons very 
simple. For more information see 
http://www.sqlite.org/cvstrac/wiki?p=DateAndTimeFunctions

Once you have decided how you want to store your dates you can insert 
them easily using a default value for your column or supplying a date in

the required format. Lets assume you decide to store strings since they 
are human readable, even if somewhat inefficient from a storage point of

view.

create table t (id, data, created default current_date);

insert into t (id, data) values (1, 'test');
insert into t (id, data) values (2, 'more');

select created from t;
2007-02-28
2007-02-28

You can also provide an explicit value that overrides the default value 
when you add a record.

insert into t values (3, 'explicit', '2007-02-14');
insert into t values (3, 'explicit', date('now', '+2 days'));

And you can update these dates using the date and time functions

update t set created = date(created, '-1 month') where id = 2;

Finally you can use the date and time functions to convert the dates for

display.

select created, julianday(created) from t;

HTH
Dennis Cote


-
To unsubscribe, send email to [EMAIL PROTECTED]

-



-- 
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.446 / Virus Database: 268.18.4/705 - Release Date:
2/27/2007 3:24 PM



-
To unsubscribe, send email to [EMAIL PROTECTED]
-



[sqlite] how to insert and select a date column

2007-02-27 Thread Rafi Cohen
 
Hi, I apologize in advance for the probably very basic question as I'm a
newbie to sqlite and integrating sql in C applications.
One of my tables in my database requires a date column. I understand
from a tutorial not related to C/C++ api that insering date columns is
not trivial and triggers need to be used.
As I am not knowledgeable about triggers, I would like to ask how can I
insert a date column in a C application and later retrieve this column
or compare it with current date in a select statement?
I need to insert just any date in this column and not only the current
date.
If somebody can send me an example off list to implement this, I'll much
appreciate it.
Thanks, Rafi.