mysql-python

How to update multiple rows with single MySQL query in python?

怎甘沉沦 提交于 2019-11-26 21:03:43
问题 #!/usr/bin/python # -*- coding: utf-8 -*- import MySQLdb as mdb con = mdb.connect('localhost', 'root', 'root', 'kuis') with con: cur = con.cursor() cur.execute("UPDATE Writers SET Name = %s WHERE Id = %s ", ("new_value" , "3")) print "Number of rows updated:", cur.rowcount With above code the third row's value of the table Writers in the database kuis gets updated with new_value and the output will be Number od rows updated: 1 How am I supposed to update multiple rows at the same time? 回答1:

Accessing a MySQL connection pool from Python multiprocessing

[亡魂溺海] 提交于 2019-11-26 20:29:20
问题 I'm trying to set up a MySQL connection pool and have my worker processes access the already established pool instead of setting up a new connection each time. I'm confused if I should pass the database cursor to each process, or if there's some other way to do this? Shouldn't MySql.connector do the pooling automatically? When I check my log files, many, many connections are opened and closed ... one for each process. My code looks something like this: PATH = "/tmp" class DB(object): def _

python setup.py egg_info mysqlclient

随声附和 提交于 2019-11-26 19:45:51
问题 Trying to install mysqlclient using pip3 on Python 3.6.0 $ pip3 install mysqlclient Collecting mysqlclient Using cached mysqlclient-1.3.10.tar.gz Complete output from command python setup.py egg_info: Traceback (most recent call last): File "<string>", line 1, in <module> File "/private/var/folders/3k/08g3yx_12kg99kyfs989md600000gn/T/pip-build-1qv_89jc/mysqlclient/setup.py", line 17, in <module> metadata, options = get_config() File "/private/var/folders/3k/08g3yx_12kg99kyfs989md600000gn/T

Replacing Pandas or Numpy Nan with a None to use with MysqlDB

余生长醉 提交于 2019-11-26 18:48:49
问题 I am trying to write a Pandas dataframe (or can use a numpy array) to a mysql database using MysqlDB . MysqlDB doesn't seem understand 'nan' and my database throws out an error saying nan is not in the field list. I need to find a way to convert the 'nan' into a NoneType. Any ideas? 回答1: @bogatron has it right, you can use where, it's worth noting that you can do this natively in pandas: df1 = df.where((pd.notnull(df)), None) Note: this changes the dtype of all columns to object . Example: In

When to close cursors using MySQLdb

北城以北 提交于 2019-11-26 18:30:20
I'm building a WSGI web app and I have a MySQL database. I'm using MySQLdb, which provides cursors for executing statements and getting results. What is the standard practice for getting and closing cursors? In particular, how long should my cursors last? Should I get a new cursor for each transaction? I believe you need to close the cursor before committing the connection. Is there any significant advantage to finding sets of transactions that don't require intermediate commits so that you don't have to get new cursors for each transaction? Is there a lot of overhead for getting new cursors,

How to insert pandas dataframe via mysqldb into database?

試著忘記壹切 提交于 2019-11-26 18:26:12
I can connect to my local mysql database from python, and I can create, select from, and insert individual rows. My question is: can I directly instruct mysqldb to take an entire dataframe and insert it into an existing table, or do I need to iterate over the rows? In either case, what would the python script look like for a very simple table with ID and two data columns, and a matching dataframe? Andy Hayden Update: There is now a to_sql method, which is the preferred way to do this, rather than write_frame : df.to_sql(con=con, name='table_name_for_df', if_exists='replace', flavor='mysql')

How to disable query cache with mysql.connector

房东的猫 提交于 2019-11-26 17:01:31
问题 I'm connecting mysql on my Kivy application. import mysql.connector con = mysql.connector.Connect(host='XXX', port=XXX, user='XXX', password='XXX', database='XXX') cur = con.cursor() db = cur.execute("""select SELECT SQL_NO_CACHE * from abc""") data = cur.fetchall() print (data) After inserting or deleting on table abc from another connection; i call the same query on python; but data is not updating. I add the query "SET SESSION query_cache_type = OFF;" before select query, but it didn't

get raw decimal value from mysqldb query

泄露秘密 提交于 2019-11-26 16:59:30
问题 I am executing a mysql query in python using the MySQLdb package. The code looks something like this: c=db.cursor() c.execute("""select * from table""") output = [] for row in c: output.append(row[4]) where row[4] contains a decimal value that I want to store in the output list. The problem is every value that I am getting looks like this: Decimal('XX.XX') where all I want in the output list is XX.XX. At the end of the script, my output list looks like this: [Decimal('10.02'), Decimal('20.24'

Install mysql-python (Windows)

大憨熊 提交于 2019-11-26 16:16:11
I've spent hours trying to make Django work on my computer. The problem is that I can't install the mysql-python package. I'm running Windows 7 64bit. This is what I've tried: I have downloaded easy_install I have downloaded Cygwin64 to be able to run Linux commands (Win cmd was driving me crazy) I have typed in: easy_install mysql-python (gave me an error message saying it can't find vcvarsall.bat) I have downloaded Visual Studio 2010. However, I uninstalled it since I found out that I had some other version of it already (it didn't solve the problem) I have googled this problem like a

Python MySQLDB: Get the result of fetchall in a list

拥有回忆 提交于 2019-11-26 16:06:07
问题 I would like to get the result of the fetchall operation in a list instead of tuple of tuple or tuple of dictionaries. For example, cursor = connection.cursor() #Cursor could be a normal cursor or dict cursor query = "Select id from bs" cursor.execute(query) row = cursor.fetchall() Now, the problem is the resultant row is either ((123,),(234,)) or ({'id':123}, {'id':234}) What I am looking for is (123,234) or [123,234]. Be best if I can save on parsing the resulset. Thanks in advance 回答1: And