mysql-python

MySQL: Get column name or alias from query

本小妞迷上赌 提交于 2019-11-26 15:09:38
问题 I'm not asking for the SHOW COLUMNS command. I want to create an application that works similarly to heidisql, where you can specify an SQL query and when executed, returns a result set with rows and columns representing your query result. The column names in the result set should match your selected columns as defined in your SQL query. In my Python program (using MySQLdb ) my query returns only the row and column results, but not the column names. In the following example the column names

What's the most efficient way to convert a MySQL result set to a NumPy array?

▼魔方 西西 提交于 2019-11-26 14:08:47
问题 I'm using MySQLdb and Python. I have some basic queries such as this: c=db.cursor() c.execute("SELECT id, rating from video") results = c.fetchall() I need "results" to be a NumPy array, and I'm looking to be economical with my memory consumption. It seems like copying the data row by row would be incredibly inefficient (double the memory would be required). Is there a better way to convert MySQLdb query results into the NumPy array format? The reason I'm looking to use the NumPy array format

'pip install MySQL-python' fails with 'IndexError'

大兔子大兔子 提交于 2019-11-26 14:03:02
问题 I'm on OSX El Capitan, using Python 2.7 (Anaconda). Launching the command pip install MySQL-python yields: Collecting MySQL-python Using cached MySQL-python-1.2.5.zip Complete output from command python setup.py egg_info: Traceback (most recent call last): File "<string>", line 1, in <module> File "/private/var/folders/d2/gd004m2s35z5dlyz9mfn6sc40000gn/T/pip-build-FYvb_T/MySQL-python/setup.py", line 17, in <module> metadata, options = get_config() File "setup_posix.py", line 53, in get_config

How to use python mysqldb to insert many rows at once

十年热恋 提交于 2019-11-26 13:49:16
问题 I have a list of lists, e.g [['a','b'],['c','d']] . I have a table called T and two fields F1 , F2 . The first item in the field list maps to F1 , second to F2 . How can I insert rows for each inner list in a single command or call, rather than using a for loop like this? for i in [['a','b'],['c','d']]: c.execute("insert into T (F1,F2) values (%s, %s)", (i[0], i[1])) 回答1: From MySQLdb User's Guide: c.executemany( """INSERT INTO breakfast (name, spam, eggs, sausage, price) VALUES (%s, %s, %s,

What&#39;s the difference between MySQLdb, mysqlclient and MySQL connector/Python?

假如想象 提交于 2019-11-26 12:52:57
问题 So I\'ve been trying to do some database update with python and while setting up the whole dev environment, I came across these three things which made me dizzy. There\'s MySQLdb There\'s mysqlclient And then there\'s a mysql connector python What\'s each of them, the difference and where to use them? Thanks 回答1: MySQLdb is a thin python wrapper around C module which implements API for MySQL database. There was MySQLDb1 version of wrapper used some time ago and now it is considered to be a

Can&#39;t install mysql-python (newer versions) in Windows

萝らか妹 提交于 2019-11-26 12:48:53
问题 I have mysql-python v1.2.4 installed just fine on my machine (Windows 8). I am using Python 2.7. I always got this below error every time I try to upgrade to v1.2.5. (still happens as of v1.3.7) C:\\Users\\User\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\ 9.0\\VC\\Bin\\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_info=(1,2,5,\'fi nal\',1) -D__version__=1.2.5 \"-IC:\\Program Files (x86)\\MySQL\\MySQL Connector C 6. 0.2\\include\" -Ic:\\python27\\include -Ic:\

How can I use executemany to insert into MySQL a list of dictionaries in Python

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 12:32:06
问题 I\'m currently using MySQL and Python to scrape data from the web. Specifically, I am scraping table data and inserting it into my database. My current solution works, but I feel it is extremely inefficient and will most likely lock up my database if I don\'t rewrite the code. Here is what I currently use (partial code): itemBank = [] for row in rows: itemBank.append((tempRow2,tempRow1,tempRow3,tempRow4)) #append data #itemBank List of dictionaries representing data from each row of the table

Python and MySQLdb: substitution of table resulting in syntax error

柔情痞子 提交于 2019-11-26 12:17:45
问题 I need to dynamically change tables and variables from time to time, so I wrote a python method like this: selectQ =\"\"\"SELECT * FROM %s WHERE %s = %s;\"\"\" self.db.execute(selectQ,(self.table,self.columnSpecName,idKey,)) return self.db.store_result() However this results in a syntax error exception. I tried debugging it so I printed the variables in the method and filled them in manually, and that worked. So I am not sure what I am doing wrong ? Is it because I try to use a substitute for

Python MySQLdb issues (TypeError: %d format: a number is required, not str)

霸气de小男生 提交于 2019-11-26 12:10:17
问题 I am trying to do the following insert operation: cursor.execute(\"\"\" insert into tree (id,parent_id,level,description,code,start,end) values (%d,%d,%d,%s,%s,%f,%f) \"\"\", (1,1,1,\'abc\',\'def\',1,1) ) The structure of my MYSQL table is: id int(255), parent_id int(255), level int(11), description varchar(255), code varchar(255), start decimal(25,4), end decimal(25,4) However when I run my program, I get the error \" File \"/usr/lib/pymodules/python2.6/MySQLdb/cursors.py\", line 151, in

Print the actual query MySQLdb runs?

ⅰ亾dé卋堺 提交于 2019-11-26 12:05:05
问题 I\'m looking for a way to debug queries as they are executed and I was wondering if there is a way to have MySQLdb print out the actual query that it runs, after it has finished inserting the parameters and all that? From the documentation, it seems as if there is supposed to be a Cursor.info() call that will give information about the last query run, but this does not exist on my version (1.2.2). This seems like an obvious question, but for all my searching I haven\'t been able to find the