1

The code below inserts whatever data is typed into the text fields: name1 and phone1 into my database and I need to be able to type in stored data and retrieve it

def insert():

   name1 = textin.get()
   phone1 = textinn.get()
   conn = sqlite3.connect('D:\lastfm-dataset-360K\msd.sqlite3')
   with conn:
      cursor = conn.cursor()
      cursor.execute('INSERT INTO people(name, phone) VALUES(?,?)',(name1, phone1,))
      db.close()


but=Button(root,padx=2,pady=2,text='Submit',command=insert,font=('none 13 bold'))
but.place(x=60,y=100)

I need to retrieve the records by typing them into the same text field and then print them out. So far I have this but Im confused with the SQL.

def show():
   name1 = textin.get()
   phone1 = textinn.get()
   conn = sqlite3.connect('D:\lastfm-dataset-360K\msd.sqlite3')
   with conn:
      cursor = conn.cursor()
      cursor.execute('SELECT * FROM people(name, phone) VALUES(?,?)',(name1, phone1,))
for row in cursor.fetchall():
  print(row)


res=Button(root,padx=2,pady=2,text='Show',command=show,font=('none 13 bold'))
res.place(x=160,y=100)

1 Answer 1

1

Use:

cursor.execute('''INSERT INTO students(name, phone) VALUES(?,?)''',[(name1), (phone1)])

and:

cursor.execute("SELECT * FROM students WHERE name = ? AND phone = ?", [(name1),(phone1)])

Newcode:

def insert():
   name1 = textin.get()
   phone1 = textinn.get()
   conn = sqlite3.connect('D:\lastfm-dataset-360K\msd.sqlite3')
   with conn:
      cursor = conn.cursor()
      cursor.execute('''INSERT INTO students(name, phone) VALUES(?,?)''',[(name1), (phone1)])
      db.close()


def show():
   name1 = textin.get()
   phone1 = textinn.get()
   conn = sqlite3.connect('D:\lastfm-dataset-360K\msd.sqlite3')
   with conn:
      cursor = conn.cursor()
      cursor.execute("SELECT * FROM students WHERE name = ? AND phone = ?", [(name1),(phone1)])
      for row in cursor.fetchall():
          print(row)

res=Button(root,padx=2,pady=2,text='Show',command=show,font=('none 13 bold'))
res.place(x=160,y=100)
Sign up to request clarification or add additional context in comments.

1 Comment

db.close() .. what is db? currently thats an NameError?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.