If I make a database and add several rows I can retrieve them using their id, but if I now DELETE a row it appears to only remove the row contents but leave the id, so it can be retrieved, using fetchone, but now contains None which I can't then find find a way to either insert or modify data in that row or renumber the id's to match the now valid rows.
Am I doing something wrong ?
The attached code will allow saving, reading, modifying and deleting rows. Run it, enter a name and press SAVE , repeat a few times, then use VIEW NEXT to recall the entries, now DELETE the entry for ID 2 and see it is now blank and can't be modified to put data back in it.
Is there a way to INSERT/SAVE at a specific user_id ?
Thanks
Code: Select all
#!/usr/bin/python3
import tkinter as tk
import os
import sqlite3
# define variables
dbname = "mydb7"
age1 = ""
height1 = ""
mobile1 = ""
global db
global cursor
global max_id
user_id = 0
if os.path.exists(dbname) == False:
db = sqlite3.connect(dbname)
cursor = db.cursor()
cursor.execute('''
CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT,
phone TEXT, email TEXT, dob TEXT, age TEXT, height TEXT, mobile TEXT)
''')
db.commit()
db.close()
db = sqlite3.connect(dbname)
cursor = db.cursor()
# determine number of rows in table
cursor.execute('''INSERT INTO users(name, phone, email, dob, age, height, mobile)
VALUES(?,?,?,?,?,?,?)''', ("test","test","test","test","test","test","test"))
db.commit()
max_id = cursor.lastrowid
cursor.execute('''DELETE FROM users WHERE id = ? ''', (max_id,))
db.commit()
class MainApplication(tk.Frame):
def __init__(self, master):
self.master = master
tk.Frame.__init__(self, self.master)
self.dataure_gui()
self.create_widgets()
def dataure_gui(self):
self.master.title('Data EDIT Form')
self.master.geometry('600x240')
self.master.resizable(0, 0)
def create_widgets(self):
self.create_frames()
self.create_entries()
self.create_labels()
self.create_buttons()
def create_frames(self):
self.Frame10 = tk.Frame(width=600, height=240)
self.Frame10.grid_propagate(0)
self.Frame10.grid(row=0, column=0)
def create_entries(self):
global E1,E2,E3,E4,E5,E6,E7
E1 = tk.Text(self.Frame10, height=1, width=30)
E1.place(x=190, y=25)
E2 = tk.Text(self.Frame10, height=1, width=30)
E2.place(x=190, y=50)
E3 = tk.Text(self.Frame10, height=1, width=30)
E3.place(x=190, y=75)
E4 = tk.Text(self.Frame10, height=1, width=30)
E4.place(x=190, y=100)
E5 = tk.Text(self.Frame10, height=1, width=5)
E5.place(x=190, y=125)
E6 = tk.Text(self.Frame10, height=1, width=5)
E6.place(x=190, y=150)
E7 = tk.Text(self.Frame10, height=1, width=10)
E7.place(x=190, y=175)
def create_buttons(self):
self.B1 = tk.Button(self.Frame10, text='Clear', command = self.clear_data)
self.B1.grid(row=0, column=0,padx=420, pady=5)
self.B2 = tk.Button(self.Frame10, text='VIEW NEXT', command = self.read_next)
self.B2.grid(row=1, column=0,padx=420, pady=5)
self.B3 = tk.Button(self.Frame10, text='VIEW PREVIOUS', command = self.read_prev)
self.B3.grid(row=2, column=0,padx=420, pady=5)
self.B4 = tk.Button(self.Frame10, text=' MODIFY ', command = self.modify_data)
self.B4.grid(row=3, column=0,padx=420, pady=5)
self.B5 = tk.Button(self.Frame10, text='SAVE NEW', command = self.save_data)
self.B5.grid(row=4, column=0,padx=420, pady=5)
self.B6 = tk.Button(self.Frame10, text=' DELETE ', command = self.delete_data)
self.B6.grid(row=5, column=0,padx=420, pady=5)
def create_labels(self):
LU = tk.Label(self.Frame10, text="ID :" , font = 30)
LU.place(x=20, y=0)
L1 = tk.Label(self.Frame10, text="Name :" , font = 30)
L1.place(x=20, y=25)
L2 = tk.Label(self.Frame10, text="Contact Number :" , font = 30)
L2.place(x=20, y=50)
L3 = tk.Label(self.Frame10, text="Email:" , font = 30)
L3.place(x=20, y=75)
L4 = tk.Label(self.Frame10, text="Date of Birth :" , font = 30)
L4.place(x=20, y=100)
L5 = tk.Label(self.Frame10, text="Age :" , font = 30)
L5.place(x=20, y=125)
L6 = tk.Label(self.Frame10, text="Height :" , font = 30)
L6.place(x=20, y=150)
L7 = tk.Label(self.Frame10, text="Mobile Phone Maker :" , font = 30)
L7.place(x=20, y=175)
self.read_next()
def clear_data(self):
global E1,E2,E3,E4,E5,E6,E7
L6 = tk.Label(self.Frame10, text=" ")
L6.config(font=("Courier", 10))
L6.place(x=190, y=0)
E1.delete('1.0','30.0')
E2.delete('1.0','30.0')
E3.delete('1.0','30.0')
E4.delete('1.0','30.0')
E5.delete('1.0','30.0')
E6.delete('1.0','30.0')
E7.delete('1.0','30.0')
LT = tk.Label(self.Frame10, text=" ")
LT.config(font=("Courier", 10))
LT.place(x=280, y=210)
def read_next(self):
global E1,E2,E3,E4,E5,E6,E7, user_id, max_id
if user_id < max_id:
user_id +=1
cursor.execute('''SELECT name, phone, email, dob, age, height, mobile FROM users WHERE id=?''', (user_id,))
user = cursor.fetchone()
E1.delete('1.0','30.0')
E2.delete('1.0','30.0')
E3.delete('1.0','30.0')
E4.delete('1.0','30.0')
E5.delete('1.0','30.0')
E6.delete('1.0','30.0')
E7.delete('1.0','30.0')
L6 = tk.Label(self.Frame10, text=" ")
L6.config(font=("Courier", 10))
L6.place(x=190, y=0)
L6 = tk.Label(self.Frame10, text=user_id)
L6.config(font=("Courier", 10))
L6.place(x=190, y=0)
if user != None :
E1.insert('1.0',user[0])
E2.insert('1.0',user[1])
E3.insert('1.0',user[2])
E4.insert('1.0',user[3])
E5.insert('1.0',user[4])
E6.insert('1.0',user[5])
E7.insert('1.0',user[6])
def read_prev(self):
global E1,E2,E3,E4,E5,E6,E7, user_id
user_id -=1
if user_id < 1:
user_id = 1
cursor.execute('''SELECT name, phone, email, dob, age, height, mobile FROM users WHERE id=?''', (user_id,))
user = cursor.fetchone()
E1.delete('1.0','30.0')
E2.delete('1.0','30.0')
E3.delete('1.0','30.0')
E4.delete('1.0','30.0')
E5.delete('1.0','30.0')
E6.delete('1.0','30.0')
E7.delete('1.0','30.0')
L6 = tk.Label(self.Frame10, text=" ")
L6.config(font=("Courier", 10))
L6.place(x=190, y=0)
L6 = tk.Label(self.Frame10, text=user_id)
L6.config(font=("Courier", 10))
L6.place(x=190, y=0)
if user != None:
E1.insert('1.0',user[0])
E2.insert('1.0',user[1])
E3.insert('1.0',user[2])
E4.insert('1.0',user[3])
E5.insert('1.0',user[4])
E6.insert('1.0',user[5])
E7.insert('1.0',user[6])
def save_data(self):
global E1,E2,E3,E4,E5,E6,E7,user_id, max_id
Name = str(E1.get('1.0','30.0'))
name1 = Name.strip()
Phone = str(E2.get('1.0','30.0'))
phone1 = Phone.strip()
Email = str(E3.get('1.0','30.0'))
email1 = Email.strip()
dob = str(E4.get('1.0','30.0'))
dob1 = dob.strip()
Age = str(E5.get('1.0','20.0'))
age1 = Age.strip()
Height = str(E6.get('1.0','20.0'))
height1 = Height.strip()
Mobile = str(E7.get('1.0','20.0'))
mobile1 = Mobile.strip()
if len(name1) > 0 :
cursor = db.cursor()
cursor.execute('''INSERT INTO users(name, phone, email, dob, age, height, mobile)
VALUES(?,?,?,?,?,?,?)''', (name1,phone1, email1, dob1, age1, height1, mobile1))
db.commit()
user_id = cursor.lastrowid
max_id = user_id
L6 = tk.Label(self.Frame10, text=user_id)
L6.config(font=("Courier", 10))
L6.place(x=190, y=0)
LT = tk.Label(self.Frame10, text="Data Saved ")
LT.config(font=("Courier", 10))
LT.place(x=280, y=210)
else:
LT = tk.Label(self.Frame10, text="Fields Missing ")
LT.config(font=("Courier", 10))
LT.place(x=280, y=210)
def modify_data(self):
global E1,E2,E3,E4,E5,E6,E7
global user_id
cursor = db.cursor()
Name = str(E1.get('1.0','30.0'))
name1 = Name.strip()
Phone = str(E2.get('1.0','30.0'))
phone1 = Phone.strip()
Email = str(E3.get('1.0','30.0'))
email1 = Email.strip()
dob = str(E4.get('1.0','30.0'))
dob1 = dob.strip()
Age = str(E5.get('1.0','20.0'))
age1 = Age.strip()
Height = str(E6.get('1.0','20.0'))
height1 = Height.strip()
Mobile = str(E7.get('1.0','20.0'))
mobile1 = Mobile.strip()
cursor.execute('''UPDATE users SET name = ? WHERE id = ? ''',(name1, user_id))
cursor.execute('''UPDATE users SET phone = ? WHERE id = ? ''',(phone1, user_id))
cursor.execute('''UPDATE users SET email = ? WHERE id = ? ''',(email1, user_id))
cursor.execute('''UPDATE users SET dob = ? WHERE id = ? ''',(dob1, user_id))
cursor.execute('''UPDATE users SET age = ? WHERE id = ? ''',(age1, user_id))
cursor.execute('''UPDATE users SET height = ? WHERE id = ? ''',(height1, user_id))
cursor.execute('''UPDATE users SET mobile = ? WHERE id = ? ''',(mobile1, user_id))
db.commit()
def delete_data(self):
global user_id
cursor.execute('''DELETE FROM users WHERE id = ? ''', (user_id,))
db.commit()
self.clear_data()
if __name__ == '__main__':
root = tk.Tk()
main_app = MainApplication(root)
root.mainloop()