简体   繁体   English

创建表时,自动增量在 pyodbc 中引发错误

[英]Auto Increment throws error in pyodbc when table is created

I have a list of parts that I need to put into an MDB database.我有一个需要放入 MDB 数据库的部件列表。 I used pyOBDC to create the table and run SQL commands for it.我使用 pyOBDC 创建表并为其运行 SQL 命令。 I am relatively new to SQL.我对 SQL 比较陌生。 My code is as follows:我的代码如下:

# Create database in current directory
db_path = '{}\juki_new_db.mdb'.format(os.getcwd())

# If database does not exist, create it
if not os.path.exists(db_path):
    msaccessdb.create(db_path)

conn_str = (r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};' + 'DBQ={};'.format(db_path))
conn = pyodbc.connect(conn_str)
cursor = conn.cursor()

# Check if components table exists, if not create it
if not cursor.tables(table='Compo').fetchone():
    sql = """create table Compo
                        (
                            id int not null identity(1,1),
                            mpn text not null,
                            width int,
                            length int,
                            height int,
                            compoval text,
                            primary key (id)
                        );"""
    cursor.execute(sql)
    cursor.commit()

When I remove the keyword identity and nothing else, the program runs correctly.当我删除关键字标识时,程序运行正常。 I have tried using auto increment, but it throws the same error.我曾尝试使用自动增量,但它会引发相同的错误。

Traceback (most recent call last):
  File "C:\Users\SMT-PRODUCTION\Documents\Python Scripts\SMT Database Updater\octopart_search.py", line 385, in <module>
    main()
  File "C:\Users\SMT-PRODUCTION\Documents\Python Scripts\SMT Database Updater\octopart_search.py", line 371, in main
    cursor.execute(sql)
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][Controlador ODBC Microsoft Access] Error de sintaxis en la instrucción CREATE TABLE. (-3551) (SQLExecDirectW)')

I work for a multilingual company so the error is in Spanish.我在一家多语言公司工作,所以错误是西班牙语。 It translates to, "Syntax error in the instruction CREATE TABLE".它转换为“CREATE TABLE 指令中的语法错误”。 Any help would be appreciated.任何帮助,将不胜感激。

This is the correct syntax:这是正确的语法:

[int] IDENTITY(1,1) NOT NULL

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM