I understand that the primary key autoincrements the primary key for each record inserted, but when I insert data using option 1, then display the data using option 2, it shows the PK as 1,2,3... etc. However, I would like it to store the PK as 4 digits, E.G. 0001,0002,0003 rather than 1,2,3. Is there any way to do this? PLEASE NOTE: this is example simplified code.
import sqlite3
connection = sqlite3.connect('database.db')
cursor = connection.cursor()
create_table = '''
CREATE TABLE IF NOT EXISTS employee (
staff_ID INTEGER PRIMARY KEY NOT NULL,
fname VARCHAR(20) NOT NULL,
lname VARCHAR(30) NOT NULL,
gender VARCHAR(20) NOT NULL);'''
cursor.execute(create_table)
print('press 1 to add new user')
print('press 2 to show all users')
option = input('1 or 2: ')
if option == '1':
#userid = input('User ID: ')
first_name = input('First name : ')
surname = input('Surname: ')
gender = input('Gender: ')
add_staff = '''INSERT INTO employee (fname, lname, gender)
VALUES (?, ?, ?, ?);'''
cursor.execute(add_staff, (first_name, surname, gender))
elif option == '2':
cursor.execute('SELECT * FROM employee')
print('fetchall:')
result = cursor.fetchall()
for r in result:
print(r)
cursor.execute('SELECT * FROM employee')
connection.commit()
connection.close()
Also, how do I make sure that when the first name and surname are entered, they are only text values? I tried using 'text' in the CREATE section... any ide