[Advanced Python] Python Code to DB by SQLAlchemy
Flask 调用
Ref: [Advanced Python] Flask model to DB by SQLAlchemy
再看一个纯Python调用SQLAlchemy的方案。
psycopg2 模块
直接调用 SQL 的方法,而不是 ORM。
import psycopg2 # database,user,password,host,port分别对应要连接的PostgreSQL数据库的数据库名、数据库用户名、用户密码、主机、端口信息,请根据具体情况自行修改 conn = psycopg2.connect(database="test",user="postgres",password="postgres",host="127.0.0.1",port="5432") cur = conn.cursor() # 执行查询命令 cur.execute("SELECT name,setting FROM pg_settings")
sqlalchemy链接postgresql数据库
Ref: python使用sqlalchemy连接postgresql数据库