如何使用PyoDBC将变量作为列名传递?

发布于 2025-02-03 06:23:42 字数 1446 浏览 3 评论 0原文

我有一个有两个电话号码的列表,我想将每个电话号码放入访问数据库中的自己的列中。列名为phone_number1和phone_number2。如何将其传递给插入语句?

phone_numbers = ['###.218.####', '###.746.####']

driver = '{Microsoft Access Driver (*.mdb, *.accdb)}'
filepath = 'C:/Users/Notebook/Documents/master.accdb'
myDataSources = pyodbc.dataSources()
access_driver = myDataSources['MS Access Database']

conn = pyodbc.connect(driver=driver, dbq=filepath)
cursor = conn.cursor()

phone_number_count = 1
for phone_number in phone_numbers:
    column_name = "Phone_Number" + str(phone_number_count)
    cursor.execute("INSERT INTO Business_Cards (column_name) VALUES (?)", (phone_number))

conn.commit()
print("Your database has been updated.")

这就是我到目前为止的。

Traceback (most recent call last):
  File "C:/Users/Notebook/PycharmProjects/Jarvis/BusinessCard.py", line 55, in <module>
    database_entry(phone_numbers, emails, name, title)
  File "C:/Users/Notebook/PycharmProjects/Jarvis/BusinessCard.py", line 47, in database_entry
    cursor.execute("INSERT INTO Business_Cards (column_name) VALUES (?)", (phone_number))
pyodbc.Error: ('HYS22', "[HYS22] [Microsoft][ODBC Microsoft Access Driver] The INSERT INTO statement contains the following unknown field name: 'column_name'. Make sure you have typed the name correctly, and try the operation again. (-1507) (SQLExecDirectW)")

I have a list that has two phone numbers and I'd like to put each phone number into its own column in an Access database. The column names are Phone_Number1 and Phone_Number2. How do I pass that to the INSERT statement?

phone_numbers = ['###.218.####', '###.746.####']

driver = '{Microsoft Access Driver (*.mdb, *.accdb)}'
filepath = 'C:/Users/Notebook/Documents/master.accdb'
myDataSources = pyodbc.dataSources()
access_driver = myDataSources['MS Access Database']

conn = pyodbc.connect(driver=driver, dbq=filepath)
cursor = conn.cursor()

phone_number_count = 1
for phone_number in phone_numbers:
    column_name = "Phone_Number" + str(phone_number_count)
    cursor.execute("INSERT INTO Business_Cards (column_name) VALUES (?)", (phone_number))

conn.commit()
print("Your database has been updated.")

This is what I have so far.

Traceback (most recent call last):
  File "C:/Users/Notebook/PycharmProjects/Jarvis/BusinessCard.py", line 55, in <module>
    database_entry(phone_numbers, emails, name, title)
  File "C:/Users/Notebook/PycharmProjects/Jarvis/BusinessCard.py", line 47, in database_entry
    cursor.execute("INSERT INTO Business_Cards (column_name) VALUES (?)", (phone_number))
pyodbc.Error: ('HYS22', "[HYS22] [Microsoft][ODBC Microsoft Access Driver] The INSERT INTO statement contains the following unknown field name: 'column_name'. Make sure you have typed the name correctly, and try the operation again. (-1507) (SQLExecDirectW)")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心头的小情儿 2025-02-10 06:23:42

如果要在同一行中插入两个数字,请删除for循环,然后调整insert以考虑两列:

phone_numbers = ['###.218.####', '###.746.####']
# ...
column_names = [f"PhoneNumber{i}" for i in range(1, len(phone_numbers) + 1)]
placeholders = ['?'] * len(phone_numbers)
cursor.execute(f"INSERT INTO Business_Cards ({', '.join(column_names)}) VALUES ({', '.join(placeholders)})", tuple(phone_numbers))

conn.commit()
# ...

If you want to insert both numbers in the same row, remove the for loop and adjust the INSERT to consider the two columns:

phone_numbers = ['###.218.####', '###.746.####']
# ...
column_names = [f"PhoneNumber{i}" for i in range(1, len(phone_numbers) + 1)]
placeholders = ['?'] * len(phone_numbers)
cursor.execute(f"INSERT INTO Business_Cards ({', '.join(column_names)}) VALUES ({', '.join(placeholders)})", tuple(phone_numbers))

conn.commit()
# ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文