简体   繁体   中英

What is causing this syntax error with CREATE TABLE in Python SQLite3?

I'm trying to create a method which creates a table if not exists.

When I try to execute this method, it returns

sqlite3.OperationalError: near "TEXT": syntax error

The method:

def create_table_if_exists(self):
    self.conn.execute(
        "CREATE TABLE IF NOT EXISTS products(NAME TEXT, URL TEXT" #"
        "ESHOP1 TEXT, CENA1 TEXT, DOPRAVA1 TEXT, STAV1 TEXT, "
        "ESHOP2 TEXT, CENA2 TEXT, DOPRAVA2 TEXT, STAV2 TEXT, "
        "ESHOP3 TEXT, CENA3 TEXT, DOPRAVA3 TEXT, STAV3 TEXT, "
        "ESHOP4 TEXT, CENA4 TEXT, DOPRAVA4 TEXT, STAV4 TEXT, "
        "ESHOP5 TEXT, CENA5 TEXT, DOPRAVA5 TEXT, STAV5 TEXT)"
    )
    self.conn.commit()

Error:

    'CREATE TABLE IF NOT EXISTS products(NAME TEXT, URL TEXT, '
sqlite3.OperationalError: near "TEXT": syntax error

I can't realize where could be the problem. Do you have any advices?

The problem is the missing closing paren at the end. The error is, for some reason, only showing you the first line of the query.

For multi line strings, I would recommend using the multi-line ''' quotes to make it easier to read. That, plus proper indentation of your SQL statements, will avoid this sort of syntax errors in the future.

def create_table_if_exists(self):
    self.conn.execute(conn.execute('''
        CREATE TABLE IF NOT EXISTS products(
            NAME TEXT,
            URL TEXT,
            ESHOP1 TEXT, CENA1 TEXT, DOPRAVA1 TEXT, STAV1 TEXT,
            ESHOP2 TEXT, CENA2 TEXT, DOPRAVA2 TEXT, STAV2 TEXT,
            ESHOP3 TEXT, CENA3 TEXT, DOPRAVA3 TEXT, STAV3 TEXT,
            ESHOP4 TEXT, CENA4 TEXT, DOPRAVA4 TEXT, STAV4 TEXT,
            ESHOP5 TEXT, CENA5 TEXT, DOPRAVA5 TEXT, STAV5 TEXT
        )
    ''')
    self.conn.commit()

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