2015-02-10 179 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