Date insert from python to mysql "out of range" issue -



Date insert from python to mysql "out of range" issue -

i've created mysql db l column "date":

+--------+---------+------+-----+---------+-------+ | field | type | null | key | default | | +--------+---------+------+-----+---------+-------+ | datum | date | yes | | null | | | stroom | int(15) | yes | | null | | | gas | int(15) | yes | | null | | | water | int(15) | yes | | null | | +--------+---------+------+-----+---------+-------+

yet, when seek insert date python script returns: warning: out of range value column 'datum' @ row 1

my script looks this:

#!/usr/bin/python import datetime import mysqldb # define strings = datetime.datetime.now() mydatum = ("%s-%s-%s" % (i.day, i.month, i.year)) #mydatum = time.strftime("%y-%m-%d") mystroom = open('/home/pi/textfiles/stroomverbruik.txt', 'r').read(4) mygas = open('/home/pi/textfiles/gasverbruik.txt', 'r').read(4) mywater = open('/home/pi/textfiles/waterverbruik.txt', 'r').read(4) # open database connection db = mysqldb.connect("localhost","root","[password]","p1" ) # prepare cursor object using cursor() method cursor = db.cursor() # prepare sql query insert record database. sql = "insert verbruik (datum, stroom, gas, water) values(%s,%s,%s,%s)" % (mydatum, mystroom, mygas, mywater) try: # execute sql command cursor.execute(sql) # commit changes in database db.commit() except: # rollback in case there error db.rollback() # disconnect server db.close() print mydatum print mystroom print mygas print mywater

is mysql expecting date-format? other values correctly stored in database, date value stored 0000-00-00. doing wrong?

looks python code isn't formatting date putting d-m-y instead of y-m-d format...

try changing

mydatum = ("%s-%s-%s" % (i.day, i.month, i.year))

to

mydatum = ("%s-%s-%s" % (i.year, i.month, i.day))

in python code.

edit reflect solution derived comments:

the date's %s had quoted well, since database had told process string. modified line:

sql = "insert verbruik (datum, stroom, gas, water) values('%s',%s,%s,%s)" % (mydatum, mystroom, mygas, mywater)

python mysql-python

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -