This is a script written to analyse the reputation of files by the file hash. File hash is taken from Azure SQL database and analysed by calling APIs and the result is entered again to the database. I am using ODBC Driver 17 for SQL Server.
I am getting an error while re-entering the data to the database.
# Scanning files
def fileScan():
select_sql = "SELECT source_hash FROM [Downloads]"
crsr.execute(select_sql)
rows = crsr.fetchall()
# Read all rows
for row in rows:
print(''.join(row))
response = vt.get_file_report(''.join(row))
time.sleep(15)
source_hash = row
md5 = json.dumps(response['results']['md5'])
sha1 = json.dumps(response['results']['sha1'])
sha256 = json.dumps(response['results']['sha256'])
detections = json.dumps(response['results']['positives'])
query = (
"UPDATE Downloads SET md5=(?), sha1=(?), sha256=(?), detections=(?)"
"WHERE source_hash=(?)")
crsr.execute(query, (md5,sha1,sha256,detections,source_hash))
crsr.commit()
Below is the error.
461a26ad4219ea5e504fcdd1a61beb80b1a7fba8ff760e6696aa8cc3a3937887 is the hash value retrieved from the database to analyse.
Data type of the source_hash in the database is VARCHAR(1024).
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]
Column, parameter, or variable #5: Cannot find data type 461a26ad4219ea5e504fcdd1a61beb80b1a7fba8ff760e6696aa8cc3a3937887. (2715)
(SQLExecDirectW); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s) could not be prepared. (8180);
[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Parameter or variable '@P5' has an invalid data type. (2724)")
Although similar errors are posted in many sources, no exact solution was mentioned.