pymysql

Use a python dictionary to insert into mysql

我们两清 提交于 2019-12-21 05:52:22
问题 I am trying to take the data from a dictionary (the example is simplified for readability) and insert it into a mysql database. I have the following piece of code. import pymysql conn = pymysql.connect(server, user , password, "db") cur = conn.cursor() ORFs={'E7': '562', 'E6': '83', 'E1': '865', 'E2': '2756 '} table="genome" cols = ORFs.keys() vals = ORFs.values() sql = "INSERT INTO %s (%s) VALUES(%s)" % ( table, ",".join(cols), ",".join(vals)) print sql print ORFs.values() cur.execute(sql,

pymysql模块的使用

两盒软妹~` 提交于 2019-12-21 03:07:38
pymysql模块 # 1.安装:pip3 insatll pymysql # 2.代码链接 import pymysql #链接 conn=pymysql.connect( host='localhost', user='root', password='123', database='egon', charset='utf8') autocommit = True # 自动提交;rows = cursor.excute(sql) 手动提交 #游标 cursor=conn.cursor() #执行完毕返回的结果集默认以元组显示 #cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) # 以字典的方式显示数据 # 3.pymysql操作数据库 #执行sql语句 user = input(">>>:").strip() pwd = input(">>>:").strip() sql='select * from userinfo where name="%s" and password="%s"' %(user,pwd) #注意%s需要加引号 rows=cursor.execute(sql) #执行sql语句,返回sql查询成功的记录数目 # 获取真实数据cursor.fetchone(),cursor.fetchall()

Navicat使用,pymysql模块

痞子三分冷 提交于 2019-12-20 23:58:17
Navicat使用 掌握: #1. 测试+链接数据库 #2. 新建库 #3. 新建表,新增字段+类型+约束 #4. 设计表:外键 #5. 新建查询 #6. 建立表模型 #注意: 批量加注释:ctrl+?键 批量去注释:ctrl+shift+?键 快速建表 #准备表、记录 >>> 命令行 mysql> create database db1; mysql> use db1; mysql> source /root/init.sql # navicat建表 pymysql模块 # 1.安装:pip3 insatll pymysql # 2.代码链接 import pymysql #链接 conn=pymysql.connect( host='localhost', user='root', password='123', database='egon', charset='utf8') #游标 cursor=conn.cursor() #执行完毕返回的结果集默认以元组显示 #cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) # 以字典的方式显示数据 # 3.pymysql操作数据库 #执行sql语句 user = input(">>>:").strip() pwd = input(">>>:").strip() sql=

pymysql模块的使用

旧城冷巷雨未停 提交于 2019-12-20 00:54:35
本节重点: pymysql的下载和使用 execute()之sql注入 增、删、改:conn.commit() 查:fetchone、fetchmany、fetchall 一、pymysql的下载和使用   之前我们都是通过MySQL自带的命令行客户端工具mysql来操作数据库,那如何在python程序中操作数据库呢?这就用到了pymysql模块,该模块本质就是一个套接字客户端软件,使用前需要事先安装。 (1)pymysql模块的下载 pip3 install pymysql (2)pymysql的使用 数据库和数据都已存在 # 实现:使用Python实现用户登录,如果用户存在则登录成功(假设该用户已在数据库中) import pymysql user = input('请输入用户名:') pwd = input('请输入密码:') # 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,

Python高级应用程序设计任务

老子叫甜甜 提交于 2019-12-20 00:41:23
Python高级应用程序设计任务要求 用Python实现一个面向主题的网络爬虫程序,并完成以下内容: (注:每人一题,主题内容自选,所有设计内容与源代码需提交到博客园平台) 一、主题式网络爬虫设计方案(15分) 1.主题式网络爬虫名称 穷游网香港旅游攻略中的景点爬取 2.主题式网络爬虫爬取的内容与数据特征分析 内容:主要爬取穷游网香港旅游攻略景点排名前 100的旅游景点的信息,以及对应的景点评论信息。 数据特征分析:对所有的评论做了词云、对景点的评分做了可视化和柱状图 3.主题式网络爬虫设计方案概述(包括实现思路与技术难点) 实现思路:首先通过分析此网站得知数据的传输通过 ajax实现,其中景点信息固定url为“https://place.qyer.com/poi.php?action=list_json”,通过修改data参数实现翻页。创建一个ScenicInfo 类定义param_info和insert实现对数据的抓取和保存。景点评论信息是通过url拼接来实现,创建一个Comments类定义param_comments发起请求获得json数据,insert方法插入数据库 技术难点:在爬取过程中没有出现反爬。 二、主题页面的结构特征分析(15分) 1.主题页面的结构特征 每页 15项数据,爬取了64页,一共960项数据

No module named 'pymysql'

感情迁移 提交于 2019-12-18 13:55:15
问题 I'm trying to use PyMySQL on Ubuntu. I've installed pymysql using both pip and pip3 but every time I use import pymysql , it returns ImportError: No module named 'pymysql' I'm using Ubuntu 15.10 64-bit and Python 3.5. The same .py works on Windows with Python 3.5, but not on Ubuntu. 回答1: Sort of already answered this in the comments, but just so this question has an answer, the problem was resolved through running: sudo apt-get install python3-pymysql 回答2: Use: import pymysql Not: import

Python3 mysqlclient-1.3.6 (aka PyMySQL) usage?

倾然丶 夕夏残阳落幕 提交于 2019-12-18 12:52:52
问题 Im still very much learning python and all the different ways to use 3rd party modules. I have installed https://pypi.python.org/pypi/mysqlclient which was recommended here Python 3 and MySQL I believe i installed the package correctly D:\install\python modules>python -m pip install mysqlclient-1.3.6-cp34-none-win_amd64.whl Unpacking d:\install\python modules\mysqlclient-1.3.6-cp34-none-win_amd64.whl Installing collected packages: mysqlclient Successfully installed mysqlclient Cleaning up...

Python学习 Day17 Python对Mysql操作和使用ORM框架(SQLAlchemy)

混江龙づ霸主 提交于 2019-12-18 10:10:36
Python对Mysql操作和使用ORM框架(SQLAlchemy) Mysql 常见操作 数据库操作 创建数据库  create database fuzjtest 删除数据库  drop database fuzjtest 查询数据库 show databases 切换数据库 use databas 123123 ###用户授权 创建用户 create user '用户名'@'IP地址' identified by '密码'; 删除用户 drop user '用户名'@'IP地址'; 修改用户 rename user '用户名'@'IP地址'; to '新用户名'@'IP地址';; 修改密码 set password for '用户名'@'IP地址' = Password('新密码') 查看权限 show grants for '用户'@'IP地址' 授权 grant 权限 on 数据库.表 to '用户'@'IP地址 取消权限 revoke 权限 on 数据库.表 from '用户'@'IP地址' PS:用户权限相关数据保存在mysql数据库的user表中,所以也可以直接对其进行操作(不建议) 授权数据库 相关权限 all privileges 除grant外的所有权限 select 仅查权限 select,insert 查和插入权限 ... usage 无访问权限

How do PyMySQL prevent user from sql injection attack?

◇◆丶佛笑我妖孽 提交于 2019-12-18 09:09:03
问题 Sorry for ask here but I cannot found much reference about pymysql's security guide about how do we prevent sql injection, When I do PHP develope I know use mysql preparedstatement(or called Parameterized Query or stmt),but I cannot found reference about this in pymysql simple code use pymysql like sqls="select id from tables where name=%s" attack="jason' and 1=1" cursor.execute(sqls,attack) How do I know this will prevent sql injection attack or not?if prevent succeed,how do pymysql prevent

Python3中获取mysql的结果集

不想你离开。 提交于 2019-12-18 06:24:55
目录 起步 查 获取结果 fetchone 情况一 情况二 情况三 fetchmany 情况一 情况二 情况三 fetchall 情况一 情况二 情况三 scroll移动 相对移动 绝对移动 示例代码 起步 #!/usr/bin/python3 # -*- coding: utf-8 -*- """pymysql查询 """ import pymysql conn = pymysql . connect ( user = 'root' , password = '' , host = 'localhost' , port = 3306 , charset = 'utf8mb4' , database = 'hardy2_db' , ) # 游标类型 cursor = conn . cursor ( cursor = None ) # cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) 查 sql = 'select id, age, name as nickname from pymysql_tb;' affected = cursor . execute ( query = sql ) # 拿到查询到的记录数 # print(affected) # int, 返回受影响的行数数目 获取结果 fetchone cursor