您的位置:首页 > 编程语言 > Python开发

python 连接数据库

2013-06-17 00:00 330 查看
#!/usr/bin/python
import MySQLdb

try:
conn=MySQLdb.connect(host='localhost',user='root',passwd='cdyanfa',port=3306)
cur=conn.cursor()
conn.select_db('nova')

count=cur.execute('select * from instances')
print 'there has %s rows record' % count

cur.execute('SELECT uuid,hostname FROM instances where deleted = 0')
print "Rows selected:", cur.rowcount

for row in cur.fetchall():
print "note : ", row[0], row[1]

conn.commit()
cur.close()
conn.close()

except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 数据库