so I am trying to make a very simple database, I am using sqlite3 and I am ever so new to programming with python and other things. My goal is to be able to input a name which is a section in my table and have it display the row that that name is found in. I have found tutorials where they just do normal data as opposed to dynamic searches. I don't have much of an example to show, but here is where I'm working from
import sqlite3
conn = sqlite3.connect('tutorial.db')
c = conn.cursor()
def create_table():
c.execute("CREATE TABLE nameinfo(name TEXT, age REAL, color TEXT)")
def enter_dynamic_data():
name = input("What\s their name? ")
age = float(input("How old are they? "))
color = input("What\'s their favorite color? ")
c.execute("INSERT INTO nameinfo(name, age, color) VALUES (?, ?, ?)", (name, age, color))
conn.commit()
def read_from_database():
sql = ("SELECT * FROM nameinfo")
for row in c.execute(sql): #It names off the names that I can select from
print(row[0])
inp = input("Who would you like to know more about? ")
for row in c.execute(sql): #Where I plan to have it only show a specific
row, being the name age and favorite color of a person
print(row)
read_from_database()
conn.close()
You can use SELECT * FROM nameinfo WHERE name = ?
. Complete code:
import sqlite3
conn = sqlite3.connect(':memory:')
c = conn.cursor()
def create_table():
c.execute("CREATE TABLE nameinfo(name TEXT, age REAL, color TEXT)")
def enter_dynamic_data(name, age, color):
c.execute("INSERT INTO nameinfo(name, age, color) VALUES (?, ?, ?)", (name, age, color))
create_table()
enter_dynamic_data("jack", 20, "green")
enter_dynamic_data("jill", 30, "red")
conn.commit()
def read_from_database():
sql = "SELECT * FROM nameinfo"
for row in c.execute(sql): # It names off the names that I can select from
print(row[0])
name = "jack"
sql = "SELECT * FROM nameinfo WHERE name = ?"
for row in c.execute(sql, (name,)):
print(row)
read_from_database()
conn.close()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.