Python3 pymysql连接MySQL数据库 不指定

jed , 2016-5-17 22:30 , 服务器技术 , 评论(0) , 阅读(15222) , Via 本站原创 | |

#!/usr/bin/python
# -*- coding:utf8 -*-

import pymysql

#取得数据库连接对象
conn = pymysql.connect(host=‘127.0.0.1‘,port=3306,user=‘root‘,passwd=‘1234‘,db=‘python‘)
#取得游标对象
cur = conn.cursor()

#插入数据
cur.execute("INSERT INTO student(name,sex,age) VALUES(‘3‘, ‘0‘, ‘45‘)")
conn.commit()
#修改数据
cur.execute("UPDATE student SET age = 90 WHERE id = 2" )
conn.commit()
#删除数据
cur.execute("DELETE FROM student WHERE name = ‘3‘")
conn.commit()

###########################################
#    数据发生改变时一定要conn.commit()    #
###########################################

#查询数据
cur.execute(‘SELECT *FROM student‘)
for r in cur.fetchall():
    print(r)

cur.close()
conn.close()
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]