[파이썬] SQLite 데이터베이스 파일 경로

SQLite is a popular lightweight relational database management system used in many applications. In Python, you can easily work with SQLite databases using the built-in sqlite3 module.

In this blog post, we will discuss how to retrieve the file path of an SQLite database in Python.

Getting the SQLite Database File Path

To get the file path of an SQLite database in Python, you need to establish a connection to the database using the connect() function from the sqlite3 module. Once the connection is established, you can use the database attribute to obtain the file path.

Here’s an example code snippet:

import sqlite3

# Connect to the SQLite database
conn = sqlite3.connect('example.db')

# Get the database file path
db_file_path = conn.database

# Print the file path
print("SQLite database file path:", db_file_path)

# Close the database connection
conn.close()

In the above code, we first import the sqlite3 module. Then, we establish a connection to the SQLite database example.db using the connect() function.

Next, we access the database attribute of the connection object to retrieve the file path of the SQLite database. We store the file path in the db_file_path variable.

Finally, we print the file path using the print() function. Remember to close the database connection using the close() method to free up resources.

Conclusion

Retrieving the SQLite database file path in Python is a straightforward process. By establishing a connection to the database and accessing the database attribute, you can easily obtain the file path.

SQLite databases are widely used due to their simplicity and portability. Understanding how to work with SQLite databases in Python is essential for many data-related applications.

I hope this blog post has helped you understand how to retrieve the file path of an SQLite database in Python. Happy coding!