mysql-python

Inserting multiple dictionary objects into a MySQL database using python and MySQLdb

谁说胖子不能爱 提交于 2019-12-04 18:48:29
I have been struggling with this for hours and I feel like crying now as I'm unable to fathom out what is happening. Here is a simplified version of my data: mydata = [ { 'id': 123, 'thing': 'ghi', 'value': 1 }, { 'id': 456, 'thing': 'xyz', 'value': 0 } ] This is the code I have: import MySQLdb as mdb con = None con = mdb.connect('localhost', 'testing', 'anothervalue', 'andanother'); cur = con.cursor() sql = "INSERT INTO `tablename` ( `id`, `thing`, `value` ) VALUES ( %(id)s, %(thing)s, %(value)s )" cur.executemany( sql, ( mine for mine in mydata ) ) con.close() What I expected to happen was

How to get matched Rows from MySQLdb.cursors.Cursor python2.6

元气小坏坏 提交于 2019-12-04 17:17:17
I'm working with python2.6 and MySQLdb. I have a table with this data +----+--------+ | id | status | +----+--------+ | 1 | A | | 2 | B | | 3 | B | +----+--------+ I want to do an mysql update like this example: UPDATE my_table SET status = "A" where id in (1,2,3,10001); Query OK, 2 rows affected (0.03 sec) Rows matched: 3 Changed: 2 Warnings: 0 And I need to know if all the ids in the update exits in the database. My idea to get this information was to compare the number of items I tried to update vs the number of matched rows. In the example the numbers are 4 vs 3. The problem is that i don

Twisted, MySQLdb and (2006, 'MySQL server has gone away') using Twisted adbapi

隐身守侯 提交于 2019-12-04 16:46:32
In twisted I am a perpetual event loop that is always lookig for a new query to run It polls a SQS queue and are are times where time between queres is long enough to time out and this is the error I get when a new query arrives... MySQLdb _mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away') here is my connection self.pool = adbapi.ConnectionPool("MySQLdb", self.parms['host'], self.parms['username'], self.parms['password'], self.parms['database']) Here is the logic I use to try and solve the problem. try: d = self.pool.runQuery(query, ()) except: self.pool = adbapi

Use Python list in SQL query for column names

百般思念 提交于 2019-12-04 16:44:30
I have a bunch of column names in a Python list. Now I need to use that list as the column names in a SELECT statement. How can I do that? pythonlist = ['one', 'two', 'three'] SELECT pythonlist FROM data; So far I have: sql = '''SELECT %s FROM data WHERE name = %s INTO OUTFILE filename''' cur.execute(sql,(pythonlist,name)) You cannot pass list of columns to select as a parameter to cur.execute . It should be part of your SQL expression, something like: sql = "SELECT " + ",".join(pythonlist) + " FROM data WHERE name = %s INTO OUTFILE filename" cur.execute(sql, (name,)) One thing to be aware of

Why doesn't the MySQLdb Connection context manager close the cursor?

两盒软妹~` 提交于 2019-12-04 15:54:28
问题 MySQLdb Connections have a rudimentary context manager that creates a cursor on enter , either rolls back or commits on exit , and implicitly doesn't suppress exceptions. From the Connection source: def __enter__(self): if self.get_autocommit(): self.query("BEGIN") return self.cursor() def __exit__(self, exc, value, tb): if exc: self.rollback() else: self.commit() So, does anyone know why the cursor isn't closed on exit? At first, I assumed it was because closing the cursor didn't do anything

Installing MySQLdb for Django on Mac OS X 10.6 Snow Leopard with MAMP

这一生的挚爱 提交于 2019-12-04 11:14:53
So I know this is not a new topic, but its one that nobody has seemed to be able to solve, at least not for Python 2.6 / Snow Leopard. (The Leopard fixes I've found aren't applicable to Snow Leopard.) Situation: I'm trying to get Django installed locally on my Mac OS X Snow Leopard laptop. (10.6.7) I have Python 2.6.1, which is what came preinstalled with Snow Leopard, MySQL-python 1.2.3, and MAMP 1.9.6. All are latest current versions. Without making any changes to the MySQLdb package, if I run python setup.py build I get hundreds or more errors, the first of which are: $ python setup.py

error: command 'cc' failed with exit status 1 - MySQLdb installation on MAC

北城以北 提交于 2019-12-04 08:41:14
问题 I am new to Mac and i am trying to install MySQLdb for Python on MAC but after following the steps mentioned on http://www.tutorialspoint.com/python/python_database_access.htm, i received an error after running $ python setup.py build Error: clang: warning: argument unused during compilation: '-mno-fused-madd' _mysql.c:44:10: fatal error: 'my_config.h' file not found #include "my_config.h" ^ 1 error generated. error: command 'cc' failed with exit status 1 Note: My "mysql_config" path is

UPDATE or INSERT MySQL Python

99封情书 提交于 2019-12-04 06:47:26
I need to update a row if a record already exists or create a new one if it dosen't. I undersant ON DUPLICATE KEY will accomplish this using MYSQLdb, however I'm having trouble getting it working. My code is below cursor = database.cursor() cursor.execute("INSERT INTO userfan (user_id, number, round VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE user_id =%s, number=%s, round=%s", (user_id, number, round)) database.commit() primary key is user_id A parenthesis was missiing. You can also use the VALUES(column) in the ON DUPLICATE KEY UPDATE section of the statement: cursor = database.cursor()

Flask_SQLAlchemy, MySQL, store Swedish characters å, ä, ö?

孤人 提交于 2019-12-04 05:45:33
问题 I can't sto re Swedish characters in MySQL by using Flask_SQLAlchemy :( I have tried to find a solution for a week now and I really need help as it feels like I have reached a dead end. I think it could be something wrong with the version compatibilities abong my tools, but I don't hope so! I am trying to build an website using Flask, Flask_SQLAlchemy and MySQL (5.5.3). If this is unsolvable, I am considering changing Flask_SQLAlchemy to something else.. (I have taken one course in

Django: how to install mysql/connector python with pip3

被刻印的时光 ゝ 提交于 2019-12-04 04:16:09
I am working on projects based on Django 1.7 and Python 3.4. However, I had problems installing MySQL/Connector Python with pip3. According to this document , MySQL/Connector Python supports Python 3. I used to install MySQL-python in Python with command pip install MySQL-python . This download page only provides .deb files for installation on Ubuntu (btw, the installation also has conflict problems ) I tried to install with: pip3 install mysql-connector-python --allow-external mysql-connector-python No error messages. But when I run the Django app, I got the following error message: django