initial commit
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import pyodbc
|
||||
|
||||
try:
|
||||
connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes')
|
||||
cursor = connection.cursor()
|
||||
|
||||
create_table="""
|
||||
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tbl_customers_from_csv_bulk_insert]') AND type in (N'U'))
|
||||
begin
|
||||
create table [dbo].[tbl_customers_from_csv_bulk_insert](
|
||||
id int not null,
|
||||
code varchar(50) null,
|
||||
first_name varchar(50) null,
|
||||
last_name varchar(50) null,
|
||||
)
|
||||
end;
|
||||
"""
|
||||
cursor.execute(create_table)
|
||||
connection.commit()
|
||||
|
||||
bulk_insert_cmd="""
|
||||
bulk insert tbl_customers_from_csv_bulk_insert
|
||||
from 'C:\\Users\\bl.SEV\\repos\\python-mssql\\advanced-topics\\csv\\sample-csv-file-for-bulk-insert-demo.csv'
|
||||
with
|
||||
(
|
||||
fieldterminator = ';',
|
||||
rowterminator = '\n'
|
||||
);
|
||||
"""
|
||||
cursor.execute(bulk_insert_cmd)
|
||||
connection.commit()
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
except pyodbc.Error as ex:
|
||||
print()
|
||||
print("exception: ",ex)
|
||||
cursor.close()
|
||||
connection.close()
|
||||
print()
|
||||
exit()
|
||||
@@ -0,0 +1,41 @@
|
||||
import subprocess
|
||||
import os
|
||||
|
||||
sql_instance = "."
|
||||
sql_database = "master"
|
||||
folder_to_check = "./sql_scripts/"
|
||||
|
||||
def run_ps_command(cmd):
|
||||
# Build the PowerShell invocation list
|
||||
command = [
|
||||
"PowerShell",
|
||||
"-ExecutionPolicy", "Unrestricted",
|
||||
"-Command", cmd
|
||||
]
|
||||
|
||||
result = subprocess.run(
|
||||
command,
|
||||
capture_output=True,
|
||||
text=True
|
||||
)
|
||||
return result
|
||||
|
||||
for file in os.listdir(folder_to_check):
|
||||
if file.endswith(".sql"):
|
||||
full_path = os.path.join(folder_to_check, file)
|
||||
print(f"executing .sql file: {full_path}")
|
||||
|
||||
sql_command = (
|
||||
f"Invoke-Sqlcmd -ServerInstance {sql_instance} "
|
||||
f"-Database {sql_database} -InputFile \"{full_path}\""
|
||||
)
|
||||
|
||||
result = run_ps_command(sql_command)
|
||||
|
||||
if result.returncode != 0:
|
||||
print("Error executing script:")
|
||||
print(result.stderr.strip())
|
||||
else:
|
||||
if result.stdout:
|
||||
print("Output:")
|
||||
print(result.stdout.strip())
|
||||
@@ -0,0 +1,45 @@
|
||||
import tkinter
|
||||
import pyodbc
|
||||
|
||||
sql_server_ver = "Not Available"
|
||||
|
||||
root = tkinter.Tk()
|
||||
root.geometry("600x400")
|
||||
root.title("Sample Python GUI App")
|
||||
|
||||
label_hello_world = tkinter.Label(root, text=str(sql_server_ver), fg="red")
|
||||
|
||||
def sql_server_version():
|
||||
|
||||
try:
|
||||
connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes')
|
||||
|
||||
cursor = connection.cursor()
|
||||
|
||||
cursor.execute("select @@VERSION as version")
|
||||
|
||||
while 1:
|
||||
row = cursor.fetchone()
|
||||
if not row:
|
||||
break
|
||||
sql_server_ver = row.version
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
except pyodbc.Error as ex:
|
||||
print()
|
||||
print("exception: ",ex)
|
||||
cursor.close()
|
||||
connection.close()
|
||||
print()
|
||||
exit()
|
||||
|
||||
label_hello_world.config(text=str(sql_server_ver))
|
||||
label_hello_world.pack()
|
||||
|
||||
btn_sql_server_version = tkinter.Button(root, command=sql_server_version, text="Retrieve SQL Server Version")
|
||||
btn_sql_server_version.pack(side=tkinter.TOP, pady=50)
|
||||
|
||||
|
||||
root.mainloop()
|
||||
@@ -0,0 +1,5 @@
|
||||
1;customer1;fistName1;lastName1
|
||||
2;customer2;fistName2;lastName2
|
||||
3;customer3;fistName3;lastName3
|
||||
4;customer4;fistName4;lastName4
|
||||
5;customer5;fistName5;lastName5
|
||||
|
@@ -0,0 +1,5 @@
|
||||
1;customer1;fistName1;lastName1
|
||||
2;customer2;fistName2;lastName2
|
||||
3;customer3;fistName3;lastName3
|
||||
4;customer4;fistName4;lastName4
|
||||
5;customer5;fistName5;lastName5
|
||||
|
@@ -0,0 +1,10 @@
|
||||
create database SampleDB2022_1a;
|
||||
go
|
||||
|
||||
use SampleDB2022_1a;
|
||||
go
|
||||
|
||||
create table tblSample1a(
|
||||
id int,
|
||||
code varchar(50));
|
||||
go
|
||||
@@ -0,0 +1,14 @@
|
||||
use SampleDB2022_1a;
|
||||
go
|
||||
|
||||
insert into tblSample1a
|
||||
values (1,'test1'), (2,'test2');
|
||||
|
||||
create table tblSample1b(
|
||||
id int,
|
||||
code varchar(50));
|
||||
go
|
||||
|
||||
insert into tblSample1b
|
||||
values (1,'test1'), (2,'test2');
|
||||
go
|
||||
@@ -0,0 +1,14 @@
|
||||
create database SampleDB2022_1b;
|
||||
go
|
||||
|
||||
use SampleDB2022_1b;
|
||||
go
|
||||
|
||||
create table tblSample1a(
|
||||
id int,
|
||||
code varchar(50));
|
||||
|
||||
create table tblSample1b(
|
||||
id int,
|
||||
code varchar(50));
|
||||
go
|
||||
@@ -0,0 +1,71 @@
|
||||
import pyodbc
|
||||
import csv
|
||||
|
||||
try:
|
||||
connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=.;DATABASE=SampleDB;Trusted_Connection=yes')
|
||||
|
||||
cursor = connection.cursor()
|
||||
|
||||
print("[creating table tbl_customers_from_csv...]")
|
||||
create_table="""IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tbl_customers_from_csv]') AND type in (N'U'))
|
||||
begin
|
||||
create table [dbo].[tbl_customers_from_csv] (
|
||||
id int not null,
|
||||
code varchar(50) null,
|
||||
first_name varchar(50) null,
|
||||
last_name varchar(50) null,
|
||||
)
|
||||
end;
|
||||
"""
|
||||
|
||||
cursor.execute(create_table)
|
||||
connection.commit()
|
||||
|
||||
print("[selecting from table before csv insert...]")
|
||||
cursor.execute("select * from tbl_customers_from_csv")
|
||||
while 1:
|
||||
row = cursor.fetchone()
|
||||
if not row:
|
||||
break
|
||||
print(row.id, row.code, row.first_name, row.last_name)
|
||||
|
||||
print()
|
||||
|
||||
# read csv file and insert each row into table in sql server
|
||||
print("[reading csv file and insert new rows into sql server table...]")
|
||||
with open("./csv/sample-csv-file-for-demo.csv", newline='') as csv_file:
|
||||
csv_reader = csv.reader(csv_file, delimiter=';', quotechar='|')
|
||||
for row in csv_reader:
|
||||
prm_id=row[0]
|
||||
prm_code=row[1]
|
||||
prm_first_name=row[2]
|
||||
prm_last_name=row[3]
|
||||
|
||||
print("inserting record:",', '.join(row))
|
||||
cursor.execute("insert into dbo.tbl_customers_from_csv values (?,?,?,?)", prm_id, prm_code, prm_first_name, prm_last_name)
|
||||
connection.commit()
|
||||
|
||||
print()
|
||||
|
||||
print("[select table after inserts...]")
|
||||
cursor.execute("select * from tbl_customers_from_csv")
|
||||
while 1:
|
||||
row = cursor.fetchone()
|
||||
if not row:
|
||||
break
|
||||
print(row.id, row.code, row.first_name, row.last_name)
|
||||
|
||||
print()
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
except pyodbc.Error as ex:
|
||||
print()
|
||||
print("exception: ",ex)
|
||||
cursor.close()
|
||||
connection.close()
|
||||
print()
|
||||
exit()
|
||||
|
||||
print()
|
||||
Reference in New Issue
Block a user