Files
Arcaea-server/tools/update_song.py
Lost-MSth 6fcca17918 Code refactoring
- Code refactoring
- Fix a bug that the other player will not become the host of the room at once, when the player disconnect in link play.

> Maybe add many unknown bugs. XD
> The song database `arcsong.db` will not used in the future. You can use a tool in `tool` folder to import old data.
2022-07-04 18:36:30 +08:00

61 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import sqlite3
class Connect():
# 数据库连接类,上下文管理
def __init__(self, file_path='./arcaea_database.db'):
"""
数据库连接默认连接arcaea_database.db\
接受:文件路径\
返回sqlite3连接操作对象
"""
self.file_path = file_path
def __enter__(self):
self.conn = sqlite3.connect(self.file_path)
self.c = self.conn.cursor()
return self.c
def __exit__(self, exc_type, exc_val, exc_tb) -> bool:
if exc_type is not None:
if self.conn:
self.conn.rollback()
if self.conn:
self.conn.commit()
self.conn.close()
return True
def insert(cursor, song_id, name, a, b, c, d):
'''Insert a new song into database.'''
cursor.execute(
'''select exists(select * from chart where song_id=?)''', (song_id, ))
if cursor.fetchone()[0]:
return None
cursor.execute(
'''insert into chart values (?,?,?,?,?,?)''', (song_id, name, a, b, c, d))
def old_to_new():
'''Update old database to new database.'''
with Connect('./arcsong.db') as c:
c.execute(
'''select sid, name_en, rating_pst, rating_prs, rating_ftr, rating_byn from songs''')
data = c.fetchall()
with Connect() as c:
for x in data:
insert(c, x[0], x[1], x[2], x[3], x[4], x[5])
def main():
old_to_new()
if __name__ == '__main__':
main()
print('Done.')
input()
exit()