使用pandas获取PostgreSQL数据库中的数据
程序员文章站
2024-03-21 21:03:58
...
方法一:
import psycopg2
import pandas as pd
from sqlalchemy import create_engine
#初始化数据库连接,使用pymysql模块
#postgresql的用户:root,
#password:你的密码,
#端口:5432,
#数据库:trust
engine = create_engine(“postgresql+psycopg2://**root**:**password**@**localhost**:**3306/trust**”,encoding=‘utf-8’)
#查询语句,选出testexcel表中的所有数据
sql = “”“select * from testexcel”""
#read_sql_query的两个参数: sql语句, 数据库连接
df = pd.read_sql_query(sql,con=engine)
方法二:
import psycopg2
import pandas as pd
# postgres config
postgres_host = "" # 数据库地址
postgres_port = "5432" # 数据库端口
postgres_user = "" # 数据库用户名
postgres_password = "" # 数据库密码
postgres_datebase = "" # 数据库名字
postgres_table1 = "" #数据库中的表的名字
# connection string
conn_string = "host=" + postgres_host + " port=" + postgres_port + " dbname=" + postgres_datebase +
" user=" + postgres_user + " password=" + postgres_password
conn = psycopg2.connect(conn_string)
sql_command1 = "select * from test"
try:
data1 = pd.read_sql(sql_command1, conn)
except:
print("load data from postgres failure !")
exit()
上一篇: mysql去重面试总结
推荐阅读
-
SQLSERVER获取数据库中的所有表的名称、所有字段的名称
-
使用pandas获取PostgreSQL数据库中的数据
-
使用poi将数据库数据表中的数据导出成excel
-
使用Java将数据库的数据导出至Excel表中
-
MySQL 获取数据库中的所有表名和列名
-
web中使用阿里巴巴的EasyExcel导出数据库中的数据
-
SQL使用技巧-两个表比对数据(Excel表中存在而数据库表中不存在的数据)
-
Spring Security使用数据库中的用户进行身份认证
-
mybatis框架中,使用databaseIdProvider来配置支持多数据库的支持
-
JAVA 向数据库中插入数据并获取主键的方法(逐渐补充)