首页 > 数据库 >python + mysql

python + mysql

时间:2023-08-04 09:22:04浏览次数:40  
标签:python mysql 游标 pymysql cursor sql conn row

1.连接mysql数据库,基本数据查询流程

# 1.连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='', db='db8', charset='utf8')

# 2.创建游标
cursor = conn.cursor()
#注意%s需要加引号
sql = "select * from userinfo where username='%s' and pwd='%s'" %(user, pwd)

# 3.执行sql语句
result=cursor.execute(sql) #执行sql语句,返回sql查询成功的记录数目
print(result)

# 关闭游标
cursor.close()
# 关闭连接 conn.close()

 

2. 数据库增,删,改,操作

commit()方法:在数据库里增、删、改的时候,必须要进行提交,否则插入的数据不生效。

# 1.连接
conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='db', charset='utf8')


# 2.创建游标
cursor = conn.cursor()


# 操作
# 增
# sql = "insert into userinfo(username,pwd) values (%s,%s)"


# effect_row = cursor.execute(sql,(username,pwd))
#同时插入多条数据
#cursor.executemany(sql,[('李四','110'),('王五','119')]) 

# print(effect_row)#

# 改
# sql = "update userinfo set username = %s  where id = 2"
# effect_row = cursor.execute(sql,username)
# print(effect_row)


# 删
sql = "delete from userinfo  where id = 2"
effect_row = cursor.execute(sql)
print(effect_row)


#一定记得commit
conn.commit()

# 4.关闭游标
cursor.close()

# 5.关闭连接
conn.close()

查询数据:

fetchone、fetchmany、fetchall

fetchone():获取下一行数据,第一次为首行;
fetchall():获取所有行数据源
fetchmany(4):获取4行数据

拿fetchall():举例

import pymysql

# 1.连接
conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='db8', charset='utf8')


# 2.创建游标
cursor = conn.cursor()

sql = 'select * from userinfo'
cursor.execute(sql)

# 获取所有的数据
rows = cursor.fetchall()
print(rows)

# 4.关闭游标
cursor.close()

# 5.关闭连接
conn.close()

#运行结果
((1, 'mjj', '123'), (3, '张三', '110'), (4, '李四', '119'))

默认情况下,我们获取到的返回值是元组,只能看到每行的数据,却不知道每一列代表的是什么,这个时候可以使用以下方式来返回字典,每一行的数据都会生成一个字典:

cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)  
#在实例化的时候,将属性cursor设置为pymysql.cursors.DictCursor
{'id': 1, 'username': 'mjj', 'pwd': '123'}

 

标签:python,mysql,游标,pymysql,cursor,sql,conn,row
From: https://www.cnblogs.com/xujunhui/p/17605017.html

相关文章

  • 【备考实战】计算机二级Python刷题【一】
    时间报名时间:2023-8-31考试时间:2023-9-23第1题计算机完成一条指令所花费的时间称为一个A.执行时序B.存取周期C.执行速度D.指令周期参考解析参考解析:D[解析]一般把计算机完成一条指令所花费的时间称为-一个指令周期。指令周期越短,指令执行就越快。本题答案为D选项......
  • MySQL查询语句(1)
    连接数据库mysql-hlocalhost-uroot-prootDQL-介绍DQL英文全称是DataQueryLanguage(数据查询语言),数据查询语言,用来查询数据库中表的记录查询SELECT字段列表FROM表名列表WHERE条件列表GROUPBY分组字段列表HAVING分组后条件列表ORDERBY排序字......
  • Python用RNN神经网络:LSTM、GRU、回归和ARIMA对COVID19新冠疫情人数时间序列预测|附代
    全文下载链接: http://tecdat.cn/?p=27042最近我们被客户要求撰写关于新冠疫情的研究报告,包括一些图形和统计输出。在本文中,该数据根据世界各国提供的新病例数据提供。获取时间序列数据  df=pd.read_csv("C://global.csv")探索数据此表中的数据以累积的形式呈现,为了......
  • vscode snnipet of python
    {//Placeyoursnippetsforpythonhere.Eachsnippetisdefinedunderasnippetnameandhasaprefix,bodyand//description.Theprefixiswhatisusedtotriggerthesnippetandthebodywillbeexpandedandinserted.Possiblevariablesare://$1,......
  • Python 优化第一步: 性能分析实践 使用cporfile+gprof2dot可视化
    拿来主义:python-mcProfile-oprofile.pstatsto_profile.pygprof2dot-fpstatsprofile.pstats|dot-Tpng-oclick.png然后顺着浅色线条优化就OK了。 windows下:google下graphviz-2.38.msi,然后安装。dot命令需要。gitclone https://github.com/jrfonseca/gprof2dot.git......
  • python 闭包变量不允许write,要使用nonlocal
     以下是一段简单的闭包代码示例:deffoo():m=3n=5defbar():a=4returnm+n+areturnbar>>>bar=foo()>>>bar()12是可以的!但是:deffoo():m=3n=5defbar():a=4m+=1#不可以!!!return......
  • python 代码混淆工具汇总
    pyminifierPyminifierisaPythoncodeminifier,obfuscator,andcompressor.NoteForthelatest,completedocumentation:http://liftoff.github.io/pyminifier/Forthelatestcode:https://github.com/liftoff/pyminifierOverviewWhenyouinstallpyminifierit......
  • Python安装与配置
    一、Windows下安装Python3。官网地址:https://www.python.org/下载地址:https://www.python.org/ftp/python/3.6.4/python-3.6.4-amd64.exe二、安装1、为了不去设置环境变量,我选择Customizeinstallation安装,并且勾选AddPython3.6toPATH。   2、选择安装路径,我安装......
  • mysql插入报错java.sql.SQLException: Incorrect string value: '\xF0\x9F\x87\xA
    背景环境java8,centos7.9,mysql8.0.34新装的环境,默认给装了mysql8,想着与时俱进用下新版,结果插入就报错java.sql.SQLException:Incorrectstringvalue:'\xF0\x9F\x87\xA8\xF0\x9F...'forcolumn解决方法这个错误通常是由于MySQL数据库中的字符集不支持存储特定的字符或表情符......
  • python coding style guide 的快速落地实践——业内python 编码风格就pep8和谷歌可以
    pythoncodingstyleguide的快速落地实践机器和人各有所长,如codingstyle检查这种可自动化的工作理应交给机器去完成,故发此文帮助你在几分钟内实现codingstyle的自动检查。1.有哪些著名的PythonCodingStyleGuidePEP8https://www.python.org/dev/peps/pep-0008/发明Python语言......