Skip to content

Commit dee74c5

Browse files
authoredMar 29, 2021
ajouter pyodbc
1 parent 773b68a commit dee74c5

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed
 

‎LME.py

+46-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pandas as pd
1111
from io import BytesIO
1212
import re
13+
import pyodbc
1314

1415

1516
#say if we wanna get the trader commitment report of lme from the link below
@@ -98,12 +99,43 @@ def etl(content,date):
9899
return output
99100

100101

102+
#for sql server
103+
#we have to use pyodbc driver
104+
def connect(
105+
server=None, database=None, driver=None,
106+
username=None, password=None,
107+
autocommit=False
108+
):
109+
""" get the db connection """
110+
connection_string = "Driver={driver}; Server={server}; Database={database}"
111+
if username:
112+
connection_string += "; UID={username}"
113+
if password:
114+
connection_string += "; PWD={password}"
115+
if not driver:
116+
driver = [
117+
d for d in sorted(pyodbc.drivers())
118+
if re.match(r"(ODBC Driver \d+ for )?SQL Server", d)
119+
][0]
120+
121+
return pyodbc.connect(
122+
connection_string.format(
123+
server=server,
124+
database=database,
125+
driver=driver,
126+
username=username,
127+
password=password,
128+
),
129+
autocommit=autocommit,
130+
)
131+
132+
101133
#this function is to insert data into sqlite3 database
102134
#i will not go into details for sql grammar
103135
#for pythoners, sql is a piece of cake
104136
#go check out the following link for sql
105137
# https://www.w3schools.com/sql/
106-
def database(df):
138+
def database(df,SQL=False):
107139

108140
#plz make sure u have created the database and the table to proceed
109141
#to create a table in database, first two lines are the same as below
@@ -113,13 +145,19 @@ def database(df):
113145
#conn.commit()
114146
#conn.close()
115147

116-
#to see what it looks like in the database
117-
#use microsoft access or toad or just pandas
118-
#db=pd.read_sql("""SELECT * FROM lme""",conn)
119-
conn = sqlite3.connect('database.db')
120-
c = conn.cursor()
121-
122-
148+
#connect to sqlite3
149+
if not SQL:
150+
151+
#to see what it looks like in the database
152+
#use microsoft access or toad or just pandas
153+
#db=pd.read_sql("""SELECT * FROM lme""",conn)
154+
conn = sqlite3.connect('database.db')
155+
else:
156+
SERVER='10.10.10.10'
157+
DATABASE='meme_stock'
158+
conn=connect(SERVER,DATABASE,'SQL Server')
159+
c = conn.cursor()
160+
123161
#insert data
124162
for i in range(len(df)):
125163
try:

0 commit comments

Comments
 (0)
Please sign in to comment.