Python PostgreSQL - Insert 插入数据
您可以使用
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 语句创建了一个名为 Basketball 的表,如下所示 −
postgres=# CREATE TABLE Basketball (
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 Basketball
(First_Name, Last_Name, Age, Place_Of_Birth, Country) values
('Shikhar', 'Dhawan', 33, 'Delhi', 'India');
INSERT 0 1
postgres=#
在使用 INSERT INTO 语句插入记录时,如果您跳过任何列名称,记录将被插入,并在您跳过的列处留下空白。
postgres=# insert into Basketball
(First_Name, Last_Name, Country) values('Jonathan', 'Trott', 'SouthAfrica');
INSERT 0 1
如果您传递的值的顺序与其在表中各自的列名相同,您也可以在不指定列名的情况下将记录插入表中。
postgres=# insert into Basketball values('Kumara', 'Sangakkara', 41, 'Matale', 'Srilanka');
INSERT 0 1
postgres=# insert into Basketball values('Virat', 'Kohli', 30, 'Delhi', 'India');
INSERT 0 1
postgres=# insert into Basketball values('Rohit', 'Sharma', 32, 'Nagpur', 'India');
INSERT 0 1
postgres=#
将记录插入表后,您可以使用 SELECT 语句验证其内容,如下所示 −
postgres=# SELECT * from Basketball;
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
包。
通过将用户名、密码、主机(可选默认值:localhost)和数据库(可选)作为参数传递给它,使用
connect()
方法创建连接对象。
通过将 false 设置为属性
autocommit
的值来关闭自动提交模式。
psycopg2 库的
Connection
类的
cursor()
方法返回一个游标对象。 使用此方法创建游标对象。。
然后,通过将 INSERT 语句作为参数传递给 execute() 方法来执行 INSERT 语句。
以下 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........