10
10
import pandas as pd
11
11
from io import BytesIO
12
12
import re
13
+ import pyodbc
13
14
14
15
15
16
#say if we wanna get the trader commitment report of lme from the link below
@@ -98,12 +99,43 @@ def etl(content,date):
98
99
return output
99
100
100
101
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
+
101
133
#this function is to insert data into sqlite3 database
102
134
#i will not go into details for sql grammar
103
135
#for pythoners, sql is a piece of cake
104
136
#go check out the following link for sql
105
137
# https://www.w3schools.com/sql/
106
- def database (df ):
138
+ def database (df , SQL = False ):
107
139
108
140
#plz make sure u have created the database and the table to proceed
109
141
#to create a table in database, first two lines are the same as below
@@ -113,13 +145,19 @@ def database(df):
113
145
#conn.commit()
114
146
#conn.close()
115
147
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
+
123
161
#insert data
124
162
for i in range (len (df )):
125
163
try :
0 commit comments