如何使主键自动增量为 0001、0002、0003 而不是 1、2、3?
How do I make the primary key autoincrement like 0001, 0002 ,0003 rather than 1, 2, 3?
我知道主键会为插入的每条记录自动递增主键,但是当我使用选项 1 插入数据,然后使用选项 2 显示数据时,它显示 PK 为 1,2,3...等。但是,我希望它将 PK 存储为 4 位数字,E.G. 0001,0002,0003 而不是 1,2,3。有什么办法吗? 请注意:这是示例简化代码。
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()
另外,如何确保在输入名字和姓氏时,它们只是文本值?我尝试在 CREATE 部分使用 'text'...任何 ide
您可以使用zfill方法。
key=1
str(key).zfill(4)
out[1]: '0001'
key=10
str(key).zfill(4)
out[1]: '0010'
integers
只是数字。您无法控制它们的存储方式。但是,您可以在查询 table:
时用零填充它们
SELECT TO_CHAR(staff_id, 'fm0000'), fname, lname, gender
FROM employee
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
除了 rowid 之外,任何列都可以包含任何类型的值作为以下存储之一 classes :-
- 空
- 整数(最多 8 个字节,但最少需要)
- 作为 8 字节 IEEE 浮点数的实数
- 根据数据库编码的文本
- BLOB(字节数组)
然而,存储class 的确定方式取决于值本身和列的声明type/affinifty。
VARCHAR(30) 与 VARCHAR(SOMESTUPIDAMOUNT) 和 CHARISMA 没有区别,因为出现 CHAR(并且没有出现 INT),然后应用列关联的第二条规则,因此列的关联是文本。
规则 2 是
If the declared type of the column contains any of the strings "CHAR",
"CLOB", or "TEXT" then that column has TEXT affinity. Notice that the
type VARCHAR contains the string "CHAR" and is thus assigned TEXT
affinity.
More here about SQLite 3 Datatypes - See 3.1 Determination of Column Affinity for the rules
因此,对于 VARCHAR 列,列亲和力是 TEXT,因此它取决于具有 TEXT 亲和力的列的值和规则,即(3.类型亲和力):-
A column with TEXT affinity stores all data using storage classes
NULL, TEXT or BLOB. If numerical data is inserted into a column with
TEXT affinity it is converted into text form before being stored.
所以你真的不需要任何东西。
然而, 同样重要的是如何检索数据,这将通过 get????
方法之一。简而言之,如果您存储 1
并使用 getInt
方法,您将得到一个整数 1
。如果你存储something
并使用getInt
方法,你将得到0
。使用 getString
你会得到 1
和 something
.
我知道主键会为插入的每条记录自动递增主键,但是当我使用选项 1 插入数据,然后使用选项 2 显示数据时,它显示 PK 为 1,2,3...等。但是,我希望它将 PK 存储为 4 位数字,E.G. 0001,0002,0003 而不是 1,2,3。有什么办法吗? 请注意:这是示例简化代码。
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()
另外,如何确保在输入名字和姓氏时,它们只是文本值?我尝试在 CREATE 部分使用 'text'...任何 ide
您可以使用zfill方法。
key=1
str(key).zfill(4)
out[1]: '0001'
key=10
str(key).zfill(4)
out[1]: '0010'
integers
只是数字。您无法控制它们的存储方式。但是,您可以在查询 table:
SELECT TO_CHAR(staff_id, 'fm0000'), fname, lname, gender
FROM employee
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
除了 rowid 之外,任何列都可以包含任何类型的值作为以下存储之一 classes :-
- 空
- 整数(最多 8 个字节,但最少需要)
- 作为 8 字节 IEEE 浮点数的实数
- 根据数据库编码的文本
- BLOB(字节数组)
然而,存储class 的确定方式取决于值本身和列的声明type/affinifty。
VARCHAR(30) 与 VARCHAR(SOMESTUPIDAMOUNT) 和 CHARISMA 没有区别,因为出现 CHAR(并且没有出现 INT),然后应用列关联的第二条规则,因此列的关联是文本。
规则 2 是
If the declared type of the column contains any of the strings "CHAR", "CLOB", or "TEXT" then that column has TEXT affinity. Notice that the type VARCHAR contains the string "CHAR" and is thus assigned TEXT affinity.
More here about SQLite 3 Datatypes - See 3.1 Determination of Column Affinity for the rules
因此,对于 VARCHAR 列,列亲和力是 TEXT,因此它取决于具有 TEXT 亲和力的列的值和规则,即(3.类型亲和力):-
A column with TEXT affinity stores all data using storage classes NULL, TEXT or BLOB. If numerical data is inserted into a column with TEXT affinity it is converted into text form before being stored.
所以你真的不需要任何东西。
然而, 同样重要的是如何检索数据,这将通过 get????
方法之一。简而言之,如果您存储 1
并使用 getInt
方法,您将得到一个整数 1
。如果你存储something
并使用getInt
方法,你将得到0
。使用 getString
你会得到 1
和 something
.