mysql-python

Execute .sql file in Python with MySQLdb

此生再无相见时 提交于 2019-12-05 10:50:47
I have a .sql file containing a bunch of SQL queries, with each query spanning multiple lines. I want to execute these queries in MySQL via Python using MySQLdb . sqlite3 has "a nonstandard shortcut" for this purpose called executescript() , but there doesn't seem to be any equivalent function in MySQLdb . I noticed this old question from 2 years ago which asks the same thing, but I found the answers unsatisfying. The answers are basically: Use subprocess to run the mysql command and send it your .sql file. This works, but it is rather inelegant, and it introduces unwanted complexity with

python mysql.connector DictCursor?

天涯浪子 提交于 2019-12-05 10:26:20
问题 In Python mysqldb I could declare a cursor as a dictionary cursor like this: cursor = db.cursor(MySQLdb.cursors.DictCursor) This would enable me to reference columns in the cursor loop by name like this: for row in cursor: # Using the cursor as iterator city = row["city"] state = row["state"] Is it possible to create a dictionary cursor using this MySQL connector? http://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-select.html Their example only returns a tuple. I

Python MySQLdb converters isn't working

 ̄綄美尐妖づ 提交于 2019-12-05 08:54:34
I'm trying to run an ETL script using python and MySQLdb but I'm stuck with the results from my initial extract query. The types returned are all Long and Decimal when I want Int and Float. I've searched around for a few hours trying to get an answer to this without any success. database = MySQLdb.connect(host='db',user='user', passwd='password', db='db123') database_cursor = database.cursor() database_query = ("SELECT id, siteId, campaignId, hour, sum(impressions) AS impressions, " "sum(clicks) AS clicks, sum(conversions) AS conversions, sum(costs/1000000) AS revenue " "FROM database.DM

ImportError: No module named mysql.base, in django project on Ubuntu 11.04 server

佐手、 提交于 2019-12-05 07:03:23
I am following the steps in the Django Book and got to the part where the authors explain hot wo set up a django project to use a database. I chose mysql. My settings in settings.py are: DATABASES = { 'default': { 'ENGINE': 'mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'mydatabase', # Or path to database file if using sqlite3. 'USER': 'myname', # Not used with sqlite3. 'PASSWORD': 'mypassword', # Not used with sqlite3. 'HOST': '', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '', # Set to empty string for default. Not used with sqlite3. } }

MySQLdb with multiple transaction per connection

风流意气都作罢 提交于 2019-12-05 05:49:03
Is it okay to use a single MySQLdb connection for multiple transactions without closing the connection between them? In other words, something like this: conn = MySQLdb.connect(host="1.2.3.4", port=1234, user="root", passwd="x", db="test") for i in range(10): try: cur = conn.cursor() query = "DELETE FROM SomeTable WHERE ID = %d" % i cur.execute(query) cur.close() conn.commit() except Exception: conn.rollback() conn.close() It seems to work okay, but I just wanted to double check. I think there is a misunderstanding about what constitutes a transaction here. Your example opens up one connection

CA SSL parameter for Python MySQLdb not working, but key does?

余生颓废 提交于 2019-12-05 01:35:22
问题 I'm trying to connect to a MySQL DB that requires SSL (only doing server authentication, not mutual). I have the server's CA saved as a .pem in the same directory I'm running the script from. My connection string looks like this: ssl_settings = {'ca':'ca.pem'} conn = MySQLdb.connect(host=HOST, user=USER, passwd=PASS, db=DB, ssl=ssl_settings} This results in "Error 2026: SSL connection error". However, if I change ssl_settings to: ssl_settings = {'key':'ca.pem'} The database connects just fine

error installing mysql-python and none of mentioned solutions worked

本秂侑毒 提交于 2019-12-05 01:13:12
问题 I just wanted to install Mysql-python, but every time errors occur. I have tried all mentioned solutions as installing wheel, using easy_install, renewing visual c++ for python, installing mysql connector, even installing latest mysql itself,and every other things came to my sight and my mind. I have python 2.7.11 on windows 10. and I have installed most of windows updates. always it occurs: easy_install Mysql-python Searching for Mysql-python Reading https://pypi.python.org/simple/Mysql

Compiling mysql-python on Windows with PIP

╄→гoц情女王★ 提交于 2019-12-05 00:18:41
So I have Python 2.7 and setuptools installed on my Windows 7 laptop. I also have Visual Studio 2008 Express installed and MySQL with dev tools. I am trying to install mysql-python via pip like: pip install mysql-python I am getting link errors: Creating library build\temp.win32-2.7\Release\_mysql.lib and object build\temp.win32-2.7\Release\_mysql.exp _mysql.obj : error LNK2019: unresolved external symbol _mysql_error@4 referenced in function __mysql_Exception _mysql.obj : error LNK2019: unresolved external symbol _mysql_errno@4 referenced in function __mysql_Exception _mysql.obj : error

TypeError: 'int' object is not iterable - Python

て烟熏妆下的殇ゞ 提交于 2019-12-05 00:09:54
问题 I got the following error: File "/home/ec2-user/test/test_stats.py", line 43, in get_test_ids_for_id cursor.execute("""select test_id from test_logs where id = %s """, (id)) File "/home/ec2-user/.etl/lib/python2.7/site-packages/MySQLdb/cursors.py", line 187, in execute query = query % tuple([db.literal(item) for item in args]) TypeError: 'int' object is not iterable Here's the section of my code I'm having trouble with: def get_test_ids_for_id(prod_mysql_conn, id): cursor = prod_mysql_conn

Is __enter__ and __exit__ behaviour for connection objects specified in the Python database API?

我的未来我决定 提交于 2019-12-04 19:01:51
问题 Background I recently discovered the Python with keyword and started seeing its potential usefulness for more prettily handling some scenarios where I'd previously have used try: ... finally: ... constructs. I immediately decided to try it out on the MySQLdb connection object in some code I was writing. I didn't bother reading up on how __enter__ and __exit__ behave in implementors of the Python database API, and naively expected the behaviour to be like that of file objects - all I was