Forums

django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax;... )

Hi,

I'm working with Python 3 and I have installed "MySQL-for-Python-3" in order to work with my SQL databases.

I have created the model in model.py:

from django.db import models
from django.contrib.auth.models import User
from django.db.models import signals

class Client(models.Model):
    user        = models.OneToOneField(User, null=True, blank=True)
    birthday    = models.DateField(null=True, blank=True)
    name        = models.CharField(max_length = 100)

    def __unicode__(self):
        return self.name

def create_reg_user_callback(sender, instance, **kwargs):
    reg, new = Client.objects.get_or_create(user = instance)
signals.post_save.connect(create_reg_user_callback, User)

as well as the admin.py

from django.contrib import admin
from project_vitamine.reg.models import Client

admin.site.register(Client)

Now if I try to go to maypage.admin I get the rror

django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s  AND `django_session`.`expire_date` > %s )' at line 1")

What am I missing here?

Are you using a Python 3 version of Django? That looks like it's trying to use old-style string interpolation.

When I created the Django Project via the user interface I selected Python 3.3 and Django 1.7. Do I have to change something?

No, that should be good. It's just that many of our customers run Django in a virtualenv and sometimes get the versions wrong.

Actually, it looks like you're using the wrong connector for the database. I have fixed the help page so it's less confusing (I got caught out by it, too)

Thanks for your respons. Yes I saw the post on your help page. I have successfully installed "mysql-connector-python-1.1.6.tar.gz" but when I run

python manage.py syncdb

I get the error

django.core.exceptions.ImproperlyConfigured: 'mysql.connector.django' isn't an available database backend. 
Try using django.db.backends.XXX, where XXX is one of:
'dummy', 'mysql', 'oracle', 'postgresql', 'postgresql_psycopg2', 'sqlite3'
Error was: cannot import name six

Any other idea?

You're running under Python 3, and I assume you also installed the connector using pip3.3, so your manage.py call should be:

python3.3 manage.py syncdb

Alternatively,

chmod +x manage.py

then you can just go

./manage.py syncdb

Hi Glenn,

Thanks for your support. Yes I tried already with

python3.3 manage.py syncdb

however now I get a new error message saying:

TypeError: sql_for_inline_foreign_key_references() takes 4 positional arguments but 5 were given

I checked that error message already and apparently one has to change the creation.py in the python-connector-django package. See here:

http://stackoverflow.com/a/21357079

Now the next problem is that the file is set to "readonly". I guess I don't have root access?

I got that error when I's installed mysql-connector to the wrong Python version. I thought it was just a finger slip, but it looks like the help page had the wrong pip version on the suggested command line. I have fixed the help page. You need to do:

pip3.3 install --user https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-1.1.6.tar.gz

Unbelievable!! Glenn, you saved my day!

Everything works perfect ... until my next forum post ... ;-)

Cheers from Luxembourg

Excellent, thanks for confirming that!

Update: the package for MySQL in Python 3.x has now changed. If you're not using a virtualenv, then it's already installed. If you are using a virtualenv, start a bash console inside it and then run:

pip install mysqlclient