【问题标题】:executing native SQL to Mysql in Python [duplicate]在Python中执行本机SQL到Mysql [重复]
【发布时间】:2019-04-29 18:52:18
【问题描述】:

我想在mysql数据库中执行这条语句。但我无法获得正确的格式。这 ?正在替换表名而不是值。我也尝试将字符串连接在一起,但结果相同。

尝试执行与 Mysql 工作台相同的插入,没有问题。但是还是不行

mysql.connector.errors.DataError: 1136 (21S01): Column count doesn't match value count at row 1

import mysql.connector    
for table in tables:
    sql = """insert into close_price
    select * from nn_indexes_20180105
    where identifier = 'OMXS30'
    and seconds_offs = (select max(seconds_offs) as seconds_offs 
    from nn_indexes_20180105 
    where seconds_offs <= 55800
    and identifier = 'OMXS30');"""

【问题讨论】:

    标签: python mysql


    【解决方案1】:

    您只能绑定这样的值,而不是对象名称。在这种情况下,您将不得不求助于某种字符串操作,例如格式化:

    for table in tables:
        sql = """insert into close_price select * from {} where identifier = 'OMXS30' and seconds_offs = (select max(seconds_offs)
        from {} where seconds_offs <= 55800 and identifier = 'OMXS30'""".format(table[0], table[0])
        cursor = mySQLconnection.cursor()
        cursor.execute(sql)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-03
      • 2015-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 2013-03-27
      相关资源
      最近更新 更多