w3resource

Python: Create a SQLite database and connect with the database and print the version of the SQLite database


1. SQLite DB Version

Write a Python program to create a SQLite database and connect with the database and print the version of the SQLite database.

Sample Solution:

Python Code :

import sqlite3
try:
   sqlite_Connection = sqlite3.connect('temp.db')
   conn = sqlite_Connection.cursor()
   print("\nDatabase created and connected to SQLite.")
   sqlite_select_Query = "select sqlite_version();"
   conn.execute(sqlite_select_Query)
   record = conn.fetchall()
   print("\nSQLite Database Version is: ", record)
   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:

Database created and connected to SQLite.

SQLite Database Version is:  [('3.11.0',)]

The SQLite connection is closed.

Alternate solution (Connect to a SQLite Database):

Python Code :

import  sqlite3
print("creating connecting ...")
conn  =  sqlite3.connect ('mydatabase.db' )
conn . close ()
print("\nThe SQLite connection is closed.")

Sample Output:

creating connecting ...

The SQLite connection is closed.

For more Practice: Solve these Related Problems:

  • Write a Python program that creates a SQLite database file, connects to it, retrieves the SQLite version using a SQL query, and prints it.
  • Write a Python script to create a new SQLite database, execute a query to get the version, and log the version information to a text file.
  • Write a Python function that establishes a SQLite connection, executes a "SELECT sqlite_version()" query, and returns the version as a string.
  • Write a Python program that creates a SQLite database, prints its version, and then compares the version to a minimum required version, printing a warning if outdated.

Go to:


Previous: SQLite Database Home.
Next: Write a Python program to create a SQLite database connection to a database that resides in the memory.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.