<legend id='7SQWC'><style id='7SQWC'><dir id='7SQWC'><q id='7SQWC'></q></dir></style></legend>

    <small id='7SQWC'></small><noframes id='7SQWC'>

    1. <i id='7SQWC'><tr id='7SQWC'><dt id='7SQWC'><q id='7SQWC'><span id='7SQWC'><b id='7SQWC'><form id='7SQWC'><ins id='7SQWC'></ins><ul id='7SQWC'></ul><sub id='7SQWC'></sub></form><legend id='7SQWC'></legend><bdo id='7SQWC'><pre id='7SQWC'><center id='7SQWC'></center></pre></bdo></b><th id='7SQWC'></th></span></q></dt></tr></i><div id='7SQWC'><tfoot id='7SQWC'></tfoot><dl id='7SQWC'><fieldset id='7SQWC'></fieldset></dl></div>
      <tfoot id='7SQWC'></tfoot>

      • <bdo id='7SQWC'></bdo><ul id='7SQWC'></ul>

      将 Python dict 用于 SQL INSERT 语句

      Using a Python dict for a SQL INSERT statement(将 Python dict 用于 SQL INSERT 语句)
        <bdo id='sROCR'></bdo><ul id='sROCR'></ul>

          <small id='sROCR'></small><noframes id='sROCR'>

          <legend id='sROCR'><style id='sROCR'><dir id='sROCR'><q id='sROCR'></q></dir></style></legend><tfoot id='sROCR'></tfoot>

            • <i id='sROCR'><tr id='sROCR'><dt id='sROCR'><q id='sROCR'><span id='sROCR'><b id='sROCR'><form id='sROCR'><ins id='sROCR'></ins><ul id='sROCR'></ul><sub id='sROCR'></sub></form><legend id='sROCR'></legend><bdo id='sROCR'><pre id='sROCR'><center id='sROCR'></center></pre></bdo></b><th id='sROCR'></th></span></q></dt></tr></i><div id='sROCR'><tfoot id='sROCR'></tfoot><dl id='sROCR'><fieldset id='sROCR'></fieldset></dl></div>
                  <tbody id='sROCR'></tbody>

                本文介绍了将 Python dict 用于 SQL INSERT 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我正在尝试使用 dict 来执行 SQL INSERT.逻辑基本上是:

                I am trying to use a dict to do a SQL INSERT. The logic would basically be:

                INSERT INTO table (dict.keys()) VALUES dict.values()
                

                但是,我很难找出正确的语法/流程来执行此操作.这是我目前拥有的:

                However, I am having a tough time figuring out the correct syntax / flow to do this. This is what I currently have:

                # data = {...}
                sorted_column_headers_list = []
                sorted_column_values_list = []
                for k, v in data.items():
                    sorted_column_headers_list.append(k)
                    sorted_column_values_list.append(v)
                sorted_column_headers_string = ', '.join(sorted_column_headers_list)
                sorted_column_values_string = ', '.join(sorted_column_values_list)
                
                cursor.execute("""INSERT INTO title (%s) 
                            VALUES (%s)""", 
                            (sorted_column_headers_string, sorted_column_values_string))
                

                由此我得到一个 SQL 异常(我认为与逗号也包含在我拥有的某些值中的事实有关).执行上述操作的正确方法是什么?

                From this I get a SQL exception (I think related to the fact that commas are also included in some of the values that I have). What would be the correct way to do the above?

                推荐答案

                您想向查询添加参数占位符.这可能会为您提供所需的内容:

                You want to add parameter placeholders to the query. This might get you what you need:

                qmarks = ', '.join('?' * len(myDict))
                qry = "Insert Into Table (%s) Values (%s)" % (qmarks, qmarks)
                cursor.execute(qry, myDict.keys() + myDict.values())
                

                这篇关于将 Python dict 用于 SQL INSERT 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Can#39;t Create Entity Data Model - using MySql and EF6(无法创建实体数据模型 - 使用 MySql 和 EF6)
                MySQL select with CONCAT condition(MySQL选择与CONCAT条件)
                Capitalize first letter of each word, in existing table(将现有表格中每个单词的首字母大写)
                How to retrieve SQL result column value using column name in Python?(如何在 Python 中使用列名检索 SQL 结果列值?)
                Update row with data from another row in the same table(使用同一表中另一行的数据更新行)
                Exporting results of a Mysql query to excel?(将 Mysql 查询的结果导出到 excel?)

                <small id='4zaiV'></small><noframes id='4zaiV'>

                  • <legend id='4zaiV'><style id='4zaiV'><dir id='4zaiV'><q id='4zaiV'></q></dir></style></legend>
                    <i id='4zaiV'><tr id='4zaiV'><dt id='4zaiV'><q id='4zaiV'><span id='4zaiV'><b id='4zaiV'><form id='4zaiV'><ins id='4zaiV'></ins><ul id='4zaiV'></ul><sub id='4zaiV'></sub></form><legend id='4zaiV'></legend><bdo id='4zaiV'><pre id='4zaiV'><center id='4zaiV'></center></pre></bdo></b><th id='4zaiV'></th></span></q></dt></tr></i><div id='4zaiV'><tfoot id='4zaiV'></tfoot><dl id='4zaiV'><fieldset id='4zaiV'></fieldset></dl></div>

                      <tfoot id='4zaiV'></tfoot>
                      • <bdo id='4zaiV'></bdo><ul id='4zaiV'></ul>
                          <tbody id='4zaiV'></tbody>