-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPySQLAlch.py
More file actions
60 lines (47 loc) · 1.54 KB
/
PySQLAlch.py
File metadata and controls
60 lines (47 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 4 11:53:44 2023
@author: VIM1CA
"""
import pyodbc
from sqlalchemy import create_engine, Column, Integer, String, Date
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
def insert_data_into_MySQLdatabase(connection_string,
person_id,
last_name,
first_name,
age):
connection_string = connection_string
engine = create_engine(f"mssql+pyodbc:///?odbc_connect={connection_string}")
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()
class Persons(Base):
__tablename__ = 'Persons'
Personid = Column(Integer, primary_key=True)
LastName = Column(String(255))
FirstName = Column(String(255))
Age = Column(Integer)
new_person = Persons(
Personid=person_id,
LastName=last_name,
FirstName=first_name,
Age=age,
)
session.add(new_person)
session.commit()
session.close()
insert_data_into_MySQLdatabase(
connection_string = (
"DSN=SampleDNS;"
"Driver={ODBC Driver 17 for SQL Server};"
"Server=CA-C-002AG\SQLEXPRESS;"
"Database=SampleDB;"
"Trusted_Connection=yes;"
),
person_id = '7',
last_name = 'Testeab',
first_name = 'SilvioS',
age = '15',
)