Answer the question
In order to leave comments, you need to log in
SQL conversion error. How to fix?
Good afternoon everyone. Help solve the problem.
1. I connect to the database via pyodbc
2. I try to retrieve data from the table ( I want to get the data of the ACTIVE column from the block named 4-20 )
3. I get an error:
My code:
import pyodbc
def connect_to_base(): # Подключение к БД
global cursor
server = ************
database = ************
username = ************
password = ************
cnxn = pyodbc.connect(
'DRIVER={ODBC Driver 17 for SQL Server};SERVER=' + server + ';DATABASE=' + database + ';UID=' + username +
';PWD=' + password)
cursor = cnxn.cursor()
def select(column, db_table, obj): # Чтение таблиц в БД
cursor.execute('SELECT {} FROM {} WHERE NAME = {}'.format(column, db_table, obj))
row = cursor.fetchone()
while row:
if row[0] != False:
print('Блок {} - активен <<OK OK OK>> [1/1]'.format(obj))
else:
print('Блок {} - неактивен ERROR ERROR ERROR [1/1]')
row = cursor.fetchone()
connect_to_base()
select('ACTIVE', '[dbo].[BLAST_BLOCK]', '4-20')
Answer the question
In order to leave comments, you need to log in
I'm not particularly familiar with pyodbc, but as far as I understand, the problem is passing the value without quotes, i.e. select turns out
SELECT ACTIVE FROM [dbo].[BLAST_BLOCK] WHERE NAME = 4-20
SELECT ACTIVE FROM [dbo].[BLAST_BLOCK] WHERE NAME = '4-20'
cursor.execute('SELECT {} FROM {} WHERE NAME = ?'.format(column, db_table), obj)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question