#!/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()
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-03 10:32:26