# 插入行
> 原文: [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/)。
* * *
* * *
- 初級 Python
- python 入門
- 安裝 Python3
- 運行 python 程序
- 數據類型和變量
- Python 數字
- Python 字符串
- Python 列表
- Python 字典
- Python 元組
- 數據類型轉換
- Python 控制語句
- Python 函數
- Python 循環
- Python 數學函數
- Python 生成隨機數
- Python 文件處理
- Python 對象和類
- Python 運算符重載
- Python 繼承與多態
- Python 異常處理
- Python 模塊
- 高級 Python
- Python *args和**kwargs
- Python 生成器
- Python 正則表達式
- 使用 PIP 在 python 中安裝包
- Python virtualenv指南
- Python 遞歸函數
- __name__ == "__main__"是什么?
- Python Lambda 函數
- Python 字符串格式化
- Python 內置函數和方法
- Python abs()函數
- Python bin()函數
- Python id()函數
- Python map()函數
- Python zip()函數
- Python filter()函數
- Python reduce()函數
- Python sorted()函數
- Python enumerate()函數
- Python reversed()函數
- Python range()函數
- Python sum()函數
- Python max()函數
- Python min()函數
- Python eval()函數
- Python len()函數
- Python ord()函數
- Python chr()函數
- Python any()函數
- Python all()函數
- Python globals()函數
- Python locals()函數
- 數據庫訪問
- 安裝 Python MySQLdb
- 連接到數據庫
- MySQLdb 獲取結果
- 插入行
- 處理錯誤
- 使用fetchone()和fetchmany()獲取記錄
- 常見做法
- Python:如何讀取和寫入文件
- Python:如何讀取和寫入 CSV 文件
- 用 Python 讀寫 JSON
- 用 Python 轉儲對象