2

My goal is to take two variables, xdate and xtime and store them into an sqlite database in two separate columns using a python scripts. My code is

from datetime import datetime import sqlite3 as mydb import sys con = mydb.connect('testTime.db') def logTime(): i=datetime.now() xdate = i.strftime('%Y-%m-%d') xtime = i.strftime('%H-%M-%S') return xdate, xtime z=logTime() 

this is where I get hung up I tried

try: with con: cur = con.cursor cur.execute('INSERT INTO DT(Date, Time) Values (?,?)' (z[0],z[1])) data = cur.fetchone() print (data) con.commit() except: with con: cur=con.cursor() cur.execute("CREATE TABLE DT(Date, Time)') cur.commit() 

I keep getting none when I try to fetch the data.

Any tips or recommended readings??

1 Answer 1

1

You are executing a insert query, it's result is not having any thing to fetch. You should run a select query and then fetch the data.

fetchone()

Fetches the next row of a query result set, returning a single sequence, or None when no more data is available. 

An example -

>>> cur.execute('INSERT INTO DT(Date, Time) Values (?,?)', (z[0],z[1])) <sqlite3.Cursor object at 0x0353DF60> >>> print cur.fetchone() None >>> cur.execute('SELECT Date, Time from DT') <sqlite3.Cursor object at 0x0353DF60> >>> print cur.fetchone() (u'2016-02-25', u'12-46-16') 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.