Python: Create a SQLite database connection to a database that resides in the memory
2. In-Memory SQLite DB
Write a Python program to create a SQLite database connection to a database that resides in the memory.
Sample Solution:
Python Code :
import sqlite3
try:
sqlite_Connection = sqlite3.connect('temp.db')
conn = sqlite3.connect(':memory:')
print("\nMemory database created and connected to SQLite.")
sqlite_select_Query = "select sqlite_version();"
conn.execute(sqlite_select_Query)
print("\nSQLite Database Version is: ", sqlite3.version)
conn.close()
except sqlite3.Error as error:
print("\nError while connecting to sqlite", error)
finally:
if (sqlite_Connection):
sqlite_Connection.close()
print("\nThe SQLite connection is closed.")
Sample Output:
Memory database created and connected to SQLite. SQLite Database Version is: 2.6.0 The SQLite connection is closed.
For more Practice: Solve these Related Problems:
- Write a Python program to create an in-memory SQLite database, create a simple table in it, and then print the number of rows in the table (initially zero).
- Write a Python function that creates a SQLite database in memory, inserts a few records, and then retrieves and prints them.
- Write a Python script to benchmark operations (insert, select, update) on an in-memory SQLite database versus a file-based database.
- Write a Python program to create an in-memory SQLite database and demonstrate that the database is volatile by attempting to access it after closing the connection.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to create a SQLite database and connect with the database and print the version of the SQLite database.
Next: Write a Python program to connect a database and create a SQLite table within the database.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.