SQL INSERT INTO 对我的 Access 数据库不起作用
SQL INSERT INTO does not work my Access database
我使用与 MS Access 交互的有点旧的软件(2000 年代)。我正在使用 Python 编写我需要插入到软件中的 MDB 文件。我需要更新 7 个不同 table 的值以获得预期值。对于所有 7 tables,我使用以下代码:
def sql_cols(df):
cols_aux = tuple(df.columns)
cols_aux = str(cols_aux).replace("'","")
cols_aux = cols_aux.replace(', DESC', ', [DESC]')
return cols_aux
for k in range(len(df)):
vals = tuple(list(df.iloc[k]))
cols = sql_cols(df)
action = 'INSERT INTO Strategy {columns} VALUES {values}'.format(columns = cols, values = str(vals).replace("'",''))
cursor.execute(action)
conn.commit()
在 6 tables 中,此过程有效,但我不断收到 ('42000', '[42000] [Microsoft][ODBC Microsoft Access Driver] INSERT INTO 语句中的语法错误。(-3502) (SQLExecDirectW)') 在最后 table。 table 的操作语句类似于:
'INSERT INTO Strategy (STRATGY_ID, NUMBUDGETS, PDISC_RATE, INC_IMPS, OPT_METHOD, OBJ_FUNC, MININCVAL, EFFFZONE, BUDGETPD, REPORTING, NUM_GROUPS, MATRIX_DEF, NAME, [DESC], FLT_CONRTE, WRK_CONRTE, NET_CONRTE, START_YEAR, END_YEAR, CURRENCY, FLEET, NETWORK, NUM_SECSEL, NUM_VEHSEL, ANAL_MODE, NUMPRJOPTS, NUMSECOPTS, INC_NMT, DOECONANAL, DISC_RATE, BASE_OPTN, ACCCOSTS, FATALCOST, INJURYCOST, DAMAGECOST, ALLACCCOST, ENERGY, EMISSIONS, ACCLEFFECT, LOGFILE, INCSENSTVY, NUMSENSCEN, DOASSETVAL, EXCVEHDATA, EXCPERDATA)
VALUES (0, 0, , 0, 1, 0, 0, 95, 0, -1, 0, 0, Road_Networks_PA, , 1, 1, 1, 2019, 2038, Reais, Vehicles 2017, Road_Networks_PA, 157, 12, 0, , 157, -1, -1, 6.49, , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, , 0, -1, -1)'
我已经尝试将所有列放在方括号 [] 中,但它也不起作用。谁能帮我解决这个问题?
编辑: 按照 Gordon 的建议,我更改了代码,但出现了同样的错误:
for k in range(len(df)):
vals = tuple(list(df.iloc[k]))
cols = sql_cols(df)
action = 'INSERT INTO Strategy {columns} VALUES {values}'.format(columns = cols, values = str(vals))
cursor.execute(action)
conn.commit()
动作语句是:
"INSERT INTO Strategy (STRATGY_ID, NUMBUDGETS, PDISC_RATE, INC_IMPS, OPT_METHOD, OBJ_FUNC, MININCVAL, EFFFZONE, BUDGETPD, REPORTING, NUM_GROUPS, MATRIX_DEF, NAME, [DESC], FLT_CONRTE, WRK_CONRTE, NET_CONRTE, START_YEAR, END_YEAR, CURRENCY, FLEET, NETWORK, NUM_SECSEL, NUM_VEHSEL, ANAL_MODE, NUMPRJOPTS, NUMSECOPTS, INC_NMT, DOECONANAL, DISC_RATE, BASE_OPTN, ACCCOSTS, FATALCOST, INJURYCOST, DAMAGECOST, ALLACCCOST, ENERGY, EMISSIONS, ACCLEFFECT, LOGFILE, INCSENSTVY, NUMSENSCEN, DOASSETVAL, EXCVEHDATA, EXCPERDATA)
VALUES (0, 0, ' ', 0, 1, 0, 0, 95, 0, -1, 0, 0, 'Road_Networks_PA', ' ', 1, 1, 1, 2019, 2038, 'Reais', 'Vehicles 2017', 'Road_Networks_PA', 157, 12, 0, ' ', 157, -1, -1, 6.49, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ' ', 0, -1, -1)"
编辑 2: 正如 Kshitij Saxena 所问,这是我的数据类型
Field Name Data Type
STRATGY_ID Number
NUMBUDGETS Number
PDISC_RATE Number
INC_IMPS Yes/No
OPT_METHOD Number
OBJ_FUNC Number
MININCVAL Number
EFFFZONE Number
BUDGETPD Yes/No
REPORTING Yes/No
NUM_GROUPS Number
MATRIX_DEF Yes/No
NAME Text
DESC Text
FLT_CONRTE Number
WRK_CONRTE Number
NET_CONRTE Number
START_YEAR Number
END_YEAR Number
CURRENCY Text
FLEET Text
NETWORK Text
NUM_SECSEL Number
NUM_VEHSEL Number
ANAL_MODE Number
NUMPRJOPTS Number
NUMSECOPTS Number
INC_NMT Yes/No
DOECONANAL Yes/No
DISC_RATE Number
BASE_OPTN Text
ACCCOSTS Yes/No
FATALCOST Number
INJURYCOST Number
DAMAGECOST Number
ALLACCCOST Number
ENERGY Yes/No
EMISSIONS Yes/No
ACCLEFFECT Yes/No
LOGFILE Yes/No
INCSENSTVY Yes/No
NUMSENSCEN Number
DOASSETVAL Yes/No
EXCVEHDATA Yes/No
EXCPERDATA Yes/No
正如 Gordon 所说,您需要在字符串周围加上引号,并且您不应在逗号之间留空 space。尝试插入 0 或 Null 或更适合该字段的任何内容:
VALUES (0, 0, Null, 0,
VARCHAR 列需要用引号引起来的值。尝试构建您的查询:
INSERT INTO Strategy (STRATGY_ID, NUMBUDGETS, PDISC_RATE, INC_IMPS, OPT_METHOD, OBJ_FUNC, MININCVAL, EFFFZONE, BUDGETPD, REPORTING, NUM_GROUPS, MATRIX_DEF, NAME, [DESC], FLT_CONRTE, WRK_CONRTE, NET_CONRTE, START_YEAR, END_YEAR, CURRENCY, FLEET, NETWORK, NUM_SECSEL, NUM_VEHSEL, ANAL_MODE, NUMPRJOPTS, NUMSECOPTS, INC_NMT, DOECONANAL, DISC_RATE, BASE_OPTN, ACCCOSTS, FATALCOST, INJURYCOST, DAMAGECOST, ALLACCCOST, ENERGY, EMISSIONS, ACCLEFFECT, LOGFILE, INCSENSTVY, NUMSENSCEN, DOASSETVAL, EXCVEHDATA, EXCPERDATA)
VALUES (0, 0, NULL , 0, 1, 0, 0, 95, 0, -1, 0, 0, "Road_Networks_PA", NULL , 1, 1, 1, 2019, 2038, "Reais", "Vehicles 2017", "Road_Networks_PA", 157, 12, 0, NULL, 157, -1, -1, 6.49, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL , 0, -1, -1)
为要保留为空的列添加 NULL 或“”(空字符串)。
CURRENCY
是 Access SQL 中的保留字,因此如果您有一个具有该名称的列,则必须将其括在方括号中。也就是说,这将失败 "Syntax error in INSERT INTO statement." ...
crsr.execute("INSERT INTO Strategy (CURRENCY) VALUES ('USD')")
...但这会起作用:
crsr.execute("INSERT INTO Strategy ([CURRENCY]) VALUES ('USD')")
我使用与 MS Access 交互的有点旧的软件(2000 年代)。我正在使用 Python 编写我需要插入到软件中的 MDB 文件。我需要更新 7 个不同 table 的值以获得预期值。对于所有 7 tables,我使用以下代码:
def sql_cols(df):
cols_aux = tuple(df.columns)
cols_aux = str(cols_aux).replace("'","")
cols_aux = cols_aux.replace(', DESC', ', [DESC]')
return cols_aux
for k in range(len(df)):
vals = tuple(list(df.iloc[k]))
cols = sql_cols(df)
action = 'INSERT INTO Strategy {columns} VALUES {values}'.format(columns = cols, values = str(vals).replace("'",''))
cursor.execute(action)
conn.commit()
在 6 tables 中,此过程有效,但我不断收到 ('42000', '[42000] [Microsoft][ODBC Microsoft Access Driver] INSERT INTO 语句中的语法错误。(-3502) (SQLExecDirectW)') 在最后 table。 table 的操作语句类似于:
'INSERT INTO Strategy (STRATGY_ID, NUMBUDGETS, PDISC_RATE, INC_IMPS, OPT_METHOD, OBJ_FUNC, MININCVAL, EFFFZONE, BUDGETPD, REPORTING, NUM_GROUPS, MATRIX_DEF, NAME, [DESC], FLT_CONRTE, WRK_CONRTE, NET_CONRTE, START_YEAR, END_YEAR, CURRENCY, FLEET, NETWORK, NUM_SECSEL, NUM_VEHSEL, ANAL_MODE, NUMPRJOPTS, NUMSECOPTS, INC_NMT, DOECONANAL, DISC_RATE, BASE_OPTN, ACCCOSTS, FATALCOST, INJURYCOST, DAMAGECOST, ALLACCCOST, ENERGY, EMISSIONS, ACCLEFFECT, LOGFILE, INCSENSTVY, NUMSENSCEN, DOASSETVAL, EXCVEHDATA, EXCPERDATA)
VALUES (0, 0, , 0, 1, 0, 0, 95, 0, -1, 0, 0, Road_Networks_PA, , 1, 1, 1, 2019, 2038, Reais, Vehicles 2017, Road_Networks_PA, 157, 12, 0, , 157, -1, -1, 6.49, , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, , 0, -1, -1)'
我已经尝试将所有列放在方括号 [] 中,但它也不起作用。谁能帮我解决这个问题?
编辑: 按照 Gordon 的建议,我更改了代码,但出现了同样的错误:
for k in range(len(df)):
vals = tuple(list(df.iloc[k]))
cols = sql_cols(df)
action = 'INSERT INTO Strategy {columns} VALUES {values}'.format(columns = cols, values = str(vals))
cursor.execute(action)
conn.commit()
动作语句是:
"INSERT INTO Strategy (STRATGY_ID, NUMBUDGETS, PDISC_RATE, INC_IMPS, OPT_METHOD, OBJ_FUNC, MININCVAL, EFFFZONE, BUDGETPD, REPORTING, NUM_GROUPS, MATRIX_DEF, NAME, [DESC], FLT_CONRTE, WRK_CONRTE, NET_CONRTE, START_YEAR, END_YEAR, CURRENCY, FLEET, NETWORK, NUM_SECSEL, NUM_VEHSEL, ANAL_MODE, NUMPRJOPTS, NUMSECOPTS, INC_NMT, DOECONANAL, DISC_RATE, BASE_OPTN, ACCCOSTS, FATALCOST, INJURYCOST, DAMAGECOST, ALLACCCOST, ENERGY, EMISSIONS, ACCLEFFECT, LOGFILE, INCSENSTVY, NUMSENSCEN, DOASSETVAL, EXCVEHDATA, EXCPERDATA)
VALUES (0, 0, ' ', 0, 1, 0, 0, 95, 0, -1, 0, 0, 'Road_Networks_PA', ' ', 1, 1, 1, 2019, 2038, 'Reais', 'Vehicles 2017', 'Road_Networks_PA', 157, 12, 0, ' ', 157, -1, -1, 6.49, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ' ', 0, -1, -1)"
编辑 2: 正如 Kshitij Saxena 所问,这是我的数据类型
Field Name Data Type
STRATGY_ID Number
NUMBUDGETS Number
PDISC_RATE Number
INC_IMPS Yes/No
OPT_METHOD Number
OBJ_FUNC Number
MININCVAL Number
EFFFZONE Number
BUDGETPD Yes/No
REPORTING Yes/No
NUM_GROUPS Number
MATRIX_DEF Yes/No
NAME Text
DESC Text
FLT_CONRTE Number
WRK_CONRTE Number
NET_CONRTE Number
START_YEAR Number
END_YEAR Number
CURRENCY Text
FLEET Text
NETWORK Text
NUM_SECSEL Number
NUM_VEHSEL Number
ANAL_MODE Number
NUMPRJOPTS Number
NUMSECOPTS Number
INC_NMT Yes/No
DOECONANAL Yes/No
DISC_RATE Number
BASE_OPTN Text
ACCCOSTS Yes/No
FATALCOST Number
INJURYCOST Number
DAMAGECOST Number
ALLACCCOST Number
ENERGY Yes/No
EMISSIONS Yes/No
ACCLEFFECT Yes/No
LOGFILE Yes/No
INCSENSTVY Yes/No
NUMSENSCEN Number
DOASSETVAL Yes/No
EXCVEHDATA Yes/No
EXCPERDATA Yes/No
正如 Gordon 所说,您需要在字符串周围加上引号,并且您不应在逗号之间留空 space。尝试插入 0 或 Null 或更适合该字段的任何内容:
VALUES (0, 0, Null, 0,
VARCHAR 列需要用引号引起来的值。尝试构建您的查询:
INSERT INTO Strategy (STRATGY_ID, NUMBUDGETS, PDISC_RATE, INC_IMPS, OPT_METHOD, OBJ_FUNC, MININCVAL, EFFFZONE, BUDGETPD, REPORTING, NUM_GROUPS, MATRIX_DEF, NAME, [DESC], FLT_CONRTE, WRK_CONRTE, NET_CONRTE, START_YEAR, END_YEAR, CURRENCY, FLEET, NETWORK, NUM_SECSEL, NUM_VEHSEL, ANAL_MODE, NUMPRJOPTS, NUMSECOPTS, INC_NMT, DOECONANAL, DISC_RATE, BASE_OPTN, ACCCOSTS, FATALCOST, INJURYCOST, DAMAGECOST, ALLACCCOST, ENERGY, EMISSIONS, ACCLEFFECT, LOGFILE, INCSENSTVY, NUMSENSCEN, DOASSETVAL, EXCVEHDATA, EXCPERDATA)
VALUES (0, 0, NULL , 0, 1, 0, 0, 95, 0, -1, 0, 0, "Road_Networks_PA", NULL , 1, 1, 1, 2019, 2038, "Reais", "Vehicles 2017", "Road_Networks_PA", 157, 12, 0, NULL, 157, -1, -1, 6.49, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL , 0, -1, -1)
为要保留为空的列添加 NULL 或“”(空字符串)。
CURRENCY
是 Access SQL 中的保留字,因此如果您有一个具有该名称的列,则必须将其括在方括号中。也就是说,这将失败 "Syntax error in INSERT INTO statement." ...
crsr.execute("INSERT INTO Strategy (CURRENCY) VALUES ('USD')")
...但这会起作用:
crsr.execute("INSERT INTO Strategy ([CURRENCY]) VALUES ('USD')")