0

I have a model and custom manager

model

class VideoDescription(models.Model):
    title_eng = models.CharField(max_length=120, unique=True)
    title_ru = models.CharField(max_length=120, unique=True)
    slug = models.SlugField(max_length=200, unique=True, blank=True)
    rating = models.IntegerField(default=0)
    pub_date_start = models.DateField()
    poster = models.ImageField(upload_to=get_poster_path)
    genre = models.CharField(validators=[validate_comma_separated_integer_list], max_length=10, default=0)
    description = models.TextField(blank=True)

objects = VideoDescriptionManager()

class VideoDescriptionManager(models.Manager):
def get_video_by_genre(self, genre):
    from django.db import connection
    with connection.cursor() as cursor:
        cursor.execute('''select m.id, m.title_eng, m.title_ru, m.slug, m.rating, m.pub_date_start,
        m.poster, m.genre, m.description, COUNT(*) from main_app_videodescription m
                        where genre like "%%%s%%";''', [genre])
        result_list = []
        for row in cursor.fetchall():
            p = self.model(id=row[0], title_eng=row[1], title_ru=row[2], slug=row[3], rating=row[4],
                           pub_date_start=row[5], poster=row[6], genre=row[7], description=row[8])
            p.num_responses = row[9]
            result_list.append(p)
        return result_list

And i've got a error:

django.db.utils.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 0, and there are 1 supplied.

Google talking me change [genre] to (genre,) in a raw sql query but it doesn't help. What can i do to fix it?

2
  • 1
    Why are you using raw sql here? You can use contains for the filter on genre. I don't understand why you have a COUNT(*) when you have no join or groupby. Commented Mar 21, 2017 at 17:16
  • @Alasdair Because i don't know about contains. Thank you very much. Commented Mar 21, 2017 at 17:38

1 Answer 1

2

sqlite does not use %s for parameters, it uses ?.

But either with sqlite or another db, you can't use the parameter substitution to insert a param in the middle of a string. You need to build up the string separately, and then insert it using the parameters. So:

param = "%{}%".format(genre)
cursor.execute('''select m.id, m.title_eng, m.title_ru, m.slug, m.rating, m.pub_date_start,
                  m.poster, m.genre, m.description, COUNT(*) from main_app_videodescription m
                        where genre like ?;''', [param])

Note that using .format removes the need to double-escape the percents.

Sign up to request clarification or add additional context in comments.

2 Comments

It's a Django cursor -- I thought it used %s for placeholders even if you are using sqlite.
Now i've got an error TypeError: not all arguments converted during string formatting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.