【问题标题】:sqlite3.OperationalError: near ")": syntax error in tkinter Pythonsqlite3.OperationalError:靠近“)”:tkinter Python中的语法错误
【发布时间】:2020-11-23 07:40:27
【问题描述】:

我正在尝试在我的 tkinter 应用程序中使用 python 中的 sqlite 创建一个数据库 - 我正在遵循我之前使用过的教程并像他一样正确输入了文本但是我一直收到一个我无法解决的错误 - 代码是下面。

错误是:c.execute("""CREATE TABLE jobdata ( sqlite3.OperationalError:靠近“)”:语法错误

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()

我已将它放在我为 GUI 构建的类上方的代码顶部 - 如果需要,我可以发布其余代码。我已经检查了很多次语法,发现它与教程的编写方式没有什么不同。任何帮助将不胜感激!

谢谢!

【问题讨论】:

  • SQL 语句中) 前的多余逗号:..., start_date integer,)。删除它。

标签: python database sqlite tkinter syntax-error


【解决方案1】:

嘿,你做错了,你在最后添加了额外的,

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

正确的


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

【讨论】:

  • 这个答案和上一个答案有什么主要区别吗?
  • 请在 SQL 语句中使用有效的注释(--/* */),否则如果 OP 只是复制您的答案并运行它会引发错误。
【解决方案2】:

解决错误

表定义中的最后一个字段后面有一个逗号,这可能是导致错误的原因。

start_date integer, 应更新为 start_date integer

减少未来的错误

如果您要多次运行包含此脚本的文件,我还建议您在语句中添加IF NOT EXISTS 条件。如果您设法创建表,然后尝试运行将创建同名表的语句,您很可能会收到以下错误 sqlite3.OperationalError: table jobdata already exists

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

上下文管理器

在使用sqlite3 连接 (see docs) 执行数据库事务时,可能值得考虑使用上下文管理器。

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

显然在某些情况下您可能不想使用上下文管理器,但这似乎是使用上下文管理器的可靠候选者,如果您不了解此功能,只是认为值得与您分享。

使用上下文管理器处理以下数据库事务的原始问题中的代码示例(包括直接响应上述问题所做的更改):

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

【讨论】:

  • 感谢详细的答案,文档链接对我有很大帮助!
猜你喜欢
  • 2016-11-29
  • 1970-01-01
  • 1970-01-01
  • 2021-05-11
  • 2019-02-07
  • 2020-02-06
  • 2019-01-12
  • 2014-05-17
  • 2021-10-19
相关资源
最近更新 更多