检查 pyodbc 列中精确字符串的出现(频率)
Check occurrence (frequency) of exact string in a column in pyodbc
所以我需要创建一个简单的函数来检查列中特定字符串 'c' 的出现次数,然后除以总数。
之前没有使用过 pyodbc,一直卡在一开始。
def get_c_i(c):
cursor.execute('select count (*) from conditions')
total = cursor.fetchone()[0]
print(total)
cursor.execute('select Name from Conditions where Name LIKE c')
freq = cursor.fetchone()[0]
print(freq)
cursor.close()
Traceback (most recent call last):
File "F:/Work/db/eyetoai/eyetoai/database/predict.py", line 34, in <module>
get_c_i('Fibroadenoma')
File "F:/Work/db/eyetoai/eyetoai/database/predict.py", line 27, in get_c_i
cursor.execute('select Name from Conditions where Name LIKE c')
pyodbc.ProgrammingError: ('42S22', "[42S22] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid column name 'c'. (207) (SQLExecDirectW)")
您似乎想要这样的东西:
cursor.execute("SELECT COUNT(*) FROM Conditions")
total = cursor.fetchval()
cursor.execute("SELECT COUNT(*) FROM Conditions WHERE Name=?", "c")
freq = cursor.fetchval()
print("{0} / {1} = {2}".format(freq, total, freq/total)) # check results
所以我需要创建一个简单的函数来检查列中特定字符串 'c' 的出现次数,然后除以总数。 之前没有使用过 pyodbc,一直卡在一开始。
def get_c_i(c):
cursor.execute('select count (*) from conditions')
total = cursor.fetchone()[0]
print(total)
cursor.execute('select Name from Conditions where Name LIKE c')
freq = cursor.fetchone()[0]
print(freq)
cursor.close()
Traceback (most recent call last):
File "F:/Work/db/eyetoai/eyetoai/database/predict.py", line 34, in <module>
get_c_i('Fibroadenoma')
File "F:/Work/db/eyetoai/eyetoai/database/predict.py", line 27, in get_c_i
cursor.execute('select Name from Conditions where Name LIKE c')
pyodbc.ProgrammingError: ('42S22', "[42S22] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid column name 'c'. (207) (SQLExecDirectW)")
您似乎想要这样的东西:
cursor.execute("SELECT COUNT(*) FROM Conditions")
total = cursor.fetchval()
cursor.execute("SELECT COUNT(*) FROM Conditions WHERE Name=?", "c")
freq = cursor.fetchval()
print("{0} / {1} = {2}".format(freq, total, freq/total)) # check results