Python 连接数据库 day5
import pymysql
#连接数据库,port必须是int型,字符编码是utf8,不能是utf-8,password必须是字符串
conn = pymysql.connect(host='数据库ip',user='user1',password='',
db='db_name',port=3306,charset='utf8',autocommit=True)#建立连接
cur= conn.cursor()#建立游标,可以把游标当作仓库管理员 cur.execute('show tables;')#只是帮你执行sql语句
print(cur.fetchall())#获取sql语句执行的结果
sql1="select * from app_student limit 5;"
cur.execute(sql1)
print(cur.fetchall())#取所有数据
sql2="select * from app_student where name='smh'"
cur.execute(sql2)
print(cur.fetchone())#只取一条,若返回结果有多条,只取第一条
sql3="select * from app_student where name ='hhh'"
cur.execute(sql3)
print(cur.fetchmany(3))#取几条
sql4="insert app_student ( `name`, `sex`, `age`, `addr`, `grade`, `phone`, `gold`) " \
"values ( 'test', 'girl', '18', 'beijing', 'jxz', '13121211111', '100');"
cur.execute(sql4)
conn.commit()
#提交,update、delete、insert这些修改数据库的语句,执行需要commit才能成功
#在connect中添加autocommit=True则自动提交 cur.close()#关闭游标
conn.close()#关闭连接,使用完了要关连接,不然会占数据库
将sql执行返回结果转为字典格式:
import pymysql
#连接数据库,port必须是int型,字符编码是utf8,不能是utf-8,password必须是字符串
conn = pymysql.connect(host='118.24.3.40',user='jxz',password='',
db='jxz',port=3306,charset='utf8',autocommit=True)#建立连接
cur=conn.cursor(pymysql.cursors.DictCursor)#传一个游标类型,可展示为字典
sql5= "select * from app_student where name='hhh'"
cur.execute(sql5)
print(cur.fetchmany(3))
cur.close()
conn.close() 结果为:
[{'id': 328, 'name': 'hhh', 'sex': 'girl', 'age': 18, 'addr': '河南省济源市北海大道32号', 'grade': '天蝎座', 'phone': '', 'gold': 100},
{'id': 345, 'name': 'hhh', 'sex': 'girl', 'age': 18, 'addr': '河南省济源市北海大道32号', 'grade': '天蝎座', 'phone': '', 'gold': 100},
{'id': 347, 'name': 'hhh', 'sex': 'girl', 'age': 18, 'addr': '河南省济源市北海大道32号', 'grade': '天蝎座', 'phone': '', 'gold': 100}]
将连接数据库写为方法
import pymysql def op_mysql(host,user,password,db,sql,port=3306,many_tag=False):#mang_tag是标识返回数据
conn = pymysql.connect(host=host,user=user,password=password,db=db,port=port,charset='utf8',autocommit=True)
cur = conn.cursor(pymysql.cursors.DictCursor)
cur.execute(sql)
if many_tag:#有
result = cur.fetchall()
else:
result = cur.fetchone()
cur.close()
conn.close()
return result
Python 连接数据库 day5的更多相关文章
- python笔记 - day5
python笔记 - day5 参考: http://www.cnblogs.com/wupeiqi/articles/5484747.html http://www.cnblogs.com/alex ...
- Python学习day5作业
目录 Python学习day5作业 ATM和购物商城 1. 程序说明 2. 基本流程图 3. 程序测试帐号 4. 程序结构: 5. 程序测试 title: Python学习day5作业 tags: p ...
- python连接数据库问题小结
在使用python连接数据库的时候遇到了这个问题: 大概意思就是在django的setting.py中配置的用户名和密码报错. 主要就是修改setting.py的配置 其中在里边的name和user项 ...
- 3.Python连接数据库PyMySQL
1.安装PyMySQL,输入命令:pip3 install PyMySQL 2.使用Navicat,创建数据库:TESTDB,表:EMPLOYEE,字段:FIRST_NAME,LAST_NAME,AG ...
- 数据测试001:利用python连接数据库插入excel数据
数据测试001:利用python连接数据库插入excel数据 最近在做数据测试,主要是做报表系统,需要往数据库插入数据验证服务逻辑,本次介绍如何利用python脚本插入Oracle和Mysql库中: ...
- python连接数据库自动发邮件
python连接数据库实现自动发邮件 1.运行环境 redhat6 + python3.6 + crontab + Oracle客户端 2.用到的模块 3.操作步骤 (1)安装python3.6参考 ...
- Python连接数据库流行用到的第三方库
Python连接数据库流行用到的第三方库: mysqldb:只支持Python2.x mysqlclient : mysqldb的衍生版本,完全兼容mysqldb,同时支持Python3.x,安装较复 ...
- Windows下Python连接数据库(mysql, mongodb)
一 实验平台 1 os: win7 64位旗舰版sp1 2 python: 2.7.10 x64 二 连接数据库 1 连接 mysql数据库 (1)下载mysql(5.6.25-winx64) 建议下 ...
- python连接数据库使用SQLAlchemy
参考python核心编程 ORM(Object Relational Mapper),如果你是一个更愿意操作Python对象而不是SQL查询的程序员,并且仍然希望使用关系型数据库作为你的后端,那么你可 ...
随机推荐
- InnoDB: Error: log file .\ib_logfile0 is of different size 0 10485760 bytes
启动WAMP Server的时候报例如以下的错误: 140618 23:12:32 [Note] Plugin 'FEDERATED' is disabled. 140618 23:12:32 Inn ...
- ios 仿android gallery控件
ios 上没有发现与android gallery类似的控件,因为在项目上须要使用到.採用UICollectionView实现 watermark/2/text/aHR0cDovL2Jsb2cuY3N ...
- 【网络流】 HDU 3468 Treasure Hunting
题意: A-Z&&a-z 表示 集结点 从A点出发经过 最短步数 走到下一个集结点(A的下一个集结点为B ,Z的下一个集结点为a) 的路上遇到金子(*)则能够捡走(一个点仅仅能捡一次) ...
- kafka02
- python 执行shell
一.import os ex: 1.os.system('ls') ----并不能得到返回值 2.output = os.popen('ls') res = output.read() ----能得到 ...
- Configuration.SectionGroups
对于一个空的配置文件,默认自带的sectiongroups 默认有10个section节点 1 ConfigurationSectionGroupName=system.runtime.seriali ...
- POJ 3268 最短路应用
POJ3268 题意很简单 正向图跑一遍SPFA 反向图再跑一边SPFA 找最大值即可. #include<iostream> #include<cstdio> #includ ...
- bzoj3663
几何+lis 很巧妙.直接做很困难,那么我们转化一下,把每个点能看见的圆弧画出来.只有这些圆弧相交时才满足条件. 那么也就是找出圆上尽量多两两相交的区间. 所以我们先按左端点极角排序,然后固定一个必须 ...
- PCB MS SQL 存储过程(CLR) 实现Json转DataTable表的方法
一.准备需转为DataTable的json字符串 原json字符串数据 [{"TechName":"ECN","TechNo":" ...
- [Swift通天遁地]三、手势与图表-(8)制作股市中常用的蜡烛图表
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...