sql - Python Pygresql Insert Current Date -
what correct way insert current time postgresql using pygresql db-api compliant interface? understood postgresql uses 'timedate' , python (3.x) uses 'datetime'. there clean way convert between two? pygresql package displays error when trying insert 'datetime' fields.
pg.programmingerror: error: schema "datetime" not exist
using pygresql type 'timestamp' still uses 'datetime' shown in code below 'sqlcur' postgresql cursor, 'tblnam' table, 'collst' list of columns, , 'insrec' list of records contains 1 field has current time:
## ## load packages ## import pgdb pgdb import connect datetime import datetime import pprint ## ## setup variables ## dbahst = 'localhost' dbausr = 'user' dbapas = 'pass' dbanam = 'db' dbaprt = '5432' tblnam = 'device_table' collst = ['ci_nm', 'loc_txt', 'model_nm', 'datecreated' ] insrec = ['device1', 'location1', 'model1' ] ## ## connect postgresql ## sqlobj = connect( database = dbanam, host = dbahst, port = int(dbaprt), user = dbausr, password = dbapas ) sqlcur = sqlobj.cursor() ## ## setup current time ## = datetime.now() dtenew = pgdb.timestamp(now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond) print( type(dtenew) ) pprint.pprint (dtenew) insrec.append (dtenew) ## ## store record ## sqlcur.execute( "insert %s (%s) values %s" % (tblnam, ', '.join(collst) , tuple(insrec), ) )
the code above displays following output:
<class 'datetime.datetime'> datetime.datetime(2016, 11, 5, 14, 26, 38, 242624) traceback (most recent call last): file "c:\test-pygresql-date.py", line 39, in <module> sqlcur.execute( "insert %s (%s) values (%s)" % (tblnam, ', '.join(collst) , tuple(insrec), ) ) file "c:\users\user1\appdata\local\programs\python\python36\lib\site-packages\pgdb.py", line 913, in execute return self.executemany(operation, [parameters]) file "c:\users\user1\appdata\local\programs\python\python36\lib\site-packages\pgdb.py", line 937, in executemany rows = self._src.execute(sql) pg.programmingerror: error: schema "datetime" not exist line 1: ...reated) values (('device1', 'locatin1', 'model1', datetime.d...
displaying type , 'pprint' of date field 'dtenew' shows still set 'datetime.datetime' instead of 'pgdb.timestamp'.
<class 'datetime.datetime'> datetime.datetime(2016, 11, 5, 14, 26, 38, 242624)
Comments
Post a Comment