请问,如何给sqlite数据表根据timee列排序?问题2:如果不排序,如何让新增的数据在数据库的最上面写入而不是在最后面
import sqlite3
def conn():
"""创建数据库库"""
Conn=sqlite3.connect('D:/nba2.db') # 连接数据库
Cur=Conn.cursor() # 创建游标对象
Cur.execute("create table if not exists lakers(timee DATE primary key not null on conflict ignore, age integer, phone integer)") # 创建表
Conn.commit()
print('数据库表创建成功')
def ins():
"""添加数据"""
Conn = sqlite3.connect('D:/nba2.db')
Cur = Conn.cursor()
Cur.execute('INSERT OR IGNORE INTO lakers values(?,?,?)', (20230202,320,33))
Cur.execute('INSERT OR IGNORE INTO lakers values(?,?,?)', (20201010,191,33)) # 插入数据,使用了占位符?
Cur.execute('INSERT OR IGNORE INTO lakers values(?,?,?)',(20190101,203,36))
Cur.execute('INSERT OR IGNORE INTO lakers values(?,?,?)',(20180808,203,36))
Cur.execute('INSERT OR IGNORE INTO lakers values(?,?,?)',(20170707,100,28))
Cur.execute('INSERT OR IGNORE INTO lakers values(?,?,?)',(20160606,210,36))
#Cur.execute('select * from lakers order by (phone)ASC')
Conn.commit()
print('数据添加成功')
if __name__ == '__main__':
conn()
ins()
Conn=sqlite3.connect('D:/nba2.db') # 连接数据库
Cur=Conn.cursor() # 创建游标对象
Cur.execute('INSERT OR IGNORE INTO lakers values(?,?,?)',(20230203,210,36))
Cur.execute('select * from lakers order by phone asc')
Conn.commit()
Cur.execute("SELECT * FROM lakers")
a=Cur.fetchall()
print(a)