
如何在 postgresql 中导入数据
如果想要将类似于问题中给出的数据格式导入数据库,可以使用 postgresql。与 相比,postgresql 具有更好的性能和稳定性。
以下是如何使用 的 psycopg 驱动程序将数据导入 postgresql 中:
import psycopg2
# 建立数据库连接
conn = psycopg2.connect(
host="localhost",
port="5432",
database="my_database",
user="my_user",
password="my_password",
)
# 获取游标
cur = conn.cursor()
# 创建表
cur.execute("CREATE TABLE info (code VARCHAR(50), topic VARCHAR(50), author VARCHAR(50))")
# 准备 INSERT 语句
stmt = cur.prepare("INSERT INTO info VALUES (%s, %s, %s)")
# 执行 INSERT 语句
for code, topic, author in data:
stmt(code, topic, author)
# 提交更改
conn.commit()
# 关闭游标和连接
cur.close()
conn.close()
登录后复制
通过这种方式,你可以将数据成功导入到 postgresql 数据库中。
立即学习“”;
以上就是如何使用 Python 将数据导入 PostgreSQL 数据库?的详细内容,更多请关注米云网其它相关文章!
