Note:
cursor.execute()
cursor.fetchone()
Python MySQL Solution
import mysql.connector def get_connection(): connection = mysql.connector.connect(host='localhost', database='python_db', user='pynative', password='pynative@#29') return connection def close_connection(connection): if connection: connection.close() def read_database_version(): try: connection = get_connection() cursor = connection.cursor() cursor.execute("SELECT version();") db_version = cursor.fetchone() print("You are connected to MySQL version: ", db_version) close_connection(connection) except (Exception, mysql.connector.Error) as error: print("Error while getting data", error) print("Question 1: Print Database version") read_database_version()
Python PostgreSQL Solution
import psycopg2 def get_connection(): connection = psycopg2.connect(user="postgres", password="pynative@#29", host="127.0.0.1", port="5432", database="python_db") return connection def close_connection(connection): if connection: connection.close() def read_database_version(): try: connection = get_connection() cursor = connection.cursor() cursor.execute("SELECT version();") db_version = cursor.fetchone() print("You are connected to PostgreSQL version: ", db_version) close_connection(connection) except (Exception, psycopg2.Error) as error: print("Error while getting data", error) print("Question 1: Print Database version") read_database_version()
Python SQLite Solution
import sqlite3 def get_connection(): connection = sqlite3.connect('python_db.db') return connection def close_connection(connection): if connection: connection.close() def read_database_version(): try: connection = get_connection() cursor = connection.cursor() cursor.execute("select sqlite_version();") db_version = cursor.fetchone() print("You are connected to SQLite version: ", db_version) close_connection(connection) except (Exception, sqlite3.Error) as error: print("Error while getting data", error) print("Question 1: Print Database version") read_database_version()
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Python MySQL Solution
Python PostgreSQL Solution
Python SQLite Solution
need an explanation for this answer? contact us directly to get an explanation for this answer