一 .python和数据库
1.连接数据库
import pymysql
# #01 连接数据库
# 参数1: mysql主机名 192.168.245.1
# 参数2: 用户名
# 参数3: 密码
# 参数4: 连接数据库名
db=pymysql.connect("localhost","root","root","a")
# 02 创建一个cursor对象
cursor=db.cursor()
# 03构造sql语句
sql="select version()"
# 04 执行sql语句
cursor.execute(sql)
#05 获取返回数据
data=cursor.fetchone()
print(data) #('5.5.47',) mysql版本号
# 06 断开
cursor.close()
# 07 关闭
db.close()
2. 创建表
import pymysql
db=pymysql.connect("localhost","root","root","db1")
# 02 创建一个cursor对象
cursor=db.cursor()
# 检查表是否存在 ,如果存在则删除
cursor.execute("drop table if exists info")
# 建表
sql='create table info (id int auto_increment primary key, money int not null)'
# 执行sql语句表
cursor.execute(sql)
3. 插入数据
import pymysql
db=pymysql.connect("localhost","root","root","a")
# 02 创建一个cursor对象
cursor=db.cursor()
# 插入数据
sql='insert into bb values(0,5000)'
try:
cursor.execute(sql)
except:
# 如果提交失败 滚回上一次数据
db.rollback()
# 06 断开
cursor.close()
# 07 关闭
db.close()
4. 更新数据
import pymysql
# #01 连接数据库
# 参数1: mysql主机名
# 参数2: 用户名
# 参数3: 密码
# 参数4: 连接数据库名
db=pymysql.connect("localhost","root","root","a")
# 02 创建一个cursor对象
cursor=db.cursor()
# 更新数据
sql='update bb set money=6662 where id=1'
try:
cursor.execute(sql)
except:
# 如果提交失败 滚回上一次数据
db.rollback()
# 06 断开
cursor.close()
# 07 关闭
db.close()
5. 删除数据
import pymysql
# #01 连接数据库
# 参数1: mysql主机名
# 参数2: 用户名
# 参数3: 密码
# 参数4: 连接数据库名
db=pymysql.connect("localhost","root","root","a")
# 02 创建一个cursor对象
cursor=db.cursor()
# 删除数据
sql='delete from bb where money=6662'
try:
cursor.execute(sql)
except:
# 如果提交失败 滚回上一次数据
db.rollback()
# 06 断开
cursor.close()
# 07 关闭
db.close()
6. 查找数据
"""
fetchone() 功能:获取下一个结果集 结果集是一个对象
fetchall() 功能:接收 全部返回行
rowcount: 是一个只读属性,返回execute()方法影响的行数 (就是只你查了多少条数据)
"""
import pymysql
# #01 连接数据库
# 参数1: mysql主机名
# 参数2: 用户名
# 参数3: 密码
# 参数4: 连接数据库名
db=pymysql.connect("localhost","root","root","a")
# 02 创建一个cursor对象
cursor=db.cursor()
# 插入数据
sql='select * from cc where money>400'
try:
cursor.execute(sql)
reslist=cursor.fetchall() # 接收 全部返回行
for row in reslist:
print(row[0],row[1])
# 3 500
# 4 600
# 5 700
except:
# 如果提交失败 滚回上一次数据
db.rollback()
# 06 断开
cursor.close()
# 07 关闭
db.close()
7. 数据增删改查封装
import pymysql
class My_Sql():
def __init__(self,host,user,passwd,dbName):
self.host=host
self.user=user
self.passwd=passwd
self.dbName=dbName
def connet (self) :
self.db=pymysql.connect(self.host,self.user,self.passwd,self.dbName)
self.cursor=self.db.cursor()
def close(self):
self.cursor.close()
self.db.close()
# fetchone() 查询 功能:获取下一个结果集 结果集是一个对象
def get_one(self,sql):
res=None
try:
self.connet()
self.cursor.execute(sql)
res=self.cursor.fetchone()
self.close()
except:
print("查询失败")
return res
# fetchall() 查询 功能:接收 全部返回行
def get_all(self,sql):
res=()
try:
self.connet()
self.cursor.execute(sql)
res=self.cursor.fetchall()
self.close()
except:
print("查询失败")
return res
def insert(self,sql):
return self._edit(sql)
def update(self,sql):
return self._edit(sql)
def delete(self,sql):
return self.__edit(sql)
def __edit(self,sql):
count=0
try:
self.connet()
count=self.cursor.execute(sql)
self.db.commit()
self.close()
except :
print("提交失败了")
self.db.rollback()
return count