python连接数据库

# -*- coding: utf-8 -*-

import MySQLdb
conn = MySQLdb.connect("localhost","root","123456","test1" )
cur = conn.cursor()      #建立游标
sql = "select * from student"
insert_sql="insert into student (id,name) VALUES (6,'wanghaowanghao')"
cur.execute(insert_sql)

conn.commit() #提交   防止数据不一致

cur.execute(sql)

result = cur.fetchall()   #保存结果
for row in result:
    print row[0]
    print row[1]

conn.close()   # 关闭数据库

 

posted @ 2017-01-23 11:35  webbky  阅读(166)  评论(0)    收藏  举报