首页 > 数据库 >python连接数据库

python连接数据库

时间:2023-10-17 15:11:24浏览次数:52  
标签:python 数据库 db pymysql SQL data 连接 cu

import pymysql
from pymysql.constants import CLIENT
'''
pymysql 执行多条SQL语句 8.0版本之后需要在建立连接的时候添加参数client_flag = CLIENT.MULTI_STATEMENTS
'''
# 打开数据库连接
db = pymysql.connect(host='localhost',user='root',
                     passwd='root',db='logindemo',charset='utf8',port=3306,
                     client_flag = CLIENT.MULTI_STATEMENTS)

# 获取操作游标,数据库句柄,传递操作
cu = db.cursor()
db.ping(reconnect = True)  # 检查连接是否断开,断开重连
# 构造SQL语句 ,SQL语句中如果出现数据库关键字则用反引号引起
sqlStr = '''
update user set `uname`='lll' where uid=1;
select * from user;
'''
# 执行SQL语句
cu.execute(sqlStr)

# 如果涉及到数据库的修改,则需要提交
db.commit()
data = cu.fetchone() # 返回第一条数据 ,类型为元组
print(data) # 第一条
# # data = cu.fetchone()#
# # print(data) # 接着上条数据,返回一条数据
# datas = cu.fetchall() # 返回所有结果 ,类型为元组

# 关闭数据库连接
db.close()

 

标签:python,数据库,db,pymysql,SQL,data,连接,cu
From: https://www.cnblogs.com/aiyumo/p/17769755.html

相关文章

  • 【原型链污染】Python与Js
    【原型链污染】Python与Js一、背景最近在TSCTF的比赛题中遇到了Python的原型链污染题目,所以借此机会学习一下。说到原型链,最多的还是在Js中,所以就一并学习一下。(因为是菜鸡所以文章可能的存在一些错误,欢迎批评指正)。二、JS原型链简介原型是Js代码中对象的继承方式。其实和别的......
  • 数据库SQL实战|牛客网(查找入职员工时间排名倒数第三的员工所有信息)
    描述有一个员工employees表简况如下: 请你查找employees里入职员工时间排名倒数第三的员工所有信息,以上例子输出如下:输出:10005|1955-01-21|Kyoichi|Maliniak|M|1989-09-12droptableifexists`employees`;CREATETABLE`employees`(`emp_no`int(11)NOTNULL,`bir......
  • python链接mongodb的问题
    python链接mongodb需要指定数据库importpymongomonclient=pymongo.MongoClient("mongodb://用户名:密码@192.168.10.200:27017/数据库名")mondb=monclient["数据库名"]moncol=mondb["表名"]网上很多资料都没有指定数据库名,导致后续操作提示没有权限......
  • 使用mobaxterm连接kali虚拟机
    0x01首先需要开启root用户远程ssh登录服务1、先执行以下命令cd/etc/ssh//进入文件目录ls//查看文件2、编辑ssh_config配置文件vimssh_config找到其中#PasswordAuthenticationno去掉其注释,并将no改为yes然后按esc再:wq退出3、编辑sshd_config配置......
  • python 封装日志logging
    #!/usr/bin/python#-*-coding:utf-8-*-importloggingimporttimeimportosclassLog(object):'''封装后的logging'''def__init__(self,logger=None,log_cate='search'):'''......
  • python练习.4
    问:从键盘输入十个数,统计非负数字的个数和非负数字的和defAyue():arr=[]i=0my_sum=0my_num=0whilei<10:x=int(input("请输入你要统计的数字:"))if(x>=0):arr.append(x)my_sum+=1my......
  • python练习.3
    问:已知abc都是一位数,求当三位数abc加上cba的和为1333时,abc分别的值defAyue():forainrange(10):forbinrange(10):forcinrange(10):ifa*100+b*10+c+c*100+b*10+a==1333:print(a,b,c)print(Ayue(......
  • [907] Merge multiple PDF files into one in Python
    YoucanmergemultiplePDFfilesintooneusingvariousPythonlibraries.OnecommonapproachistousethePyPDF2library,whichallowsyoutomanipulatePDFfiles.Here'sastep-by-stepguidetomergingmultiplePDFsintooneusingPyPDF2:First,......
  • [908] Implementation of the progress bar in Python
    YoucanimplementaprogressbarinPythontovisuallyrepresenttheprogressofataskusingvariouslibraries.Onecommonlyusedlibraryforthispurposeistqdm.Here'showtousetqdmtocreateasimpleprogressbar:First,youneedtoinstall......
  • [910] Copy a file to another directory with a new name in Python
    TocopyafiletoanotherdirectorywithanewnameinPython,youcanusetheshutillibrary.Here'showyoucandoit:importshutil#Specifythesourcefilepathsource_file='path/to/source/file.txt'#Specifythedestinationdirect......