
- Python 資料訪問教程
- Python 資料訪問 - 首頁
- Python MySQL
- Python MySQL - 簡介
- Python MySQL - 資料庫連線
- Python MySQL - 建立資料庫
- Python MySQL - 建立表
- Python MySQL - 插入資料
- Python MySQL - 選擇資料
- Python MySQL - Where 子句
- Python MySQL - Order By
- Python MySQL - 更新表
- Python MySQL - 刪除資料
- Python MySQL - 刪除表
- Python MySQL - Limit
- Python MySQL - Join
- Python MySQL - 遊標物件
- Python PostgreSQL
- Python PostgreSQL - 簡介
- Python PostgreSQL - 資料庫連線
- Python PostgreSQL - 建立資料庫
- Python PostgreSQL - 建立表
- Python PostgreSQL - 插入資料
- Python PostgreSQL - 選擇資料
- Python PostgreSQL - Where 子句
- Python PostgreSQL - Order By
- Python PostgreSQL - 更新表
- Python PostgreSQL - 刪除資料
- Python PostgreSQL - 刪除表
- Python PostgreSQL - Limit
- Python PostgreSQL - Join
- Python PostgreSQL - 遊標物件
- Python SQLite
- Python SQLite - 簡介
- Python SQLite - 建立連線
- Python SQLite - 建立表
- Python SQLite - 插入資料
- Python SQLite - 選擇資料
- Python SQLite - Where 子句
- Python SQLite - Order By
- Python SQLite - 更新表
- Python SQLite - 刪除資料
- Python SQLite - 刪除表
- Python SQLite - Limit
- Python SQLite - Join
- Python SQLite - 遊標物件
- Python MongoDB
- Python MongoDB - 簡介
- Python MongoDB - 建立資料庫
- Python MongoDB - 建立集合
- Python MongoDB - 插入文件
- Python MongoDB - 查詢
- Python MongoDB - 查詢
- Python MongoDB - 排序
- Python MongoDB - 刪除文件
- Python MongoDB - 刪除集合
- Python MongoDB - 更新
- Python MongoDB - Limit
- Python 資料訪問資源
- Python 資料訪問 - 快速指南
- Python 資料訪問 - 有用資源
- Python 資料訪問 - 討論
Python MySQL - 插入資料
您可以使用 **INSERT INTO** 語句向 MySQL 中現有表的新增新行。在其中,您需要指定表名、列名和值(按列名的相同順序)。
語法
以下是 MySQL INSERT INTO 語句的語法。
INSERT INTO TABLE_NAME (column1, column2,column3,...columnN) VALUES (value1, value2, value3,...valueN);
示例
以下查詢將一條記錄插入名為 EMPLOYEE 的表中。
INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES (' Mac', 'Mohan', 20, 'M', 2000 );
您可以使用 SELECT 語句驗證插入操作後的表記錄,如下所示:
mysql> select * from Employee; +------------+-----------+------+------+--------+ | FIRST_NAME | LAST_NAME | AGE | SEX | INCOME | +------------+-----------+------+------+--------+ | Mac | Mohan | 20 | M | 2000 | +------------+-----------+------+------+--------+ 1 row in set (0.00 sec)
並非總是必須指定列名,如果按表的列順序傳遞記錄的值,則可以不帶列名執行 SELECT 語句,如下所示:
INSERT INTO EMPLOYEE VALUES ('Mac', 'Mohan', 20, 'M', 2000);
使用 python 在 MySQL 表中插入資料
**execute()** 方法(在遊標物件上呼叫)接受查詢作為引數並執行給定的查詢。要插入資料,您需要將 MySQL INSERT 語句作為引數傳遞給它。
cursor.execute("""INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Mac', 'Mohan', 20, 'M', 2000)""")
要在 MySQL 中使用 python 將資料插入表中,請執行以下操作:
匯入 **mysql.connector** 包。
使用 **mysql.connector.connect()** 方法建立一個連線物件,將使用者名稱、密碼、主機(可選,預設:localhost)和資料庫(可選)作為引數傳遞給它。
透過在上面建立的連線物件上呼叫 **cursor()** 方法來建立一個遊標物件
然後,透過將其作為引數傳遞給 **execute()** 方法來執行 **INSERT** 語句。
示例
以下示例執行 SQL INSERT 語句,將一條記錄插入 EMPLOYEE 表中:
import mysql.connector #establishing the connection conn = mysql.connector.connect( user='root', password='password', host='127.0.0.1', database='mydb') #Creating a cursor object using the cursor() method cursor = conn.cursor() # Preparing SQL query to INSERT a record into the database. sql = """INSERT INTO EMPLOYEE( FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Mac', 'Mohan', 20, 'M', 2000)""" try: # Executing the SQL command cursor.execute(sql) # Commit your changes in the database conn.commit() except: # Rolling back in case of error conn.rollback() # Closing the connection conn.close()
動態插入值
您還可以使用“%s”代替 MySQL 的 **INSERT** 查詢中的值,並將值作為列表傳遞,如下所示:
cursor.execute("""INSERT INTO EMPLOYEE VALUES ('Mac', 'Mohan', 20, 'M', 2000)""", ('Ramya', 'Ramapriya', 25, 'F', 5000))
示例
以下示例將一條記錄動態插入 Employee 表中。
import mysql.connector #establishing the connection conn = mysql.connector.connect( user='root', password='password', host='127.0.0.1', database='mydb') #Creating a cursor object using the cursor() method cursor = conn.cursor() # Preparing SQL query to INSERT a record into the database. insert_stmt = ( "INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME)" "VALUES (%s, %s, %s, %s, %s)" ) data = ('Ramya', 'Ramapriya', 25, 'F', 5000) try: # Executing the SQL command cursor.execute(insert_stmt, data) # Commit your changes in the database conn.commit() except: # Rolling back in case of error conn.rollback() print("Data inserted") # Closing the connection conn.close()
輸出
Data inserted
廣告