Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sqlite3 user input
#1
Hi everyone, Im just learning how use the SQlite3 and everything seems
pretty straight forward, but I'm having trouble getting it to SELECT from a given user input.

This creates the database fine:
conn = sqlite3.connect('SVM_PROJECTS.db')
c = conn.cursor()

c.execute('CREATE TABLE IF NOT EXISTS Project_Info (ProjectNumber TEXT, ProjectName TEXT, ProjectAddress TEXT)')

c.execute("INSERT INTO Project_Info(ProjectNumber, ProjectName, ProjectAddress) VALUES(?,?,?)",
    (Project_Number, Project_Name, Project_Address))
conn.commit()
But this is were I would like a user to enter a Project Number and pull the row from the database.

import sqlite3

Number = input("What is the Project Number")

conn = sqlite3.connect('SVM_PROJECTS.db')
c = conn.cursor()

c.execute('SELECT * FROM Project_Info WHERE ProjectNumber=?',(Number))
for row in c.fetchall():
    Project_Name = row[1]
Error:
Traceback (most recent call last): File "/Users/barnettchenault/Desktop/test - AVA sqlite read.py", line 8, in <module> c.execute('SELECT * FROM Project_Info WHERE ProjectNumber=?',(Number)) sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 7 supplied.
Im sure its just in the formatting I just can't find the correct arrangement.
Reply
#2
which version of python are you running?
In python 3, (or 2.7 with proper setup), you can do the followning
sql_cmd = 'SELECT * FROM Project_Info WHERE ProjectNumber={}'.format(Number)
c.execute(sql_cmd)
Reply
#3
(Jan-21-2018, 01:36 AM)Larz60+ Wrote: sql_cmd = 'SELECT * FROM Project_Info WHERE ProjectNumber={}'.format(Number)
c.execute(sql_cmd)

Lars, Im running Python 3. When I run that revision I seem to be missing something.
as there is no error but it doesn't return anything

import sqlite3

Number = input("What is the Project Number")

conn = sqlite3.connect('SVM_PROJECTS.db')
c = conn.cursor()

sql_cmd = 'SELECT * FROM Project_Info WHERE ProjectNumber={}'.format(Number)
c.execute(sql_cmd)
for row in c.fetchall():
    Project_Number = row[0]
    Project_Name = row[1]
    print(Project_Name)

Larz -

Nevermind I figured it out I needed an extra set of quotations around the brackets.
sql_cmd = "SELECT * FROM Project_Info WHERE ProjectNumber='{}'".format(Number)
Reply
#4
Perhaps your database is not ok.
do you know how to use sqlite3 from command line?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to revert back to a previous line from user input Sharkenn64u 2 943 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  User input with while loops chizzy101010 2 5,409 Aug-25-2024, 06:00 PM
Last Post: chizzy101010
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 2,509 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  restrict user input to numerical values MCL169 2 1,864 Apr-08-2023, 05:40 PM
Last Post: MCL169
  user input values into list of lists tauros73 3 1,980 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 2,358 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 2,319 Dec-11-2022, 07:39 PM
Last Post: snippsat
Sad how to validate user input from database johnconar 3 4,060 Sep-11-2022, 12:36 PM
Last Post: ndc85430
  How to split the input taken from user into a single character? mHosseinDS86 3 2,143 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  Use pexpect to send user input alisha17 0 3,222 May-10-2022, 02:44 AM
Last Post: alisha17

Forum Jump:

User Panel Messages

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