mysql-python

Can I use with statement with MySQLdb.Connection object?

坚强是说给别人听的谎言 提交于 2019-12-19 18:29:12
问题 I wonder if I can use the context manager with with the Connection object, and write code like this: with MySQLdb.connect(...) as conn: do_something() Will the conn object be closed automatically after the block like with a file object ? Thanks. 回答1: MySQLdb does not support the context manager protocol. Roll your own, or use oursql instead. 来源: https://stackoverflow.com/questions/11751703/can-i-use-with-statement-with-mysqldb-connection-object

Is there a way to execute more than one query per string in MySQL-Python?

柔情痞子 提交于 2019-12-19 11:26:29
问题 I have some input which is a string containing more than one MySQL queries, such as USE some_db; SELECT * FROM some_table; . When I store this string as s and run cursor.execute(s) , it prints out 0L and when I do cursor.fetchall() it returns an empty tuple. It does not raise any exception, but it does not work either. When I split the string into two queries and open a cursor for each and execute, it works, but correctly splitting a string into queries is not easy, especially when there are

Is there any safe way to parameterize database names in MySQL queries?

一曲冷凌霜 提交于 2019-12-19 10:10:52
问题 I'm writing a little python script to help me automate the creation of mysql databases and associated accounts for my personal projects. Part of this script is a function that takes the database name as a string, then goes to create the database. def createDB(dbConn, dbName): import MySQLdb c = dbConn.cursor() query = """CREATE DATABASE %s;"""; c.execute(query, (dbName,)) This doesn't work because MySQL's CREATE DATABASE asks for the unquoted name of the database, as in CREATE DATAbASE test

AttributeError: 'tuple' object has no attribute 'encode' - MySQLdb Python

自作多情 提交于 2019-12-19 09:19:12
问题 I am writing a Python code with MySQL. My DB schema is as follows: ------------- | id | name | ------------- | | | | | | Following is a part of my code: cursor = self.conn.cursor() query = ("SELECT name FROM TABLENAME WHERE id = '%s'", (str(id.decode('unicode_escape').encode('ascii', 'utf-8'),))) cursor.execute(query) I am passing the ID from the URL. And getting the following error: AttributeError: 'tuple' object has no attribute 'encode' I get the results when I hard-code the valud of ID in

Python MySQLdb - Error 1045: Access denied for user

佐手、 提交于 2019-12-19 06:00:29
问题 I have a python code that goes like import MySQLdb import sys try: con = MySQLdb.connect(host = 'localhost',user = 'crawler',passwd = 'crawler', db = 'real_estate_analytics') #... rest of code ... except MySQLdb.Error, e: print "Error %d: %s" % (e.args[0],e.args[1]) sys.exit(1) The problem is that I'm getting the following error: Error 1045: Access denied for user 'crawler'@'localhost' (using password: YES) If I mysql on the terminal mysql -u crawler -pcrawler I can access the databases with

virtualenv that can find relocated libraires (like mysqlclient lib for MySQLdb)

回眸只為那壹抹淺笑 提交于 2019-12-19 00:57:10
问题 I would like to be able to share a virtualenv that is self contained, i.e. insure that all scripts installed are able to run directly without needing to set anything. E.g. I install a script in my virtualenv that uses MySQL-python. Unfortunately importing MySQLdb looks for a shared library (libmysqlclient.so) that was moved elsewhere than the standard directories on my system. Is there a way to guarantee that my virtualenv will find the library every time someone uses it? 回答1: I know this

How to create a mysql connection pool or any better way to initialize the multiple databases?

空扰寡人 提交于 2019-12-18 16:57:52
问题 In my code I am opening two mysql connections and using HTTP requests to insert data into database g.db = mysql.connector.connect(user=a ,password=password, host=localhost,database=mysq1) g.db1 = mysql.connector.connect(user=b,password=password, host=localhost,database=mysql2) @app.route('/user/<db>') def insert(db): #code for inserting data into mysql1 database #code for inserting data into mysql2 database I am making HTTP requests to select the databases. curl -i 'localhost:5000/user/mysql1

GCC-4.2 error on Mac OSX Mountain Lion, unable to install mysql-python

孤街醉人 提交于 2019-12-18 10:57:29
问题 I'm having trouble building MySQLdb on Mac OSX Mountain Lion. After upgrading to OSX Mountain Lion from OSX Lion, I have downloaded and installed Xcode 4.4 also. Then, I went to Preference > Downloads of the Xcode and installed Command Line Tools. I've downloaded MySQL-python ver. 1.2.3 from http://sourceforge.net/projects/mysql-python/ When I run python setup.py build I get below message: running build running build_py copying MySQLdb/release.py -> build/lib.macosx-10.6-intel-2.7/MySQLdb

“cryptography is required for sha256_password or caching_sha2_password”

痞子三分冷 提交于 2019-12-18 05:46:07
问题 Good day. Hope your all are well. Can someone help me with fix this? I'm new to the MySQL environment. I'm trying to connect to MySQL Database remotely. I used the following python code and got this error. Print(e) = "cryptography is required for sha256_password or caching_sha2_password" And have no idea how to solve the error. import pymysql as db HOST = "XXXXX.XXX.XX” PORT = XXXX USER = "my_username" PASSWORD = "my_password” DB = "db_name" try: connection = db.Connection(host=HOST, port

django.db.utils.operationalError: (2059,“Authentication Plugin 'caching_sha2_password'”)

99封情书 提交于 2019-12-18 05:11:21
问题 I am trying to connect my django project 'mysite' to mysql. I made a user in mysql and granted it all privileges to access the project. These are the changes I made to settings.py : DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'mysitedb', 'USER': 'username', 'PASSWORD': 'password', 'HOST': 'localhost', 'PORT': '', } } Now when I try to migrate the database using python3 manage.py makemigrations , I get the following error: django.db.utils.OperationalError: (2059,