INSERT值中的SELECT错误

时间:2017-12-20 00:18:02

标签: mysql python-2.7 mysql-python xlrd

我试图将数据插入表中。数据可以是CSV,也可以是基于CSV值的其他表格。

我收到错误:

  

不受支持的格式字符' R' (0x52)在索引64

代码:

def Test():
path = 'USA DIRECT.xls'
workbook = xlrd.open_workbook(path, on_demand = True)
sheet = workbook.sheet_by_index(0)
for rowx in xrange(1, sheet.nrows):
    try:
        Id = ''
        Year = datetime.now().strftime('%Y')
        Month = datetime.now().strftime('%m')
        Partner = 'USA Direct'
        PartnerType = 'Direct'
        Reseller = sheet.row_values(rowx, start_colx=12, end_colx=None)[0]
        Model = sheet.row_values(rowx, start_colx=2, end_colx=None)[0]
        InvDate = sheet.row_values(rowx, start_colx=11, end_colx=None)[0]
        CustomerNumber = ''
        Zip = sheet.row_values(rowx, start_colx=22, end_colx=None)[0]
        Quantity = sheet.row_values(rowx, start_colx=7, end_colx=None)[0]
        Total = sheet.row_values(rowx, start_colx=25, end_colx=None)[0]
        UnitPrice = sheet.row_values(rowx, start_colx=24, end_colx=None)[0]

        cursor.execute("INSERT INTO sales_olap (id, year, month, reporting_partner, reporting_partner_type, reseller_name, reseller_type, reseller_state, part_number, part_category, product_family, invoice_date, reseller_customer_number, reseller_zip_code, units, extended_cost, unit_cost) VALUES('%s', '%s', '%s','%s', '%s', '%s', (SELECT reseller_type FROM resellers WHERE reseller='%s' % Reseller), (SELECT reseller_state FROM resellers WHERE reseller='%s' % Reseller),'%s', (SELECT category FROM models WHERE model='%s' % Model), (SELECT family FROM models WHERE model='%s' % Model), '%s', '%s', '%s', '%s', '%s', '%s')" %(Id, Year, Month, Partner, PartnerType, Reseller, Model, InvDate, CustomerNumber, Zip, Quantity, Total, UnitPrice))
    except Exception, e:
        print (e)
workbook.release_resources()

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

这是您的查询中出现字母R(0x52)的情况。

SELECT reseller_type FROM resellers WHERE reseller='%s' % Reseller

看着它,你需要用另一个%前缀%来逃避它,因为它没有用引号括起来,即

SELECT reseller_type FROM resellers WHERE reseller='%s' %% Reseller

也适用于其他内部选择