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 7871645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:58:03+00:00 2026-06-03T01:58:03+00:00

Thanks for taking the time to read this. It’s going to be a long

  • 0

Thanks for taking the time to read this. It’s going to be a long post to explain the problem. I haven’t been able to find an answer in all the usual sources.

Problem:
I am having an issue with using the select statement with python to recall data from a table in a mysql database.

System and versions:

Linux ubuntu 2.6.38-14-generic #58-Ubuntu SMP Tue Mar 27 20:04:55 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
Python: 2.7.1+
MySql: Server version: 5.1.62-0ubuntu0.11.04.1 (Ubuntu)

Here’s the table:

mysql> describe hashes;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | varchar(20)  | NO   | PRI | NULL    |       |
| hash  | varbinary(4) | NO   | MUL | NULL    |       |
+-------+--------------+------+-----+---------+-------+

Here are responses that I want via a normal mysql query:

mysql> SELECT id FROM hashes WHERE hash='f'; 
+------+
| id   |
+------+
| 0x67 |
+------+

mysql> SELECT id FROM hashes WHERE hash='ff'; 
+--------+
| id     |
+--------+
| 0x6700 |
+--------+

As before, these are the responses that are expected and how I designed the DB.

My code:

import mysql.connector
from database import login_info
import sys
db = mysql.connector.Connect(**login_info)
cursor = db.cursor()
data = 'ff'
cursor.execute("""SELECT
            * FROM hashes
            WHERE hash=%s""",
            (data))

rows = cursor.fetchall()
print rows
for row in rows:
        print row[0]

This returns the result I expect:

[(u'0x67', 'f')]
0x67

If I change data to :
data = ‘ff’
I receive the following error:

Traceback (most recent call last):
 File "test.py", line 11, in <module>
    (data))
  File "/usr/local/lib/python2.7/dist-packages/mysql_connector_python-0.3.2_devel-    py2.7.egg/mysql/connector/cursor.py", line 310, in execute
    "Wrong number of arguments during string formatting")
mysql.connector.errors.ProgrammingError: Wrong number of arguments during string formatting

OK. So, I add a string formatting character to my SQL statement as so:

cursor.execute("""SELECT
            * FROM hashes
            WHERE hash=%s%s""",
            (data))

And I get the following response:

[(u'0x665aa6', "f'f")]
0x665aa6

and it should by 0x6700.

I know that I should be passing the data with one %s character. That is how I built my database table, using one %s per variable:

cursor.execute("""
INSERT INTO hashes (id, hash) 
VALUES (%s, %s)""", (k, hash))

Any ideas how to fix this?

Thanks.

  • 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-03T01:58:04+00:00Added an answer on June 3, 2026 at 1:58 am

    Your execute statement doesn’t seem quite correct. My understanding is that it should follow the pattern cursor.execute( <select statement string>, <tuple>) and by putting only a single value in the tuple location it is actually just a string. To make the second argument the correct data type you need to put a comma in there, so your statement would look like:

    cursor.execute("""SELECT
                * FROM hashes
                WHERE hash=%s""",
                (data, ))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

thanks for taking the time out to read this post. I'm having trouble trying
Problem: Thanks for taking the time to read this. I'm having a problem which
First of all, thanks for taking the time to read this. I've been working
Thanks for taking the time to read this. I have an unknown number of
Firstly, thanks for taking the time to read this and possibly comment on it.
Thanks everyone for taking the time to read this. I have styled my navigation
First, thanks in advance for taking the time to read through this. I have
Thanks for taking time to read this question. I am designing a 3-tier solution
thanks for taking the time to read this. I have a JavaScript (jQuery) navigation
Thanks in advance for taking the time to read this. Apologies that it is

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.