简体   繁体   中英

sqlite3.OperationalError: near “)”: syntax error in tkinter Python

I am trying to create a database using sqlite in python within my tkinter application - I was following a tutorial which i have used before and typed the text correctly as he had however I keep receiving an error I cannot solve - the code is below.

the error is: c.execute("""CREATE TABLE jobdata ( sqlite3.OperationalError: near ")": syntax error

import tkinter as tk 
from tkinter import ttk
from tkinter import *
import sqlite3
   
  
LARGEFONT =("Verdana", 35) 


conn = sqlite3.connect('jobtracker.db')

c = conn.cursor()

c.execute("""CREATE TABLE jobdata (
            company text,
            role text,
            industry text,
            location text,
            wage integer,
            start_date integer,
            )""")

conn.commit()

conn.close()

I have placed this at the top of my code above my classes I have built for my GUI - I can post the rest of the code if need be. I have gone over the syntax so many times and found it no different to how the tutorial had written it. Any help would be greatly appreciated!

Thanks!

Resolving the error

There's a comma following the last field in your table definition, this is likely what's causing error.

start_date integer, should be updated to start_date integer .

Mitigating future errors

I would also recommend that you add an IF NOT EXISTS condition to your statement if you are going to be running the file containing this script multiple times. If you manage to create the table and then try to run a statement which would create a table with the same name you will most likely get the following error sqlite3.OperationalError: table jobdata already exists .

Proposed solution #1

# Your existing code as is before CREATE TABLE statement 
c.execute("""CREATE TABLE IF NOT EXISTS jobdata (
            company text,
            role text,
            industry text,
            location text,
            wage integer,
            start_date integer
            );""")
# Your existing code as is after CREATE TABLE statement

Context Managers

It may be worth considering using a context manager when performing db transactions using an sqlite3 connection ( see docs ).

I find them useful in terms of readability and safety, they automatically commit/rollback transactions - meaning that forgetting to run conn.commit() is not a concern.

Obviously there are cases in which you might not want to use a context manager, however this seems like a solid candidate for using one, just thought this worth sharing with you if you are unaware of this feature.

An example of the code from your original question using a context manager to handle the db transaction below (including changes made in direct response to question above):

Proposed solution #2

import tkinter as tk 
from tkinter import ttk
from tkinter import *
import sqlite3
   
  
LARGEFONT =("Verdana", 35) 


conn = sqlite3.connect('jobtracker.db')

with conn:
    conn.execute("""CREATE TABLE IF NOT EXISTS jobdata (
                company text,
                role text,
                industry text,
                location text,
                wage integer,
                start_date integer
                );""")
conn.close()

Hey you are doing a mistake you are adding extra , at the last of

c.execute("""CREATE TABLE jobdata (
            company text,
            role text,
            industry text,
            location text,
            wage integer,
            start_date integer, )""")#here you are using the extra ```,```  

The correct one


c.execute("""CREATE TABLE jobdata (
            company text,
            role text,
            industry text,
            location text,
            wage integer,
            start_date integer)""") #remove the coma 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM