<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 插入行 > 原文: [https://thepythonguru.com/inserting-rows/](https://thepythonguru.com/inserting-rows/) * * * 于 2020 年 1 月 7 日更新 * * * Insert 語句用于在 mysql 中插入記錄。 **語法**: `INSERT INTO <some table> (<some column names>) VALUES("<some values>");` **示例 1**: ```py from __future__ import print_function import MySQLdb as my db = my.connect(host="127.0.0.1", user="root", passwd="", db="world" ) cursor = db.cursor() sql = "insert into city VALUES(null, 'Mars City', 'MAC', 'MARC', 1233)" number_of_rows = cursor.execute(sql) db.commit() # you need to call commit() method to save # your changes to the database db.close() ``` 該程序在城市表中插入一個新城市,注意對`db.commit()`的使用,該方法將您的更改保存到數據庫中。 **示例 2**: ```py from __future__ import print_function import MySQLdb as my db = my.connect(host="127.0.0.1", user="root", passwd="", db="world" ) cursor = db.cursor() name = "Some new city" country_code = 'PSE' district = 'Someyork' population = 10008 sql = "insert into city VALUES(null, '%s', '%s', '%s', %d)" % \ (name, country_code , district, population) number_of_rows = cursor.execute(sql) db.commit() db.close() ``` 請注意,在第 18 行中使用了反斜杠(`\`)字符。`\`字符用于將 python 語句拆分為多行。 ## 插入多行 * * * 要在表中插入多行,請使用游標對象的`executemany()`方法。 **語法**: `cursor_object.executemany(statement, arguments)` **`statement`**:包含要執行的查詢的字符串。 **`arguments`**:一個包含要在`insert`語句中使用的值的序列。 讓我們舉個例子。 ```py from __future__ import print_function import MySQLdb as my db = my.connect(host="127.0.0.1", user="root", passwd="", db="world" ) cursor = db.cursor() name = "Some new city" country_code = 'SNC' district = 'Someyork' population = 10008 data = [ ('city 1', 'MAC', 'distrct 1', 16822), ('city 2', 'PSE', 'distrct 2', 15642), ('city 3', 'ZWE', 'distrct 3', 11642), ('city 4', 'USA', 'distrct 4', 14612), ('city 5', 'USA', 'distrct 5', 17672), ] sql = "insert into city(name, countrycode, district, population) VALUES(%s, %s, %s, %s)" number_of_rows = cursor.executemany(sql, data) db.commit() db.close() ``` 在下一篇文章中,我們討論[如何處理錯誤](/handling-errors/)。 * * * * * *
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看