Postgres和SQLite的等效连接
有没有一种和sqlite一样的方式,用Python连接到postgres数据库呢?
比如,在sqlite中,连接数据库的方式是用这行代码:conn = sqlite3.connect(curruser.dbname)
。那么,postgres的连接方式是什么样的呢?
1 个回答
6
你可以使用psycopg这个连接器,连接的写法会类似,不过你需要在连接字符串中提供更多的信息:
conn_string = "host='localhost' dbname='my_database' user='postgres' password='secret'"
conn = psycopg2.connect(conn_string)
这里有一些例子:如何在PostgreSQL中使用psycopg2