Date/Time conversions

Duncan Booth duncan at rcp.co.uk
Tue Apr 20 04:42:59 EDT 1999


Rico Albanese <r.albanese at qut.edu.au> wrote in 
<371BEBA7.5DBC1B0F at qut.edu.au>:

>I am writing a web based database application and I am having problems
>in creating a string that contains an integer in it.   I wish to store
>the login time as an integer so that I may do some time difference
>calculations based  on the stored login time (ie. the integer).  The SQL
>statement string creation fails when I have the integer variable
>"logtime" in it but not when it is removed.  I believe that my problem
>is caused by trying to insert an integer into a string.  The database
>works fine (ie I can add integers to the column "LogTime").
>
>
Your problem is that you are trying to concatenate a string with an 
integer. The arguments to the '+' operator should both be numbers, or both 
strings, you cannot mix strings and numbers here.

There are two obvious solutions. One is to convert logtime to a string. 
e.g.
thesql=thesql + frontpar + str(logtime) + ","

The other, possibly better one is to build up your SQL string using the '%' 
operator. For example, your SQL statement may be build up something like 
this:

SQLtemplate = '''INSERT INTO CSJobs.compjobs
(LogTime,Caller,Phone_Extension,Caller_Category,Location,Job_Description,Jo
b_Priority,LogNumb,Log_Date,Finished)
VALUES (%(logtime)s, '%(Caller)s', '%(Ph)s', '%(Sch)s', '%(Loc)s', 
'%(JbDes)s', '%(JbPr)s', '%(LogN)s', '%(logdate)s', 'n')
''' 
thesql = SQLtemplate % locals()
crsr.execute(thesql)

Which does all the neccessary conversions to strings as you go.


-- 
Duncan Booth                                    duncan at dales.rmplc.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
http://dales.rmplc.co.uk/Duncan




More information about the Python-list mailing list