Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

Python SQL Create DB

by suresh

In this article, we will show you, How to create a Database in SQL Server using the Python Programming language with an example. 

TIP: Please refer Connect Python to SQL Server article to understand the steps involved in establishing a connection.

Before we get into the Python SQL Create DB example query, let me show you the list of available databases in SQL Server. 

Python SQL databases

Below program returns all the databases that are available in the current SQL instance. I suggest you refer to SQL Create Database article to understand the query that we used.

# Python SQL Create DB Example
import pyodbc
dbConn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
                      "Server=PRASAD;"
                      "Trusted_Connection=yes;")

dbCursor = dbConn.cursor()
dbCursor.execute("SELECT name FROM master.dbo.sysdatabases")

for x in dbCursor:
    print('Database = %r' %x)

OUTPUT

Python SQL Create DB 1

If you want to work with a particular database then you can specify that database within the connection string.

For example, in the below python program, we used “Database = SQL Tutorial” inside the connection string

# Python SQL Create DB Example
import pyodbc
dbConn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
                      "Server=PRASAD;"
                      "Database=SQL Tutorial;"
                      "Trusted_Connection=yes;")

dbCursor = dbConn.cursor()
dbCursor.execute("SELECT name FROM master.dbo.sysdatabases")

for x in dbCursor:
    print('Database = %r' %x)

OUTPUT

Python SQL Create DB 2

Python SQL Create DB Example

In this example, we will show you, how to create a database in SQL using Python.

Below program creates a new database called PythonDatabase in SQL Server.

# Python SQL Create DB Example
import pyodbc
dbConn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
                      "Server=PRASAD;"
                      "Trusted_Connection=yes;")

dbConn.autocommit = True
dbCursor = dbConn.cursor()
dbCursor.execute("CREATE DATABASE [PythonDatabase]")

OUTPUT

Python SQL Create DB 3

Let me run the first query to display the list of databases that are available in a current instance.

Python SQL Create DB 4

From the above screenshot, you can see our newly created Database.

Placed Under: Python

Trending Posts

Python Sort

JavaScript Date Function

SSIS Lookup Transformation Case Sensitivity

Tableau Dual Combination Chart

Python SQL Order By

Java String getBytes Method

Python Program to find Area of an Equilateral Triangle

Python casefold

Format Power BI Card

C Program to Print Multiplication Table

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy