pymysql

pymysql callproc() appears to affect subsequent selects

旧巷老猫 提交于 2019-12-01 06:44:41
问题 I'm attempting to transition a code base from using MySQLdb to pymysql. I'm encountering the following problem and wonder if anyone has seen something similar. In a nutshell, if I call a stored procedure through the pymysql cursor callproc() method a subsequent 'select' call through the execute() method using the same or a different cursor returns incorrect results. I see the same results for Python 2.7.2 and Python 3.2.2 Is the callproc() method locking up the server somehow? Code is shown

Mock a MySQL database in Python

徘徊边缘 提交于 2019-12-01 03:19:59
I use Python 3.4 from the Anaconda distribution. Within this distribution, I found the pymysql library to connect to an existing MySQL database, which is located on another computer. import pymysql config = { 'user': 'my_user', 'passwd': 'my_passwd', 'host': 'my_host', 'port': my_port } try: cnx = pymysql.connect(**config) except pymysql.err.OperationalError : sys.exit("Invalid Input: Wrong username/database or password") I now want to write test code for my application, in which I want to create a very small database at the setUp of every test case, preferably in memory. However, when I try

PyMySQL Warning: (1366, \"Incorrect string value: '\\\\xF0\\\\x9F\\\\x98\\\\x8D t…')

余生颓废 提交于 2019-12-01 01:28:19
I'm attempting to import data (tweets and other twitter text information) into a database using Pandas and MySQL. I received the following error: 166: Warning: (1366, "Incorrect string value: '\xF0\x9F\x92\x9C\xF0\x9F...' for column 'text' at row 3") result = self._query(query) 166: Warning: (1366, "Incorrect string value: '\xF0\x9F\x98\x8D t...' for column 'text' at row 5") result = self._query(query) After a thorough search it seems as if there's something wrong in the way my database columns are set up. I've tried setting the database charset to UTF8 and collating it to utf_unicode_ci but I

How to copy a database with mysqldump and mysql in Python?

淺唱寂寞╮ 提交于 2019-12-01 00:31:52
I am writing a simple Python script to copy a MySQL database. I am attempting to copy the database based on the following SO questions and their answers: " Copy/duplicate database without using mysqldump ", " python subprocess and mysqldump " and " Python subprocess, mysqldump and pipes ". However, my script does not work for some reason I cannot see as the tables and the data do not appear in my new database. I can see from my output that the mysqldump works correctly (I see a "Dump completed on..." in my output), so I think that something is wrong with my pipeline . Here is my script: #!/usr

PyMySQL Warning: (1366, "Incorrect string value: '\\xF0\\x9F\\x98\\x8D t…')

偶尔善良 提交于 2019-11-30 21:36:02
问题 I'm attempting to import data (tweets and other twitter text information) into a database using Pandas and MySQL. I received the following error: 166: Warning: (1366, "Incorrect string value: '\xF0\x9F\x92\x9C\xF0\x9F...' for column 'text' at row 3") result = self._query(query) 166: Warning: (1366, "Incorrect string value: '\xF0\x9F\x98\x8D t...' for column 'text' at row 5") result = self._query(query) After a thorough search it seems as if there's something wrong in the way my database

How to copy a database with mysqldump and mysql in Python?

▼魔方 西西 提交于 2019-11-30 19:59:50
问题 I am writing a simple Python script to copy a MySQL database. I am attempting to copy the database based on the following SO questions and their answers: "Copy/duplicate database without using mysqldump", "python subprocess and mysqldump" and "Python subprocess, mysqldump and pipes". However, my script does not work for some reason I cannot see as the tables and the data do not appear in my new database. I can see from my output that the mysqldump works correctly (I see a "Dump completed on..

python 第五十九章 orm单表操作

萝らか妹 提交于 2019-11-30 19:31:59
orm单表操作 对象关系映射(object relational mapping) orm语句 -- sql -- 调用pymysql客户端发送sql -- mysql服务端接收到指令并执行 orm介绍 django 连接mysql 1 settings配置文件中 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'orm02', 'USER':'root', 'PASSWORD':'666', 'HOST':'127.0.0.1', 'PORT':3306, } } 2 项目文件夹下的init文件中写上下面内容,用pymysql替换mysqldb import pymysql pymysql.install_as_MySQLdb() 3 models文件中创建一个类 # class UserInfo(models.Model): # id = models.AutoField(primary_key=True) # name = models.CharField(max_length=10) # bday = models.DateField() # checked = models.BooleanField() 4 执行数据库同步指令,添加字段的时候别忘了,该字段不能为空

PyMySQL and OrderedDict

人走茶凉 提交于 2019-11-30 18:37:31
I've been using PyMySQL for a while now and created my own wrapper that I'm used to to shorthand writing queries. Nonetheless I've been creating CSV files with OrderedDict because I need to keep the order the same but I realize that if I use PyMySQL for querying the database, I will not get the order the database is giving back. This is a little annoying for spot checking CSV files if I wanted to just dump stuff rather than hand write the orders. My question is, how do I use PyMySQL with OrderedDict? Currently my code is as follows: import pymysql conn = pymysql.connect(host='localhost', user=

MySQL的游标

纵然是瞬间 提交于 2019-11-30 16:51:17
python操作mysql 安装 python操作mysql数据库,主要就是通过pymysql模块 pip install pymysql 操作流程 1)建立数据库连接对象 conn 2)通过 conn 创建操作sql的 游标对象 3)编写sql交给 cursor 执行 4)如果是查询,通过 cursor对象 获取结果 5)操作完毕,端口操作与连接 代码步骤 注意 1.对记录增删改默认需要commit() 准备 import pymysql 一.建立连接 conn = pymysql.connect(user='root', passwd='root', database='t5') 二.获取游标对象 # 注:游标不设置参数,查询的结果就是数据元组,数据没有标识性 # 设置pymysql.cursors.DictCursor,查询的结果是字典,key是表的字段cursor = conn.cursor(pymysql.cursors.DictCursor) 三.增删改查 创建表 sql1="create table zx(id int)" cursor.execute(sql1) 增 #单条添加 sql2='insert into zx values(%s)' cursor.execute(sql2,(1,)) cursor.execute(sql2,(2,)) cursor

pymysql 基操全套

江枫思渺然 提交于 2019-11-30 15:12:33
pymysql:python操作mysql 什么是pymysql? pymysql是一个python连接操作mysql数据的一个模块。 没有他我们就不能和mysql连接所以安装... 安装 >: pip3 install pymysql 什么是Cursor游标? 游标(Cursor)是处理 数据 的一种方法,为了查看或者处理 结果集中 的数据,游标提供了在结果集中一次一行或者多行前进或向后浏览数据的能力。 设置pymysql.cursors.DictCursor,查询的结果是字典,key是表的字段 语法 ​ conn.commit() 提交 ​ conn.cursor(pymysql.cursors.DictCursor)设置游标对象 ​ cursor.execute(sql) 执行语sql语句 ​ conn.cursor(pymysql.cursors.DictCursor)设置DictCursor ​ 特殊fetch 取 ​ 查询需要通过fetchone操作 ​ fetchmany(个数) ​ fetchall()全部 增删改查 增删改查之前需要自己创建数据库 # 选取操作的模块 pymysql # pymysql连接数据库的必要参数:主机、端口、用户名、密码、数据库 # 注:pymysql不能提供创建数据库的服务,数据库要提前创建 import pymysql from