123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import sqlite3
- class DatabaseManager:
- def __init__(self):
- """初始化数据库管理器并创建数据库连接"""
- self.db_name = "database.db"
- self.conn = sqlite3.connect(self.db_name)
- def execute_query(self, query, params=()):
- """执行SQL查询"""
- cursor = self.conn.cursor()
- cursor.execute(query, params)
- self.conn.commit()
- return cursor
- def insert_command(self, action_name, action_type, action_keyword, action_content):
- """插入一条新的指令到数据库"""
- query = '''
- INSERT INTO file_commands (action_name, action_type, action_keyword, action_content) VALUES (?, ?, ?, ?);
- '''
- self.execute_query(query, (action_name, action_type, action_keyword, action_content))
- def update_command(self, action_name, action_type, action_keyword, action_content, action_id):
- """插入一条新的指令到数据库"""
- query = '''
- UPDATE file_commands set action_name=?, action_type=?, action_keyword=?, action_content=? where id=?;
- '''
- self.execute_query(query, (action_name, action_type, action_keyword, action_content, action_id))
- def fetch_all_commands(self):
- """获取数据库中所有的指令"""
- query = 'SELECT * FROM file_commands;'
- cursor = self.execute_query(query)
- return cursor.fetchall()
- def delete_command(self, command_id):
- """根据ID删除一条指令"""
- query = 'DELETE FROM file_commands WHERE id = ?;'
- self.execute_query(query, (command_id,))
- def __del__(self):
- """确保关闭数据库连接"""
- self.conn.close()
- if __name__ == '__main__':
- db = DatabaseManager()
- commands = db.fetch_all_commands()
- print(commands)
- db.__del__()
|