BlendMMM commited on
Commit
4319964
·
verified ·
1 Parent(s): e61627f

Upload utilities.py

Browse files
Files changed (1) hide show
  1. utilities.py +13 -14
utilities.py CHANGED
@@ -24,7 +24,8 @@ import sqlite3
24
  import datetime
25
  from scenario import numerize
26
  import psycopg2
27
- #
 
28
  import re
29
  import bcrypt
30
  import os
@@ -78,7 +79,7 @@ color_palette = [
78
 
79
 
80
  CURRENCY_INDICATOR = "$"
81
- db_cred=None
82
  # database_file = r"DB/User.db"
83
 
84
  # conn = sqlite3.connect(database_file, check_same_thread=False) # connection with sql db
@@ -145,15 +146,11 @@ db_cred=None
145
  # conn.close()
146
 
147
 
148
- db_path=os.path.join('imp_db.db')
 
149
 
150
  def query_excecuter_postgres(
151
- query,
152
- db_path=r'db\imp_db.db',
153
- params=None,
154
- insert=True,
155
- insert_retrieve=False,
156
- db_cred=None
157
  ):
158
  """
159
  Executes a SQL query on a SQLite database, handling both insert and select operations.
@@ -167,8 +164,13 @@ def query_excecuter_postgres(
167
 
168
  """
169
  try:
 
 
 
 
 
170
  # Establish connection to the SQLite database
171
- conn = sqlite3.connect(r'db\imp_db.db')
172
  except sqlite3.Error as e:
173
  st.warning(f"Unable to connect to the SQLite database: {e}")
174
  st.stop()
@@ -179,7 +181,7 @@ def query_excecuter_postgres(
179
  # Prepare the query with proper placeholders
180
  if params:
181
  # Handle the `IN (?)` clause dynamically
182
- query = query.replace('IN (?)', f"IN ({','.join(['?' for _ in params])})")
183
  c.execute(query, params)
184
  else:
185
  c.execute(query)
@@ -203,9 +205,6 @@ def query_excecuter_postgres(
203
  conn.close()
204
 
205
 
206
-
207
-
208
-
209
  def update_summary_df():
210
  """
211
  Updates the 'project_summary_df' in the session state with the latest project
 
24
  import datetime
25
  from scenario import numerize
26
  import psycopg2
27
+
28
+ #
29
  import re
30
  import bcrypt
31
  import os
 
79
 
80
 
81
  CURRENCY_INDICATOR = "$"
82
+ db_cred = None
83
  # database_file = r"DB/User.db"
84
 
85
  # conn = sqlite3.connect(database_file, check_same_thread=False) # connection with sql db
 
146
  # conn.close()
147
 
148
 
149
+ db_path = os.path.join("imp_db.db")
150
+
151
 
152
  def query_excecuter_postgres(
153
+ query, db_path, params=None, insert=True, insert_retrieve=False, db_cred=None
 
 
 
 
 
154
  ):
155
  """
156
  Executes a SQL query on a SQLite database, handling both insert and select operations.
 
164
 
165
  """
166
  try:
167
+ # Construct a cross-platform path to the database
168
+ db_dir = os.path.join("db")
169
+ os.makedirs(db_dir, exist_ok=True) # Make sure the directory exists
170
+ db_path = os.path.join(db_dir, "imp_db.db")
171
+
172
  # Establish connection to the SQLite database
173
+ conn = sqlite3.connect(db_path)
174
  except sqlite3.Error as e:
175
  st.warning(f"Unable to connect to the SQLite database: {e}")
176
  st.stop()
 
181
  # Prepare the query with proper placeholders
182
  if params:
183
  # Handle the `IN (?)` clause dynamically
184
+ query = query.replace("IN (?)", f"IN ({','.join(['?' for _ in params])})")
185
  c.execute(query, params)
186
  else:
187
  c.execute(query)
 
205
  conn.close()
206
 
207
 
 
 
 
208
  def update_summary_df():
209
  """
210
  Updates the 'project_summary_df' in the session state with the latest project