简体   繁体   中英

How to fix syntax error in SQLite in Python

I've finished making some SQLite tables and am executing the instructions. When executing the instructions, the following error has come up:

sqlite3.OperationalError: near "Category": syntax error

Most of my tables use the same sort of format, below is an example of one such table.

CategoryTableSQL = """ CREATE TABLE IF NOT EXISTS Category(
                            CategoryID integer PRIMARY KEY AUTOINCREMENT
                            Category text NOT NULL
                        );"""
databaseNewTable(Connection, CategoryTableSQL)

You forgot a comma between the declaration of your table fields in your SQL-statement: It should be like this. Always use comma's to seperate the field-creation statements. Except of course, for the last field you create =). Also, I would take care in naming your fields the same name as your tables. To prevent confusion. Just my two cents

 CREATE TABLE IF NOT EXISTS Category(
                            CategoryID integer PRIMARY KEY AUTOINCREMENT,
                            Category text NOT NULL
                        );

I think, You just need to add the ',' symbol after the AUTOINCREMENT word. :-) Like this:

CategoryTableSQL = """ CREATE TABLE IF NOT EXISTS Category(
                        CategoryID integer PRIMARY KEY AUTOINCREMENT,
                        Category text NOT NULL
                    );"""
databaseNewTable(Connection, CategoryTableSQL)

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