【问题标题】:Update query in Python using Dictionary key Value pairs使用字典键值对在 Python 中更新查询
【发布时间】:2020-07-22 07:08:24
【问题描述】:

我刚开始学习 python,我的要求是基于 Oracle 表中的一列,我使用字典键对值更新其他列。我的字典看起来像这样

{'United States': 'American', 'France': 'European', 'Great Britain': 'European'}

所以当国家列是美国时,描述列应该更新为美国,在我的数据库连接之后,这是我的代码

query = "select country from details_table "
        cursor.execute(query)
        returnrows = cursor.fetchone()
        while returnrows is not None:
                pickedvalue=returnrows[0]

               mainvalue=file_dictionary[pickedvalue]


                updatequery = "update details_table set description='%s' where country='%s'"

                cursor.execute(updatequery %(mainvalue,pickedvalue))
                returnrows = cursor.fetchone()

当我执行此操作时,我得到错误“不是查询”,所以我尝试更改引号,每次都会得到不同的错误

updatequery = "update details_table set description=%s where country=%s"
updatequery = "update details_table set description='%s' where country=%s"

对于以上两个查询,我得到 ORA-00933: SQL command not properly end

updatequery = "update details_table set description=%s where country='%s'"

为此我得到了 ORA-00904: "AMERICAN": invalid identifier

有人能告诉我哪个是正确的查询吗?我什至像下面这样尝试但没有运气

updatequery="update details_table set description={} where country='{}'"
sql=updatequery.format(main_value,pickedvalue)
cursor.execute(sql)

【问题讨论】:

    标签: python-3.x oracle dictionary cx-oracle


    【解决方案1】:

    不要尝试使用字符串插值来更新您的查询!相反,请使用documentation 中所述的绑定变量。

    您的示例将如下所示:

    query = "select country from details_table"
    cursor.execute(query)
    returnrows = cursor.fetchall()
    for rows in returnrows:
        for pickedvalue in rows:
            requiredvalue = file_dictionary[pickedvalue]
    
            print(requiredvalue)
            updatequery = "update details_table set description=:1 where country=:2"
            cursor.execute(updatequery, [requiredvalue, pickedvalue])
    
            connection.commit()
    

    另一种使用游标迭代和元组解包的方法如下:

    query = "select country from details_table"
    cursor.execute(query)
    updateCursor = connection.cursor()
    for country, in cursor:
        requiredvalue = file_dictionary[country]
    
        print(requiredvalue)
        updatequery = "update details_table set description=:1 where country=:2"
        updateCursor.execute(updatequery, [requiredvalue, pickedvalue])
    
        connection.commit()
    

    【讨论】:

    • 谢谢 Anthony,它工作得很好,你能帮我看看我使用的 while 循环和 fetchone() 有什么问题(在问题中提到)
    • 不客气。我没有看到另一个问题?你能澄清一下吗?
    • 我只想使用 fetchone() 和 while 循环来完成这项工作
    • 我在这里的理解是它不允许在 while 循环中使用 fetchone(),因为 fetchone 方法需要在它之前进行选择查询,如果我们在更新查询之后再次定义选择查询,它会再次循环到第一条记录...但我想知道我们是否可以做到这一点..
    • 我添加了另一种方法——使用光标迭代。您也可以使用 cursor.fetchone(),但光标迭代更方便!请注意,如果您在游标上进行迭代,则需要使用不同的游标来执行更新。
    【解决方案2】:

    我不知道什么是魔法,它开始使用这个,我无法理解这个和上面代码之间的逻辑区别是什么,请告诉我

     query = "select country from details_table"
            cursor.execute(query)
            returnrows = cursor.fetchall()
            for rows in returnrows:
                for pickedvalue in rows:
                    requiredvalue = file_dictionary[pickedvalue]
    
                    print(requiredvalue)
                    updatequery = "update details_table set description='%s' where country='%s'"
                    cursor.execute(updatequery % (requiredvalue, pickedvalue))
    
            connection.commit()
    
    

    【讨论】:

      猜你喜欢
      • 2020-05-03
      • 2016-02-20
      • 2021-08-23
      • 2021-10-28
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      相关资源
      最近更新 更多