In our previous article,
Python MS SQL Server create database, we created the database cpp_db. But before creating a database, we should confirm that the database is not already existing in server. In case we try to create a database that is already existing, it gives below error.
'cpp_db' already exists. Choose a different database name.
In this article, we will demonstrate how to check if a database is existing or not using Python. In order to that, we have to fetch the list of all the database. After that check database name in list or not.
# Below Code snippet is check the existence of database in the SQL Server
import pyodbc
server = 'Mypc'
username = 'sa'
password = 'Windows1'
new_database = 'cpp_db'
#Connecting to MS SQL Server
connection = None
try:
connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';UID='+username+';PWD='+ password)
print('Connected to SQL Server Successfully')
except:
print('Connection failed to SQL Server')
if connection is not None:
connection.autocommit = True
#Asking user to enter database name he wants to check
database_name = input('Enter database name to check exist or not: ')
cur = connection.cursor()
cur.execute("SELECT name FROM master.dbo.sysdatabases where name=?;",(database_name,))
data=cur.fetchall()
print(data)
#Printing if database exists or not
if not data:
print("'{}' Database does not exist.".format(database_name))
else:
print("'{}' Database already exists".format(database_name))
# After all operation is done close the database connection and cursor
cusor.close()
connection.close()
print('Done')
Comments