简体   繁体   English

sqlite3.OperationalError:“)”附近:tkinter Python 中的语法错误

[英]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.我正在尝试在我的 tkinter 应用程序中使用 python 中的 sqlite 创建数据库 - 我正在遵循我之前使用过的教程并输入了我无法正确解决的文本,因为他有以下代码。 -

the error is: c.execute("""CREATE TABLE jobdata ( sqlite3.OperationalError: near ")": syntax error错误是:c.execute("""CREATE TABLE jobdata (sqlite3.OperationalError: near ")": 语法错误

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.我已将它放在我为 GUI 构建的类上方的代码顶部 - 如果需要,我可以发布代码的 rest。 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 . start_date integer,应更新为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 NOT EXISTS条件。 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 .如果您设法创建表,然后尝试运行将创建同名表的语句,您很可能会收到以下错误sqlite3.OperationalError: table jobdata already exists

Proposed solution #1建议的解决方案#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 ).在使用sqlite3连接执行数据库事务时,可能值得考虑使用上下文管理器(请参阅文档)。

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.我发现它们在可读性和安全性方面很有用,它们会自动提交/回滚事务——这意味着忘记运行conn.commit()不是问题。

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建议的解决方案#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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sqlite3.OperationalError:“,”附近:语法错误python - sqlite3.OperationalError: near “,”: syntax error python python sqlite3.OperationalError:“-”附近:语法错误 - python sqlite3.OperationalError: near “-”: syntax error sqlite3.OperationalError:靠近“WHERE”:语法错误(Python 2,sqlite3) - sqlite3.OperationalError: near “WHERE”: syntax error (Python 2, sqlite3) Python-sqlite3 sqlite3.OperationalError:接近“%”:语法错误? - Python - sqlite3 sqlite3.OperationalError: near “%”: syntax error? python- sqlite3.OperationalError:“,”附近:语法错误” - python- sqlite3.OperationalError: near “,”: syntax error" sqlite3.OperationalError:靠近“&lt;”:语法错误:python 格式中的 sql 问题? - sqlite3.OperationalError: near "<": syntax error: Issue with sql in python formatting? python- sqlite3.OperationalError:“ &lt;”附近:语法错误 - python- sqlite3.OperationalError: near “<”: syntax error sqlite3.OperationalError:靠近“?”:python中的语法错误—使用“ IN”运算符 - sqlite3.OperationalError: near “?”: syntax error in python — using 'IN' operator sqlite3.OperationalError:接近“&lt;”:语法错误 - sqlite3.OperationalError: near "<": syntax error sqlite3.OperationalError:接近“,”:语法错误 - sqlite3.OperationalError: near ",": syntax error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM