Created
July 11, 2016 15:41
-
-
Save technetbytes/2720961ae365c02a0a248cc0a19db3ec to your computer and use it in GitHub Desktop.
Create Azure SQL Table using pyodbc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Sun July 10 10:09:54 2016 | |
@author: saqibullah | |
@email: saqibullah@gmail.com | |
""" | |
import pyodbc | |
# Using pyodbc library create Azure SQL table and insert single record. | |
# Replace the following items in the connection string. | |
# <Username> | |
# <HostName> | |
# <Password> | |
connection = pyodbc.connect('DSN=azuresql;UID=<username>@<HostName>;PWD=<Password>') | |
cursor = connection.cursor() | |
# create participants table | |
cursor.execute(""" | |
CREATE TABLE participants | |
(id int IDENTITY(1,1) PRIMARY KEY, | |
urlId varchar(50) NOT NULL, | |
type varchar(50) NULL, | |
relationship varchar(100) NULL, | |
name varchar(50) NULL, | |
age varchar(50) NULL, | |
agegroup varchar(50) NULL, | |
gender varchar(50) NULL, | |
status varchar(50) NULL, | |
creationTime datetime default CURRENT_TIMESTAMP) | |
""") | |
connection.commit() | |
# Add test record in the participants table | |
cursor.execute("""INSERT INTO participants (urlId, type, relationship, name , age , agegroup, gender, status) | |
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""", ('423', | |
'alpha', | |
'husband', | |
'bill', | |
'30', | |
'18+', | |
'male', | |
'status data')) | |
connection.commit() | |
connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment