从Access .mdb表中插入选定的记录/行时,我在此代码块的第23行找到“Syntax error in INSERT INTO statement. (-3502)
”。
我想要发生的是
这个阻止成功执行的INSERT INTO语句中有什么语法错误?
import pyodbc
DBFile = r'C:\Python27\FCC_Processing\LMR Combined.mdb'
conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='+DBFile)
cursor = conn.cursor()
# Creates a table "65001" in the MDB that matches the schema of table "LMR_Combined"
string = "CREATE TABLE 65001(OBJECTID integer, Unique_ID varchar(255), LICENSEE_NAME varchar(255))"
cursor.execute(string)
# Selects 65000 records from table "LMR_Combined"
cursor.execute('select OBJECTID, Unique_ID, LICENSEE_NAME from LMR_Combined where OBJECTID > 0 and OBJECTID < 65001')
row = cursor.fetchone()
# For debugging, print a line
if row:
print row
# Inserts the 65000 rows into the new table "65001"
cursor.execute('insert OBJECTID, Unique_ID, LICENSEE_NAME into 65001')
conn.commit()
提前致谢
答案 0 :(得分:1)
你的语法错了。
在表格中插入数据的正确方法是:
insert into table1 (field1, field2, ...)
select ...
或者:
insert into table1(field1, field2, ...)
values (value1, value2, ...)
答案 1 :(得分:1)
考虑使用SELECT...INTO Statement创建和填充 65001 表是否足够:
SELECT OBJECTID, Unique_ID, LICENSEE_NAME
INTO [65001]
FROM LMR_Combined
WHERE OBJECTID > 0 AND OBJECTID < 65001;
如果您需要的数据类型与SELECT...INTO
不同,请执行CREATE TABLE
,然后执行此INSERT
:
INSERT INTO [65001] (OBJECTID, Unique_ID, LICENSEE_NAME)
SELECT OBJECTID, Unique_ID, LICENSEE_NAME
FROM LMR_Combined
WHERE OBJECTID > 0 AND OBJECTID < 65001;