Python编程语言在实际使用中往往会遇到与数据库有关系的操作。那么今天,我们就会为大家系统的介绍两种比较常见的Python连接数据库的基本方法,希望朋友们可以从中学到更多的东西。

Python连接数据库之postgresql:

使用psycopg2来连接

示例代码:

import psycopg2  



conn = psycopg2.connect("dbname='dbname' user='username' 
host='localhost' password='password'")  




cur = conn.cursor()  



cur.execute("select * from dbtable")  


for row in cur:  


print row[0]  


conn.close() 

Python连接数据库之ms sql server:

使用pymssql来连接

示例代码: