pymysql

python之pymysql模块(mysql数据库操作)

半世苍凉 提交于 2019-12-18 04:30:25
import pymysql # 连接数据库 conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='12345678', db='test') # 游标默认返回元组 # cursor = conn.cursor() # 游标设置为字典,fetch返回字典格式 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 执行sql cursor.execute("insert into account (username,password) values ('a','a')") # 让改动更新到数据库 conn.commit() # 获取数据 cursor.execute("select * from account") data = cursor.fetchall() print('移动前',data) # 移动游标 cursor.scroll(0,'absolute') # 绝对位置 # cursor.scroll(1,'relative') 相对位置 data2 = cursor.fetchall() print('移动后',data2) # 获取自增id cursor.execute("insert into account

pymysql.err.InternalError: (1054, \"Unknown column 'None' in 'field list'\")

可紊 提交于 2019-12-18 04:23:05
错误提示: Traceback (most recent call last): File "D:/projectwc/test/dd.py", line 43, in <module> effect_row = cursor.execute("insert into `222` set c={}".format(None)) File "C:\python\lib\site-packages\pymysql\cursors.py", line 166, in execute result = self._query(query) File "C:\python\lib\site-packages\pymysql\cursors.py", line 322, in _query conn.query(q) File "C:\python\lib\site-packages\pymysql\connections.py", line 835, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "C:\python\lib\site-packages\pymysql\connections.py", line 1019, in _read_query_result

Pymysql Insert Into not working

送分小仙女□ 提交于 2019-12-17 15:59:20
问题 I'm running this from PyDev in Eclipse... import pymysql conn = pymysql.connect(host='localhost', port=3306, user='userid', passwd='password', db='fan') cur = conn.cursor() print "writing to db" cur.execute("INSERT INTO cbs_transactions(leagueID) VALUES ('test val')") print "wrote to db" The result is, at the top of the Console it says C:...test.py, and in the Console: writing to db wrote to db So it's not terminating until after the execute command. But when I look in the table in MySQL it's

What is PyMySQL and how does it differ from MySQLdb? Can it affect Django deployment?

故事扮演 提交于 2019-12-17 08:33:04
问题 I just solved some problems in my Django 1.3 app by using PyMySQL instead of MySQLdb. I followed this tutorial on how to make the switch: http://web-eng-help.blogspot.com/2010/09/install-mysql-5-for-python-26-and.html Now I want to know what PyMySQL actually is and how it is different from MySQLdb. I am using it on localhost and will then upload it to some hosting. Is it fine to use PyMySQL on localhost and on hosting whatever they provide? Since I have changed "MySQLdb" in base.py and

在pymysql中防止SQL注入

荒凉一梦 提交于 2019-12-17 06:07:50
sql注入就是在python程序中 改变原有的sql语句的含义 使数据库中的数据全部显示或者部分显示 能注入的原理 提前把SQL语句中的未知项 直接 拼接在SQL语句上 sql = ‘select * from user where uname="%s" and upsw="%s"’ % (name, psw) 如果你输入的那么为"哈哈" or 1 = 1 # \ 再去执行这条SQL语句 – 漏洞就出现了 会查询全部数据 具体实现注入代码如下 import pymysql name = input ( "请输入用户名: " ) psw = input ( "请输入密码: " ) con = None cur = None try : con = pymysql . connect ( host = "localhost" , port = 3306 , user = "root" , passwd = "123456" , database = "py1910" , charset = "utf8" ) if con : print ( "链接成功" ) #获取操作数据库的游标 cur = con . cursor ( ) sql = 'select * from user where uname="%s" and upsw="%s"' % ( name , psw ) print

Python之pymysql的使用

故事扮演 提交于 2019-12-16 18:00:25
在python3.x中,可以使用pymysql来MySQL数据库的连接,并实现数据库的各种操作,本次博客主要介绍了pymysql的安装和使用方法。 PyMySQL的安装 一、.windows上的安装方法: 在python3.6中,自带pip3,所以在python3中可以直接使用pip3去安装所需的模块: pip3 install pymysql -i https://pypi.douban.com/simple 二、.linux下安装方法: 1.tar包下载及解压 下载tar包 wget https://pypi.python.org/packages/29/f8/919a28976bf0557b7819fd6935bfd839118aff913407ca58346e14fa6c86/PyMySQL-0.7.11.tar.gz#md5=167f28514f4c20cbc6b1ddf831ade772 解压并展开tar包 tar xf PyMySQL-0.7.11.tar.gz 2.安装 [root@localhost PyMySQL-0.7.11]# python36 setup.py install 数据库的连接 本次测试创建的数据及表: #创建数据库及表,然后插入数据 mysql> create database dbforpymysql; mysql> create

Python操作MySQL:pymysql和SQLAlchemy

烈酒焚心 提交于 2019-12-16 15:21:46
本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同。 下载安装 pip3 install pymysql 使用操作 1、执行SQL #!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql # 创建连接 conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1') # 创建游标 cursor = conn.cursor() # 执行SQL,并返回收影响行数 effect_row = cursor.execute("update hosts set host = '1.1.1.2'") # 执行SQL,并返回受影响行数 #effect_row = cursor.execute("update hosts set host = '1.1.1.2' where nid > %s", (1,)) # 执行SQL,并返回受影响行数 #effect_row = cursor.executemany("insert into hosts(host,color_id

Python——操作数据库(二)

青春壹個敷衍的年華 提交于 2019-12-16 11:37:05
Demo-01 1 import pymysql 2 #连接数据库 3 db = pymysql.connect(host='localhost',user='root', password='123456', port=3306) 4 #获取游标 5 cursor = db.cursor() 6 #执行 7 cursor.execute('SELECT VERSION()') 8 #fetchone()获取结果集下一行 9 data = cursor.fetchone() 10 #输出 11 print('Database version:', data) 12 #执行 13 cursor.execute("CREATE DATABASE spiders DEFAULT CHARACTER SET utf8") 14 db.close() Demo-02 1 import pymysql 2 3 #连接数据库 4 db = pymysql.connect(host='localhost', user='root', password='123456', port=3306, db='spiders') 5 #获取游标 6 cursor = db.cursor() 7 #sql语句 8 sql = 'CREATE TABLE IF NOT EXISTS students (id

python连接MySQL

丶灬走出姿态 提交于 2019-12-16 10:53:14
python连接MySQL # 下载第三方模块: pip3 install pymysql # 面条版 import pymysql # 连接mysql数据库的模块 # 1.连接数据库 client = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123456', database='db4', charset='utf8', # 此处不能写utf-8 autocommit=True ) # print(client) # 2.获取游标对象 ----> 游标 可以用来提交sql命令 # 将取回值以字典形式显示:pymysql.cursors.DictCursor cursor_obj = client.cursor(pymysql.cursors.DictCursor) # cursor_obj = client.cursor() # 以元组形式显示(1, 'tank') # 3.通过execute 可以提交sql语句 # 1)查数据 # sql = 'select * from emp' # # 提交sql语句 # cursor_obj.execute(sql) # # # 4.提交后,通过cursor_obj对象.fetchall() 获取所以查询到的结果 # res = cursor

python之pymysql模块简单应用

﹥>﹥吖頭↗ 提交于 2019-12-16 00:45:06
众所周知,想要在python程序中执行SQL语句需要使用第三方模块:pymysql。 下面,我将为大家简述一下pymysql第三方库的安装到使用的大体流程。 pymysql的安装 1.windows系统通过pip安装pymysql: pip3 install pymysql 2.Liunx系统中,可以使用以下命令安装pymysql模块: sudo pip3 install pymysql 在python程序中连接数据库: 其流程可以分为6步,接下来我会为大家简述一下过程及代码。 1.导入模块 import pymysql 2.创建数据库连接对象 host:数据库的IP地址,本机域名为localhost,本机IP为127.0.0.1 port:数据库的端口,默认3306 user:数据库的用户名 password:数据库用户名的密码 database:连接后使用的数据库名称 charset:数据库的字符集 con = pymysql.connect(host,port,user,password,database,charset) 注意:pymysql中的connect = Connect = Connection 3.使用数据库连接对象调用cursor()方法创建游标 cur = con.cursor() 注意:创建游标时会默认开启一个隐式的事物