I am getting the following error when I try to connect to the CRM system using dsn, trying to get all data in batches:
The Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click here for more info. View Jupyter log for further details.
Firstly I tried to test if the code is working without trying to get all the data at once or without using batches.
I tried to test using the following code to see if it will work and the code is working as I expected:
import pandas as pd
import json
import requests
import pandas as pd
import pyodbc
from sqlalchemy import create_engine, inspect
import urllib.parse
import warnings
warnings.filterwarnings("ignore")
dsn = f"DSN={os.getenv('DSN')};UID={os.getenv('UID')};PWD={os.getenv('PWD')}"
try:
# Establish a connection to the FileMaker database
conn = pyodbc.connect(dsn)
conn.setdecoding(pyodbc.SQL_CHAR, encoding='latin1')
conn.setdecoding(pyodbc.SQL_WCHAR, encoding='latin1')
conn.setencoding(encoding='latin1')
# Query the table
query = "SELECT * FROM Sales_2022 FETCH FIRST 5 ROWS ONLY"
# Read data into a DataFrame
df = pd.read_sql(query, conn)
# Close the connection
conn.close()
# Print the DataFrame
print(df)
except pyodbc.Error as e:
print("Error:", e)
except Exception as e:
print("An unexpected error occurred:", e)
However, when I want to try to get all data in batches using the code below, that is when the Kernel is crashing. I am using python 3.9.5 and the installed RAM is 16,0 GB (15,7 GB usable), I am also using Windows 11 Pro. What could be causing the Kernel to crash?
Here is the code I am trying to use:
import pandas as pd
import json
import requests
import pandas as pd
import pyodbc
from sqlalchemy import create_engine, inspect
import urllib.parse
import warnings
warnings.filterwarnings("ignore")
dsn = f"DSN={os.getenv('DSN')};UID={os.getenv('UID')};PWD={os.getenv('PWD')}"
conn = pyodbc.connect(dsn)
""" # Create an SQLAlchemy engine
engine = create_engine("mssql+pyodbc:///?odbc_connect=" + urllib.parse.quote_plus(dsn))
"""
conn.setdecoding(pyodbc.SQL_CHAR, encoding='latin1')
conn.setdecoding(pyodbc.SQL_WCHAR, encoding='latin1')
conn.setencoding(encoding='latin1')
chunk_size = 10000
offset = 0
data = []
while True:
query = f"""
SELECT * FROM Sales_2022
"""
chunk_df = pd.read_sql(query, conn)
# Check if there are no more records
if chunk_df.empty:
break
# Filter the chunk based on offset and chunk size
chunk_df = chunk_df[offset:offset+chunk_size]
# Append the chunk to the data list
data.append(chunk_df)
# Update the offset
offset += chunk_size
if data:
df = pd.concat(data, ignore_index=True)
else:
df = pd.DataFrame()