python连接数据库
postgresql:
使用psycopg2来连接
示例代码:
1 import psycopg2
2
3 conn = psycopg2.connect("dbname='dbname' user='username' host='localhost' password='password'")
4 cur = conn.cursor()
5 cur.execute("select * from dbtable")
6 for row in cur:
7 print row[0]
8 conn.close()
2
3 conn = psycopg2.connect("dbname='dbname' user='username' host='localhost' password='password'")
4 cur = conn.cursor()
5 cur.execute("select * from dbtable")
6 for row in cur:
7 print row[0]
8 conn.close()
ms sql server:
使用pymssql来连接
示例代码:
1 import psmssql
2
3 conn = psmssql.connect(host='yourhost', user='loginname', password='password', database='dbname', charset='utf8')
4 cur = conn.cursor()
5 cur.execute('select * from dbtable')
6
7 for row in cur:
8 print row[0]
9
10 conn.close()
2
3 conn = psmssql.connect(host='yourhost', user='loginname', password='password', database='dbname', charset='utf8')
4 cur = conn.cursor()
5 cur.execute('select * from dbtable')
6
7 for row in cur:
8 print row[0]
9
10 conn.close()