Python Forum
what is wrong with my code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what is wrong with my code
#1
import sqlite3
id1 = input('Enter the book id: ')
conn = sqlite3.connect("KJV+.SQLite3")
c = conn.cursor()
c.execute("select * from verses where book_number = ? and chapter = 1 and verse = 1" %  id1)
text = c.fetchmany(1)
print(text)
                   
error:
Error:
c.execute("select * from verses where book_number = ? and chapter = 1 and verse = 1" % id1) TypeError: not all arguments converted during string formatting
Reply
#2
Test the line in the Python console.
>>> id1 = "Hello"
>>> print("select * from verses where book_number = ? and chapter = 1 and verse = 1" % id1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>>
>>> # Use the right syntax.
>>> print("select * from verses where book_number = %s and chapter = 1 and verse = 1" % id1)
select * from verses where book_number = Hello and chapter = 1 and verse = 1
>>>
>>> # Still not correct if id1 is a string.
>>> print("select * from verses where book_number = '%s' and chapter = 1 and verse = 1" % id1)
select * from verses where book_number = 'Hello' and chapter = 1 and verse = 1
>>>
>>> # Or use the modern f-string.
>>> print(f"select * from verses where book_number = '{id1}' and chapter = 1 and verse = 1")
select * from verses where book_number = 'Hello' and chapter = 1 and verse = 1
>>> 
>>> But the advised method for Sqlite3 is this:
>>> c.execute("select * from verses where book_number = ? and chapter = 1 and verse = 1", (id1,))
Reply
#3
solved by this
import sqlite3



id1 = input('Enter the book id: ')
c = input('Enter the chapter: ')
p = input('Enter the position: ')


conn = sqlite3.connect("KJV+.SQLite3")
c = conn.cursor()

c.execute("select * from verses where book_number = ? and chapter = 1 and verse = 1",(id1,))

text = c.fetchmany(1)
print(text)
but when i try too add more arguments , error happens
import sqlite3



id1 = input('Enter the book id: ')
c = input('Enter the chapter: ')
p = input('Enter the position: ')


conn = sqlite3.connect("KJV+.SQLite3")
c = conn.cursor()

c.execute("select * from verses where book_number = ? and chapter = ? and verse = ?",(id1,c,p))

text = c.fetchmany(1)
print(text)
Error:
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 3 supplied.
Yoriz write Apr-07-2022, 11:51 AM:
Please post errors (In their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#4
You are using the variable "c" for different things:
c = input('Enter the chapter: ')
...
c = conn.cursor()
This does not explain the error message, but you should repair this first.

Also: when an error occurs, please show the complete error message.
Reply
#5
what you said is right, problem solved, thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  im not sure what ive done wrong code doesnt run dgizzly 3 1,385 Nov-16-2022, 03:02 AM
Last Post: deanhystad
  What's wrong with my code? NeedHelpPython 4 2,247 Oct-22-2021, 07:59 PM
Last Post: Yoriz
  Help with my code due 11:59 pm, can you tell me where I went wrong and help fix it? shirleylam852 1 2,687 Dec-09-2020, 06:37 AM
Last Post: stranac
  I am getting an incorrect average, and not sure why? What's wrong with my code? shirleylam852 8 4,717 Nov-20-2020, 05:32 AM
Last Post: deanhystad
  Something is Wrong with my code susmith552 4 3,053 Nov-28-2019, 02:16 AM
Last Post: susmith552
  What is wrong with my code? Than999 1 2,392 Nov-10-2019, 08:59 PM
Last Post: ichabod801
  Wrong output on my code. JTNA 2 7,930 Apr-04-2019, 01:55 PM
Last Post: JTNA
  Why is this code wrong? Lemmy 4 5,196 Apr-05-2018, 03:46 PM
Last Post: Lemmy
  What's wrong with my code and visuals for python? beginnercoder04 2 2,828 Mar-17-2018, 01:06 AM
Last Post: beginnercoder04
  whats wrong with my code? syntax errors r6lay 5 6,510 Mar-16-2017, 04:14 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020