美文网首页
Python操纵mysql数据库基础

Python操纵mysql数据库基础

作者: 猛犸象和剑齿虎 | 来源:发表于2019-06-03 10:46 被阅读0次

pymysql安装。

pip install pymysql在黑屏终端中输入这句。 image.png

链接数据库

  1. 首先要知道数据库地址,端口号3306,用户名,密码,数据库名,有了这些东西才能连接数据库。python没有办法帮我们创建数据库。
import pymysql
### 打开数据库的链接
db=pymysql.Connect(host="localhost",port=3306,user="test",passwd="123321",db="stu",charset="utf8")
print(db)

返回结果:说明我们创建成功了链接。

<pymysql.connections.Connection object at 0x0000000002547E80>

创建游标对象,游标对象是指用来操纵sql语句执行的操作。

cursor=db.cursor()

操纵数据库的增删改查

  1. 创建表
    完整代码:
import pymysql
# 打开数据库的链接
db = pymysql.Connect(host="localhost",port=3306,user="test",passwd="123321",db="stu",charset="utf8")
#创建游标对象
cursor=db.cursor()
print(db)
# 定义MySQL语句
#创建表
sql='''create table student(
        id int not null,
        name varchar(20),
        age int
        )'''
cursor.execute(sql)#执行sql语句
db.close()
刷新数据库,可以看到表创建完成。 image.png
  1. 增加数据
import pymysql
# 打开数据库的链接
db = pymysql.Connect(host="localhost",port=3306,user="test",passwd="123321",db="stu",charset="utf8")
#创建游标对象
cursor=db.cursor()
sql='''insert into student(id,name,age)
    values(1,"zhangsan",12)
'''
cursor.execute(sql)
db.commit()
db.close()

刷新数据看到结果已经出现。


image.png
  1. 查询数据
import pymysql
# 打开数据库的链接
db = pymysql.Connect(host="localhost",port=3306,user="test",passwd="123321",db="stu",charset="utf8")
#创建游标对象
cursor=db.cursor()
sql="select * from student where id=1"
cursor.execute(sql)
data1=cursor.fetchone()#获取一个对象
print(data1)

结果:

(1, 'zhangsan', 12)
import pymysql
# 打开数据库的链接
db = pymysql.Connect(host="localhost",port=3306,user="test",passwd="123321",db="stu",charset="utf8")
#创建游标对象
cursor=db.cursor()
sql="select * from student"
cursor.execute(sql)
# data1=cursor.fetchone()#获取一个对象
data2=cursor.fetchall()#获取一个对象
print(data2)

结果:

((1, 'zhangsan', 12), (2, 'lisi', 13))

如果想要取值则用基础的元组取值方法print(data2[0][1]),获取出'张三'

4.数据库的修改和删除
修改:

import pymysql
# 打开数据库的链接
db = pymysql.Connect(host="localhost",port=3306,user="test",passwd="123321",db="stu",charset="utf8")
#创建游标对象
cursor=db.cursor()
sql="update student set age=age+1"
cursor.execute(sql)
db.commit()
db.close()

刷新数据表,可以看到age都加了1


image.png

删除:

import pymysql
# 打开数据库的链接
db = pymysql.Connect(host="localhost",port=3306,user="test",passwd="123321",db="stu",charset="utf8")
#创建游标对象
cursor=db.cursor()
# sql='''insert into student(id,name,age)
#     values(1,"zhangsan",12)
# '''
sql="delete from student where name='lisi'"
cursor.execute(sql)
db.commit()
db.close()
结果:只剩下一条记录。 image.png

异常处理

import pymysql
# 打开数据库的链接
db = pymysql.Connect(host="localhost",port=3306,user="test",passwd="123321",db="stu",charset="utf8")
#创建游标对象
cursor=db.cursor()
sql1="delete from student where name='zhangsan'"
sql2="delete from student where namme='lisi'"#这里写错。
try:
    cursor.execute(sql1)
    cursor.execute(sql2)
    db.commit()#提交事物,把删除过程看做一个整体,要么都执行,要么都不执行。
except Exception as e:
    db.rollback()#回滚
    print("异常")
db.close()

结果不发生变化。我们把sql2语句故意写错,回滚的意思是所有的操作都不提交。整个数据库由于sql语句的问题只改变部分数据,那么再次调整数据会非常的麻烦,所以用提交事物和回滚的方法将异常抛出。

相关文章

网友评论

      本文标题:Python操纵mysql数据库基础

      本文链接:https://www.haomeiwen.com/subject/qxaotctx.html