mysql-python

Django queryset objects return None instead of 0 even though the database has 0 stored as the field value

笑着哭i 提交于 2019-12-01 13:35:55
I encountered a problem when I started using mysql-connector/python instead of mysqlclient for my Django project. Suddenly values stored as 0 in the database is turned to "None" in the resulting queryset, here's an example. queryset = Invoices.objects.all().filter(sold=0) print(queryset[0].sold) # Prints "None" Where the same statement would have printed 0 with mysqlclient. Is there a good way to fix this? I haven't gotten mysqlclient to work well with my current setup and would rather not have to track down all the calls depending on a 0-value and restructure it to handle Nonetypes. Edit with

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

孤者浪人 提交于 2019-12-01 11:18:10
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_db but my code that attempts to safely insert the user provided db name into the query creates: CREATE

How to setup up PYTHON_EGG_CACHE environment variable on Mac?

邮差的信 提交于 2019-12-01 11:17:22
问题 I am trying to setup Django to use MySQL. I am getting the following error when I type in localhost/mysite ExtractionError at / Can't extract file(s) to egg cache The following error occurred while trying to extract file(s) to the Python egg cache: [Errno 13] Permission denied: '/Library/WebServer/.python-eggs' The Python egg cache directory is currently set to: /Library/WebServer/.python-eggs Perhaps your account does not have write access to this directory? You can change the cache

best way to run python generator cleanup code

两盒软妹~` 提交于 2019-12-01 07:38:18
I'm trying to write a generator function that gets rows out of a database and returns them one at a time. However, I'm not sure if the cleanup code marked ** below executes as I think it does. If it does not, what is the best way to put cleanup code inside a generator itself that executes after the last yield statement? I looked at catching StopIteration but that seems to be done from the caller, not within the generator. def MYSQLSelectGenerator(stmt): ... try: myDB = MySQLdb.connect(host=..., port=..., user=..., passwd=..., db=...) dbc=myDB.cursor() dbc.execute(stmt) d = "asdf" while d is

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

风格不统一 提交于 2019-12-01 06:45:54
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 the query. But for some reason it is not working when I pass in a parameter. The query parameters

UnboundLocalError: local variable 'cursor' referenced before assignment

試著忘記壹切 提交于 2019-12-01 05:53:25
问题 So I am a newbie but working on a registration system form in flask/MYSQL I am receiving this error (UnboundLocalError: local variable 'cursor' referenced before assignment) After hours of playing with the code and research I need your help. This is my file, please let me know if theres anything else I need to share. thank you from flask import Flask, render_template, json, request from flask.ext.mysqldb import MySQL from werkzeug import generate_password_hash, check_password_hash app = Flask

best way to run python generator cleanup code

泄露秘密 提交于 2019-12-01 04:16:27
问题 I'm trying to write a generator function that gets rows out of a database and returns them one at a time. However, I'm not sure if the cleanup code marked ** below executes as I think it does. If it does not, what is the best way to put cleanup code inside a generator itself that executes after the last yield statement? I looked at catching StopIteration but that seems to be done from the caller, not within the generator. def MYSQLSelectGenerator(stmt): ... try: myDB = MySQLdb.connect(host=..

MySQLdb is extremely slow with large result sets

。_饼干妹妹 提交于 2019-12-01 04:13:39
I executed the following query both in phpMyAdmin & MySQLdb (python). SELECT *, (SELECT CONCAT(`id`, '|', `name`, '|', `image_code`) FROM `model_artist` WHERE `id` = `artist_id`) as artist_data, FIND_IN_SET("metallica", `searchable_words`) as find_0 FROM `model_song` HAVING find_0 phpMyAdmin said the query took 2ms . My python code said that using MySQLdb the query took 848ms (without even fetching the results). The python code: self.db = MySQLdb.connect(host="localhost", user="root", passwd="", db="ibeat") self.cur = self.db.cursor() millis = lambda: time.time() * 1000 start_time = millis()

Python MySQLdb - Error 1045: Access denied for user

百般思念 提交于 2019-12-01 04:13:25
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 no problem. mysql> show databases; +-----------------------+ | Database | +-----------------------+ |

Mock a MySQL database in Python

徘徊边缘 提交于 2019-12-01 03:19:59
I use Python 3.4 from the Anaconda distribution. Within this distribution, I found the pymysql library to connect to an existing MySQL database, which is located on another computer. import pymysql config = { 'user': 'my_user', 'passwd': 'my_passwd', 'host': 'my_host', 'port': my_port } try: cnx = pymysql.connect(**config) except pymysql.err.OperationalError : sys.exit("Invalid Input: Wrong username/database or password") I now want to write test code for my application, in which I want to create a very small database at the setUp of every test case, preferably in memory. However, when I try