1

I'm writing code for a project, and when using SQLite3 I came across this error that I can't seem to fix.

I've looked up the answer before, but it always seems to be a case of the SQLite reading a variable as a column title, but here the column it can't find isn't one of my variables so I'm a bit lost.

I am attempting to insert data from variables into an SQLite database, and while at first I got a simpler version working okay, now that I try and integrate it into my full code I'm getting stuck again.

Here's a trial version that I'm using to get the whole thing working before I try and use it properly:

c.execute("CREATE TABLE TEST (GIFNAME, TagStyle, TagEvent, TagTime)")
def SaveStyle():
    if (GIFName.get() != "") and (TagStyle.get() != ""):
        #print(GIFName.get(), ", ", TagStyle.get(), ", ", TagEvent.get(), ", ", TagTime.get())
        #Saved1=tk.Label(window4, text="Saved!", fg="#ffffff", bg="#D0CEC2")
        #Saved1.grid(row=9, column=1)
        c.execute("INSERT INTO TEST VALUES (%s,%s,%s,%s)" %(GIFName2, TagStyle2, TagEvent2, TagTime2))
        conn.commit()
        c.execute("select * from TEST")
        print(c.fetchall())

I know the code isn't great, I'm relatively new to coding so it's not exactly perfect, I just want to get that one part fixed because it will be used for quite a few parts of my program.

The 4 variables its grabbing are being used because the original ones are tk.StringVar() and the sqlite kept coming back saying they were unsupported.

Here is the full error message:

"Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\idlelib\run.py", line 109, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File "C:\Python33\lib\queue.py", line 175, in get
raise Empty
queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
File "N:\PYTHON\Alice Program\trial.py", line 16, in SaveStyle
c.execute("INSERT INTO TEST VALUES (%s,%s,%s,%s)" %(GIFName2, TagStyle2,  TagEvent2, TagTime2))
sqlite3.OperationalError: no such column: PY_VAR0"

(Sorry I had to format this as code, it wouldn't let me post it as a quote)

Originally I was using ? instead of %s but it kept saying my variables were unsupported, and a friend is using %s and got it working, but she can't remember what she did, and now she's lost the file so I can't try and use it for help.

Thanks in advance for your help, and if I've missed any information, let me know.

  • 1
    Have you tried searching this site for `PY_VAR0`? This sort of question comes up a lot, and it's always due to the same reason. – Bryan Oakley Mar 14 '16 at 16:15

1 Answers1

0

The syntax of your INSERT INTO statement isn't quite right.

It should be:

c.execute("INSERT INTO TEST VALUES (?,?,?,?)", (GIFName2, TagStyle2, TagEvent2, TagTime2))

A question mark (?) is the parameter substitution symbol for sqlite in python; using this will ensure that your data is inserted into the database securely - see the sqlite3 documentation.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
  • This gets it to save something, so thank you, but if I force it to read the variables as strings it just says that they are "Py_Var0" 1,2,3, but if I don't force it to read them as strings it says they are 'probably unsupported' – Lucy Spiteri-Beale Mar 15 '16 at 10:01
  • As @BryanOakley suggested in his comment, have a look at questions like http://stackoverflow.com/questions/24768455/tkinter-intvar-returning-py-var0-instead-of-value - you probably need to call .get() on the variables you're saving to get their values, for example GifName2.get() instead of just GifName2. – snakecharmerb Mar 15 '16 at 10:06
  • Yeah, I just tried that, and it's working now so thank you :) – Lucy Spiteri-Beale Mar 15 '16 at 10:18