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
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,046 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  restrict user input to numerical values MCL169 2 906 Apr-08-2023, 05:40 PM
Last Post: MCL169
  user input values into list of lists tauros73 3 1,062 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 1,070 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 1,076 Dec-11-2022, 07:39 PM
Last Post: snippsat
Sad how to validate user input from database johnconar 3 1,901 Sep-11-2022, 12:36 PM
Last Post: ndc85430
  How to split the input taken from user into a single character? mHosseinDS86 3 1,164 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  Use pexpect to send user input alisha17 0 1,880 May-10-2022, 02:44 AM
Last Post: alisha17
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,470 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Matplotlib - close multple plots with user input Positron79 0 1,738 Dec-01-2021, 05:26 PM
Last Post: Positron79

Forum Jump:

User Panel Messages

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