Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8038629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:17:42+00:00 2026-06-05T03:17:42+00:00

I have an management command from django.db import connection from django.core.management.base import BaseCommand class

  • 0

I have an management command

from django.db import connection
from django.core.management.base import BaseCommand   

class Command(BaseCommand):
    args = "[id]"

    def handle(self, id, **options):
    ids = []
    if '-' in id:
        splitted = id.split('-')
        for n in range(int(splitted[0]), int(splitted[1])+1):
            ids.append(n)
    else:
        ids.append(id)
    c = connection.cursor()
    c.execute('SELECT content FROM log WHERE id IN %s', [ids])

I can run this by entering command name and specify paramater (id)

When I run command 200-205, the for loop returns a list:

ids =[200, 201, 202, 203, 204, 205]

And puts this in the query:

SELECT content FROM log WHERE id IN (200, 201, 202, 203, 204, 205)

Briljant!

When I run command 200 ids will be:

ids = [200]

Executing the query returns an error:

(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 '))' at line 1")

Seems like SELECT content FROM log WHERE id IN (200) is not working while it should.

What am I doing wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:17:45+00:00Added an answer on June 5, 2026 at 3:17 am

    Your code looks OK to me, but I’m not fluent in those Django libraries. A couple of (perhaps obvious) workaround ideas: (1) construct two different SQL statements to be executed; in the one-ID case, it’s a simple SELECT content FROM log WHERE id = (2) again for the one-ID case, make an array with two instances of the desired ID, so that Django constructs this SQL: SELECT content FROM log WHERE id IN (200, 200). In other words,

    else:
        ids.append(id)
        ids.append(id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run a Django management command from cron. I am using
I have a model project_phase : from django.db import models from django.utils import simplejson
I have a Django management command that makes thousands of TCP/UDP requests. I've used
Problem: to find the Python module execute_manager in the class django.core.management. The question is
I have a Django model where I'm importing a number of items: from django.db
Having issues getting django custom commands to work. From django documetation , have placed
I have django-registration installed. I just updated my Python installation from 2.5 to 2.7.
I want to run manage.py sqldiff myapp (command from django extension), but I get
I have a a number of management commands in my Django application. I'd like
I have a stored procedure that executes much faster from Sql Server Management Studio

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.