
- Python PostgreSQL 教程
- 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 PostgreSQL 有用資源
- Python PostgreSQL - 快速指南
- Python PostgreSQL - 有用資源
- Python PostgreSQL - 討論
Python PostgreSQL - 插入資料
您可以使用INSERT INTO語句將記錄插入PostgreSQL中的現有表中。執行此操作時,您需要指定表的名稱以及其中列的值。
語法
以下是INSERT語句的推薦語法:
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN) VALUES (value1, value2, value3,...valueN);
其中,column1、column2、column3...是表中列的名稱,value1、value2、value3...是要插入表中的值。
示例
假設我們使用CREATE TABLE語句建立了一個名為CRICKETERS的表,如下所示:
postgres=# CREATE TABLE CRICKETERS ( First_Name VARCHAR(255), Last_Name VARCHAR(255), Age INT, Place_Of_Birth VARCHAR(255), Country VARCHAR(255) ); CREATE TABLE postgres=#
以下PostgreSQL語句在上面建立的表中插入一行:
postgres=# insert into CRICKETERS (First_Name, Last_Name, Age, Place_Of_Birth, Country) values ('Shikhar', 'Dhawan', 33, 'Delhi', 'India'); INSERT 0 1 postgres=#
在使用INSERT INTO語句插入記錄時,如果跳過任何列名,則將插入記錄,並在跳過的列處留下空空格。
postgres=# insert into CRICKETERS (First_Name, Last_Name, Country) values('Jonathan', 'Trott', 'SouthAfrica'); INSERT 0 1
如果傳遞的值的順序與表中各自列名的順序相同,則您也可以在不指定列名的情況下將記錄插入表中。
postgres=# insert into CRICKETERS values('Kumara', 'Sangakkara', 41, 'Matale', 'Srilanka'); INSERT 0 1 postgres=# insert into CRICKETERS values('Virat', 'Kohli', 30, 'Delhi', 'India'); INSERT 0 1 postgres=# insert into CRICKETERS values('Rohit', 'Sharma', 32, 'Nagpur', 'India'); INSERT 0 1 postgres=#
將記錄插入表後,您可以使用SELECT語句驗證其內容,如下所示:
postgres=# SELECT * from CRICKETERS; first_name | last_name | age | place_of_birth | country ------------+------------+-----+----------------+------------- Shikhar | Dhawan | 33 | Delhi | India Jonathan | Trott | | | SouthAfrica Kumara | Sangakkara | 41 | Matale | Srilanka Virat | Kohli | 30 | Delhi | India Rohit | Sharma | 32 | Nagpur | India (5 rows)
使用Python插入資料
psycopg2的遊標類提供了一個名為execute()的方法。此方法接受查詢作為引數並執行它。
因此,要使用python將資料插入PostgreSQL中的表中,請執行以下操作:
匯入psycopg2包。
使用connect()方法建立一個連線物件,透過將使用者名稱、密碼、主機(可選,預設值:localhost)和資料庫(可選)作為引數傳遞給它。
透過將false作為值設定為autocommit屬性來關閉自動提交模式。
psycopg2庫的Connection類的cursor()方法返回一個遊標物件。使用此方法建立一個遊標物件。
然後,透過將INSERT語句作為引數傳遞給execute()方法來執行它/它們。
示例
以下Python程式在PostgreSQL資料庫中建立一個名為EMPLOYEE的表,並使用execute()方法向其中插入記錄:
import psycopg2 #Establishing the connection conn = psycopg2.connect( database="mydb", user='postgres', password='password', host='127.0.0.1', port= '5432' ) #Setting auto commit false conn.autocommit = True #Creating a cursor object using the cursor() method cursor = conn.cursor() # Preparing SQL queries to INSERT a record into the database. cursor.execute('''INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Ramya', 'Rama priya', 27, 'F', 9000)''') cursor.execute('''INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Vinay', 'Battacharya', 20, 'M', 6000)''') cursor.execute('''INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Sharukh', 'Sheik', 25, 'M', 8300)''') cursor.execute('''INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Sarmista', 'Sharma', 26, 'F', 10000)''') cursor.execute('''INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Tripthi', 'Mishra', 24, 'F', 6000)''') # Commit your changes in the database conn.commit() print("Records inserted........") # Closing the connection conn.close()
輸出
Records inserted........
廣告