Python Forum

Full Version: Python3 and sqlite3 using AND with SELECT
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi
I am trying to make a script who should look up some stuff in a database but i cant make it work.
The problem is i need to check two variables against the db to get a return match. So when i use one variable i works but when i use "AND" and two variables i get an error i cant understand.

here is the code.

c.execute('SELECT * FROM tider WHERE Namn=? AND datum=?', [namn, date])
data = c.fetchall()     
for row in data:
  version = (row[3])
This is the error when i use datum=? and date. This error goes away when i remove them.

Error:
Traceback (most recent call last): File "C:\Users\xzenon\Desktop\Ny mapp\timecalculon.py", line 23, in <module> print (version) NameError: name 'version' is not defined
Please post code in context.
The error is for line 23, you only have 4 lines.

That said, you are selecting version from the fourth item in row (row[3]).
Which if this is actually line 4, may be ok.
However, by the time you reach 23 (which we can't see) version may be out of scope, and we have no way of knowing this.

Please post enough code to show all code involved with the error.
(Feb-28-2019, 05:56 PM)Larz60+ Wrote: [ -> ]Please post code in context.
The error is for line 23, you only have 4 lines.

That said, you are selecting version from the fourth item in row (row[3]).
Which if this is actually line 4, may be ok.
However, by the time you reach 23 (which we can't see) version may be out of scope, and we have no way of knowing this.

Please post enough code to show all code involved with the error.

This is the rest of the code.

from datetime import datetime
import time
import random
import sqlite3

conn = sqlite3.connect('database.db')
c = conn.cursor()

ts = time.time()
date = datetime.fromtimestamp(ts).strftime('%Y-%m-%d')
time = datetime.fromtimestamp(ts).strftime('%H:%M:%S')

namn = "david andersen"
c.execute('SELECT * FROM tider WHERE Namn=? AND datum=?', [namn, date])
data = c.fetchall()     
for row in data:
  version = (row[3])





print (version)

Never mind i fixed it like this.

c.execute('SELECT * FROM tider WHERE Namn=? AND datum=?', (namn, date))
put a print statement as below and run, report results
for n, row in enumerate(data):
  print('row number: {}, contents: {}'.format(n, row))
  version = (row[3])
(Feb-28-2019, 08:53 PM)Larz60+ Wrote: [ -> ]put a print statement as below and run, report results
for n, row in enumerate(data):
  print('row number: {}, contents: {}'.format(n, row))
  version = (row[3])

Thank you for your help. But i already fixed it i wrote that in my previous reply and how i did it. Under the complet code.
Sorry about that.
Please share solution for other users.
(Mar-01-2019, 09:59 AM)Larz60+ Wrote: [ -> ]Sorry about that.
Please share solution for other users.

No problems. The solution vas this.

c.execute('SELECT * FROM tider WHERE Namn=? AND datum=?', (namn, date))