Executing Different Queries Using Mysql-python
I'm working with a remote db for importing data to my Django proyect's db. With the help of MySQLdb I've managed with ease to create an importing function like the following: def
Solution 1:
I think this is what you're looking for.
defconnect_and_get_data(query, data):
...
cursor.execute(query, data)
...
defget_data_about_first_amazing_topic(useful_string):
query = "SELECT ... FROM ... WHERE ... AND some_field=%s"
connect_and_get_data(query, ("one","two","three"))
...
But, if you're going to be making several queries quickly, it would be better to reuse your connection, since making too many connections can waste time.
...
CONNECTION = MySQLdb.connect(host=..., port=...,
user=..., passwd=..., db=...,
cursorclass=MySQLdb.cursors.DictCursor,
charset = "utf8")
cursor = CONNECTION.cursor()
cursor.execute("SELECT ... FROM ... WHERE ... AND some_field=%s", ("first", "amazing", "topic"))
first_result = cursor.fetchall()
cursor.execute("SELECT ... FROM ... WHERE ... AND some_field=%s", (("first", "amazing", "topic")))
second_result = cursor.fetchall()
cursor.close()
...
This will make your code perform much better.
Solution 2:
I am doing a web application project with Python and MYSQL, and I had the same error type :
MySQLdb._exceptions.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)").
All I did is changing the app configuration password to empty string ""
as follows:
app.config['MYSQL_PASSWORD'] = ""
And then I succeeded in logging in.
Post a Comment for "Executing Different Queries Using Mysql-python"