Python Forum
Python3 and sqlite3 using AND with SELECT
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3 and sqlite3 using AND with SELECT
#1
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
Reply
#2
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.
Reply
#3
(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))
Reply
#4
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])
Reply
#5
(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.
Reply
#6
Sorry about that.
Please share solution for other users.
Reply
#7
(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))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,889 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  SQL select join operation in python(Select.. join) pradeepkumarbe 1 2,231 Feb-14-2019, 08:34 PM
Last Post: woooee
  How retrieve sqlite3 database saved image and access in python3 tao01 1 2,457 Dec-22-2018, 09:04 PM
Last Post: Larz60+
  select one item from row with sqlite3 mcmxl22 1 2,403 Sep-24-2018, 01:08 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020