I want to store the data in numpy arrays fetched from the database. I want to make sure that no null value (None) go to the numpy array(throws an error anyway doing that). I have tried to do it the following way but it does not work. For some reason, NullValueCheck() always returns true How can I know about null values and do something about it?
import numpy as np
import pyodbc as odbc
cnxn = odbc.connect(conn_string)
cursor = cnxn.cursor()
cursor.execute("""SELECT ID, BuildingID, Title FROM Something"""")
rows = cursor.fetchall()
cnxn.close()
ID = [i[0] for i in rows]
buildingID = [i[1] for i in rows]
title = [i[2] for i in rows]
def NullValueCheck(rows):
if (any(elem is None for elem in rows[0])):
return True
else:
return False
if NullValueCheck(rows):
ID_array = np.fromiter(ID, dtype= np.int32)
Edit: It turns out that I don't have to write all that code. I can achieve the same using pandas dataframe that I want to achieve from numpy array.
import pandas as pd
import pyodbc as odbc
cnxn = odbc.connect(conn_string)
df = pd.io.sql.read_sql("""SELECT ID, BuildingID, Title FROM Something""", cnxn)