python sqllite3

69 阅读1分钟

python操作sqllite数据库的包,文档在:docs.python.org/3/library/s…

举例如下:

    def initialize_database(self):
        """Initialize SQLite database and create tables if they don't exist."""
        db_name = "file_info.db"
        self.db_connection = sqlite3.connect(db_name)
        cursor = self.db_connection.cursor()
        
        # Create a table for each file type if it doesn't exist
        cursor.execute(f'''
            CREATE TABLE IF NOT EXISTS {self.file_type} (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                path TEXT,
                name TEXT,
                created_time TEXT,
                size TEXT
            )
        ''')
        self.db_connection.commit()

可见基本用法,就是

db_connection = sqlite3.connect(db_name)
cursor = db_connection.cursor()
cursor.execute()
db_connection.commit()