pymysql

How to prevent PyMySQL from escaping identifier names?

余生长醉 提交于 2019-12-24 03:42:33
问题 I am utilizing PyMySQL with Python 2.7 and try to execute the following statement: 'INSERT INTO %s (%s, %s) VALUES (%s, %s) ON DUPLICATE KEY UPDATE %s = %s' With the following parameters: ('artikel', 'REC_ID', 'odoo_created', u'48094', '2014-12-23 10:00:00', 'odoo_modified', '2014-12-23 10:00:00') Always resulting in: {ProgrammingError}(1064, u"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''artikel' ('REC

How to prevent PyMySQL from escaping identifier names?

瘦欲@ 提交于 2019-12-24 03:41:18
问题 I am utilizing PyMySQL with Python 2.7 and try to execute the following statement: 'INSERT INTO %s (%s, %s) VALUES (%s, %s) ON DUPLICATE KEY UPDATE %s = %s' With the following parameters: ('artikel', 'REC_ID', 'odoo_created', u'48094', '2014-12-23 10:00:00', 'odoo_modified', '2014-12-23 10:00:00') Always resulting in: {ProgrammingError}(1064, u"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''artikel' ('REC

pymysql,Lost connection to MySQL server during query

心已入冬 提交于 2019-12-24 00:35:51
问题 Lost connection to MySQL server during query, how can I fix this? Better fix this in my program. import pymysql connection = pymysql.connect(host='***', user='***', password='***', db='***', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor ) with connection.cursor() as cursor: sql = "SELECT MAX(group_id) FROM topic_duplicate_check" cursor.execute(sql) # Exception r = cursor.fetchone() max_gid = None try: max_gid = r['MAX(group_id)'] except: pass print(max_gid) C:\ProgramData

Python3学习笔记38-pymysql模块

不问归期 提交于 2019-12-23 13:23:08
import pymysql # 连接数据库 db = pymysql.connect( host='数据库地址', port=端口号 需要数字, user='用户名', password='密码', database='数据库名', charset='utf8' ) # 创建执行sql的光标对象,返回结果已元祖显示 # cursor = db.cursor() # 返回结果已 字段名:值 字典形式显示 cursor = db.cursor(cursor=pymysql.cursors.DictCursor) sql = "" # 执行sql cursor.execute(sql) # 关闭光标 cursor.close() # 关闭数据库连接 db.close() 查询 data = cursor.execute(sql) # 查询一条数据 data.fetchone() # 查询多条数据,条数根据传参 data.fetchmany(3) # 查询全部数据 data.fetchall() 在执行查询sql,可以使用变量接收查询结果 在获取查询结果时,需要注意指针的位置。每次获取查询结果都是从指针后开始的。指正的起始位置为0,所以会存在第一次查询得到。第二次查询不到的情况。 举个例子,假设上面查询sql语句中,查询结果返回的有10条,在执行fetchone()时,查询结果返回第一条

Using Python to access SQL with a variable column name

依然范特西╮ 提交于 2019-12-23 06:24:22
问题 I'm quite new to Python (and coding in general) and trying a small project that links my code to a MySQL database using pymysql. In general everything has gone smoothly but i'm having difficulty with the following function to find the min of a variable column. def findmin(column): cur = db.cursor() sql = "SELECT MIN(%s) FROM table" cur.execute(sql,column) mintup = cur.fetchone() if everything went smoothly this would return me a tuple with the min e.g. (1,) however the issue is if i run the

python pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')

試著忘記壹切 提交于 2019-12-23 05:17:21
问题 I am scraping a website and then storing the data into mysql, the code works fine but after sometime the it give the following error. I am using python 3.5.1 and pymysql to connect to database. pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query') here is my code: from bs4 import BeautifulSoup import urllib.request import re import json import pymysql import pymysql.cursors connection = pymysql.connect(host='XXX.XXX.XXX.XX', user='XXX', password='XXX', db='XXX',

PyMysql

走远了吗. 提交于 2019-12-22 06:14:16
如何安装pymysql 1.为什么要安装? pymysql是用来连接python和mysql之间的通道,在使用python编程时,通过他来和mysql数据库进行交互 2.怎么安装? 1. 直接用pip安装,pip install pymysql 2. 去github上下载pymysql的安装包, https://github.com/PyMySQL/PyMySQL 将安装包解压到自己某个盘符 在linux下输入命令 tar -zxvf pymysql-master.zip 进入pymysql根目录下打开终端执行命令 python setup.py install pymysql 使用流程 1. 建立数据库连接(db = pymysql.connect(...)) 2. 创建游标对象(cur = db.cursor()) 3. 游标方法: cur.execute("insert ....") 4. 提交到数据库或者获取数据 : db.commit()/db.fetchall() 5. 关闭游标对象 :cur.close() 6. 断开数据库连接 :db.close() 常用函数 db = pymysql.connect(参数列表) host :主机地址,本地 localhost port :端口号,默认3306 user :用户名 password :密码 database :库

PyMySQL Insert NULL or a String

好久不见. 提交于 2019-12-22 03:24:38
问题 I tried to insert a field ( title ) with PyMySQL that can be NULL or a string. But it doesn't work. query = """ INSERT INTO `chapter` (title, chapter, volume) VALUES ("%s", "%s", %d) """ cur.execute(query % (None, "001", 1)) cur.execute(query % ("Title", "001", 1)) This code inserts None into the database. If I remove the double quote around the first %s , it throws an error: pymysql.err.InternalError: (1054, "Unknown column 'None' in 'field list'") What can I do to insert NULL? 回答1: 1) Never

How can I escape the input to a MySQL db in Python3?

若如初见. 提交于 2019-12-21 08:23:53
问题 How can I escape the input to a MySQL db in Python3? I'm using PyMySQL and works fine, but when I try to do something like: cursor.execute("SELECT * FROM `Codes` WHERE `ShortCode` = '{}'".format(request[1])) it won't work if the string has ' or " . I also tried: cursor.execute("SELECT * FROM `Codes` WHERE `ShortCode` = %s",request[1]) The problem with this is that the library (PyMySQL) uses the formatting syntax for Python2.x, % , that doesn't work anymore. I also found this possible solution

Use a python dictionary to insert into mysql

左心房为你撑大大i 提交于 2019-12-21 05:52:30
问题 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,