2015-02-10 169 views
0

我试图使用列表来创建使用其在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服务器版本的使用权语法手册靠近“'fixed_shipping_cost'VARCHAR(255)'在第1行”)

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

我试图改变代码位:

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

但后来我得到:

IndexError:元组索引超出范围

我怎样才能运行这个循环创建列表中的每个值?

编辑:

我得到当我把参数错误:

回溯(最近通话最后一个): 文件 “C:\菲利佩\代码\ bigAnalysis \数据库\ dbconnectex002.py”第13行 c.execute(query,x) 执行 query = query%self._escape_args(args)文件“C:\ Anaconda3 \ lib \ site-packages \ pymysql \ cursors.py”,第133行,conn)

回答

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

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

+0

试过,以及我得到另一个错误。 – 2015-02-10 04:13:43

+0

它在MySQL控制台上执行得很好! – 2015-02-10 04:18:02

+0

@iamfbpt是否因为你有重复的列? – 2015-02-10 04:25:38