sqlite3.OperationalError:靠近“EXIST”:语法错误

pgpifvop  于 2023-03-13  发布在  SQLite
关注(0)|答案(1)|浏览(179)

当我在python中创建数据库时出现语法错误:

File "C:\Users\CARLO\Desktop\esercizi python\programma trivium\backend.py", line 7, in connect
    curs.execute("CREATE TABLE IF NOT EXIST book (id INTEGER PRIMARY KEY, categoria text, titolo text, autore text, "
sqlite3.OperationalError: near "EXIST": syntax error

我不明白,因为在所有的解释代码是这样的

import sqlite3

def connect():
    conn = sqlite3.connect('libri.db')
    curs = conn.cursor()
    curs.execute("CREATE TABLE IF NOT EXIST book (id INTEGER PRIMARY KEY, categoria text, titolo text, autore text, "
                 "anno integer, volume integer")
    conn.commit()
    conn.close()
e4yzc0pl

e4yzc0pl1#

有两个因素:

  1. CREATE TABLE IF NOT EXIST-〉CREATE TABLE IF NOT EXISTS(缺少S
    1.查询字符串被分成2个字符串。
    因此,这应该是可行的(而且更清楚一点):
import sqlite3

def connect():
    conn = sqlite3.connect('libri.db')
    curs = conn.cursor()
    query_string = """ 
        CREATE TABLE IF NOT EXISTS book 
        (id INTEGER PRIMARY KEY, 
        categoria text, 
        titolo text, 
        autore text, 
        anno integer, 
        volume integer)
        """
    curs.execute(query_string)
    conn.commit()
    conn.close()

connect()

相关问题