1 插入一条数据
- 连接postgresql数据库中的origindb数据库,用户为dn。
- 创建表
- 插入一条数据
import psycopg2
conn = psycopg2.connect(database="origindb", user="dn", password="000000", host="192.168.10.102", port="5432")
cursor = conn.cursor()
cursor.execute("""CREATE TABLE if not exists table_word(
url varchar(60) PRIMARY KEY NOT NULL,
title varchar(30) NOT NULL,
time varchar(20) ,
content text)""")
insert_sql = "INSERT INTO table_word \
values('http://blog.sina.com.cn/s/blog_4462623d0102ze34.html','心智与觉醒','2021-12-23 12:23:42','test') \
on conflict on constraint table_word_pkey\
do nothing;"
cursor.execute(insert_sql)
conn.commit()
cursor.execute("select * from table_word")
rows = cursor.fetchall()
for row in rows:
print(row)