Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用列表在MySQL上创建列_Python_Mysql - Fatal编程技术网

Python 使用列表在MySQL上创建列

Python 使用列表在MySQL上创建列,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',

我试图使用一个列表在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()
但我得到:

索引器错误:元组索引超出范围

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

编辑:

输入参数时出现的错误:

回溯(最近一次呼叫最后一次): 文件“C:\Filipe\code\bigAnalysis\database\dbconnectex002.py”,第13行,在 c、 执行(查询,x) 文件“C:\Anaconda3\lib\site packages\pymysql\cursors.py”,执行中的第133行 查询=查询%self.\u转义\u参数(参数,conn)


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

它在mysql控制台上执行得很好@iamfbpt是因为您有重复的列吗?不是。问题是格式化的字符串在查询中添加了撇号(“”),因此,查询失败。我需要能够只传递列表中每个位置的值,例如:我需要传递的不是“id”:id@iamfbpt对不起,我把它和sqlite弄错了。但这里是代码。完成对整个sql的操作并不再使用参数执行。始终尝试用您的答案提供解释,以提高您文章的质量。
query = "ALTER TABLE taula ADD %s VARCHAR(255)".format(x)
c.execute(query,())