python - 在 MySQL 上使用列表创建列

标签 python mysql

我尝试使用列表在 MySQL 数据库上使用其名称来创建不同的列,但遇到了一些问题。

到目前为止我的代码是:

import pymysql

conn = pymysql.connect("localhost", "***", "***", "tutorial")

c = conn.cursor()

dicts = {'id': 5141, 'product_id': 193, 'price_ex_tax': '90.0000', 'wrapping_cost_tax': '0.0000', 'type': 'physical', 'ebay_item_id': '', 'option_set_id': 38, 'total_inc_tax': '198.0000', 'quantity': 2, 'price_inc_tax': '99.0000', 'cost_price_ex_tax': '0.0000', 'name': 'UQ Bachelor Graduation Gown Set', 'configurable_fields': [], 'base_cost_price': '0.0000', 'fixed_shipping_cost': '0.0000', 'wrapping_message': '', 'order_address_id': 964, 'total_ex_tax': '180.0000', 'refund_amount': '0.0000', 'event_name': None, 'cost_price_inc_tax': '0.0000', 'cost_price_tax': '0.0000', 'wrapping_cost_inc_tax': '0.0000', 'wrapping_name': '', 'price_tax': '9.0000', 'is_bundled_product ': False, 'ebay_transaction_id': '', 'bin_picking_number': '', 'parent_order_product_id': None, 'event_date': '', 'total_tax': '18.0000', 'wrapping_cost_ex_tax': '0.0000', 'base_total': '198.0000', 'product_options': [{'id': 4208, 'display_name': 'Gown size (based on height)', 'name': 'Bachelor gown size', 'display_value': 'L (175-182cm)', 'display_style': 'Pick list', 'type': 'Product list', 'option_id': 19, 'value': '77', 'product_option_id': 175, 'order_product_id': 5141}, {'id': 4209, 'display_name': 'Hood', 'name': 'H-QLD-BAC-STD', 'display_value': 'UQ Bachelor Hood', 'display_style': 'Pick list', 'type': 'Product list', 'option_id': 42, 'value': '119', 'product_option_id': 176, 'order_product_id': 5141}, {'id': 4210, 'display_name': 'Trencher size (based on head circumference)', 'name': 'Trencher size', 'display_value': 'M (53-54cm)', 'display_style': 'Pick list', 'type': 'Product list', 'option_id': 20, 'value': '81', 'product_option_id': 177, 'order_product_id': 5141}], 'base_price': '99.0000', 'sku': 'S-QLD-BAC-STD', 'return_id': 0, 'applied_discounts': [{'id': 'coupon', 'amount': 30}], 'quantity_shipped': 0, 'base_wrapping_cost': '0.0000', 'is_refunded': False, 'weight': '2.0000', 'order_id': 615496}

keys_from_dictionary = list(dicts.keys())

for x in keys_from_dictionary:
    query = "ALTER TABLE taula ADD %s VARCHAR(255)".format()
    c.execute(query, x)
    conn.commit()

 c.execute("SELECT * FROM taula")

 rows = c.fetchall()

 for eachRow in rows:
    print(eachRow)

print(keys_from_dictionary)

我收到此错误:1064,“您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本对应的手册,了解在第 1 行 ''fixed_shipping_cost' VARCHAR(255)' 附近使用的正确语法” )

问题是它添加了撇号,因此 SQL 查询失败。

我尝试将代码稍微更改为:

query = "ALTER TABLE taula ADD {} VARCHAR(255)".format()

但后来我得到:

IndexError:元组索引超出范围

如何运行此循环来为列表中的每个值创建一列?

编辑:

当我输入参数时出现错误:

回溯(最近一次调用最后一次): 文件“C:\Filipe\code\bigAnalysis\database\dbconnectex002.py”,第 13 行,位于 c.执行(查询,x) 文件“C:\Anaconda3\lib\site-packages\pymysql\cursors.py”,第 133 行,执行中 查询 = 查询 % self._escape_args(args, conn)

最佳答案

query = "ALTER TABLE taula ADD %s VARCHAR(255)".format(x)
c.execute(query,())

提示是预先完成整个sql的操作,并将一个空元组作为第二个参数传递给execute()。

关于python - 在 MySQL 上使用列表创建列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28423940/

相关文章:

mysql - Spring JDBC 模板 - 如何通过单个查询检索具有多个参数的多个结果

python - 检查变量是否在python中定义的简单方法?

python - DataFrame 列将字典列表存储为字符串 : Parse it and build a new dataframe

python - 如何在 QGraphicsView 中制作 2 层?

python - 如何将与正则表达式匹配的所有字符串放入 Python 列表中?

php - PDO 意外抛出具有多个绑定(bind)参数的绑定(bind)变量错误

Python Tkinter 用 StringVar 调用字典?

mysql - 如何在mysql中查找经理级别?

mysql - 序列化范围类型和解析器

mysql - cloud9-ide在mysql中导入数据库