質問

I have some code that links to Access and works fine with adodbapi, except for one niggling issue which I cant resolve. Basically I want to create a new table in Access with the Column Headings "Key" and "Value" but it doenst seem to work unless I include the commas which I dont want. I get the following error:

adodbapi.adodbapi.DatabaseError: (-2147352567, 'Exception occurred.', (0, u'Microsoft JET Database Engine', u'Syntax error in field definition.', None, 5003292, -2147217900), None)

import adodbapi

# create the DSN execution string and point it to the desired Database
database = 'D:\Temp.mdb' 
constr = 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=%s '  % database
conn = adodbapi.connect(constr)

# create a cursor
cur = conn.cursor()
# below doesnt work
cur.execute('CREATE TABLE OtherInfo(Key VARCHAR(100), Value VARCHAR(100))')  
# but this does
cur.execute('CREATE TABLE OtherInfo(Key2 VARCHAR(100), Value2 VARCHAR(100))')  
# so does this
cur.execute('CREATE TABLE OtherInfo('Key' VARCHAR(100), 'Value' VARCHAR(100))') 

# this also fails unless similar to above
cur.execute("INSERT INTO OtherInfo(Key,Value) VALUES('AppName','XXX')")

# close the cursor and connection
conn.commit()       # this saves all of the changes made above
cur.close()
conn.close()

How can I make it insert Column headings and Data as {Key, Value} without having to resort to 'Key' etc as the program which uses this table cannot reference other names?

Thanks for any help.

役に立ちましたか?

解決

Figured it out, it needs a [wrapper] to work as below:

cur.execute('CREATE TABLE OtherInfo([Key] VARCHAR(100), [Value] VARCHAR(100))') 

Thanks to anyone who took the trouble to view.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top