Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyodbc the sql contains 0 parameter markers but 1 parameters were supplied' 'hy000'

I am using Python 3.6, pyodbc, and connect to SQL Server.

I am trying make connection to a database, then creating a query with parameters.

Here is the code:

import sys
import pyodbc

# connection parameters
nHost = 'host'
nBase = 'base'
nUser = 'user'
nPasw = 'pass'

# make connection start
def sqlconnect(nHost,nBase,nUser,nPasw):
    try:
        return pyodbc.connect('DRIVER={SQL Server};SERVER='+nHost+';DATABASE='+nBase+';UID='+nUser+';PWD='+nPasw)
        print("connection successfull")
    except:
        print ("connection failed check authorization parameters")  
con = sqlconnect(nHost,nBase,nUser,nPasw)
cursor = con.cursor()
# make connection stop

# if run WITHOUT parameters THEN everything is OK   
ask = input ('Go WITHOUT parameters y/n ?')
if ask == 'y':
    # SQL without parameters start
    res = cursor.execute('''
    SELECT * FROM TABLE 
    WHERE TABLE.TIMESTAMP BETWEEN '2017-03-01T00:00:00.000' AND '2017-03-01T01:00:00.000'
    ''')
    # SQL without parameters stop

    # print result to console start
    row = res.fetchone()
    while row:
        print (row)
        row = res.fetchone()
    # print result to console stop

# if run WITH parameters THEN ERROR
ask = input ('Go WITH parameters y/n ?') 
if ask == 'y':

    # parameters start
    STARTDATE = "'2017-03-01T00:00:00.000'"
    ENDDATE = "'2017-03-01T01:00:00.000'"
    # parameters end

    # SQL with parameters start
    res = cursor.execute('''
    SELECT * FROM TABLE 
    WHERE TABLE.TIMESTAMP BETWEEN :STARTDATE AND :ENDDATE
    ''', {"STARTDATE": STARTDATE, "ENDDATE": ENDDATE})
    # SQL with parameters stop

    # print result to console start
    row = res.fetchone()
    while row:
        print (row)
        row = res.fetchone()
    # print result to console stop

When I run the program without parameters in SQL, it works.

When I try running it with parameters, an error occurred.

like image 322
Aleks Avatar asked Apr 19 '17 09:04

Aleks


3 Answers

Parameters in an SQL statement via ODBC are positional, and marked by a ?. Thus:

# SQL with parameters start
res = cursor.execute('''
SELECT * FROM TABLE 
WHERE TABLE.TIMESTAMP BETWEEN ? AND ?
''', STARTDATE, ENDDATE)
# SQL with parameters stop

Plus, it's better to avoid passing dates as strings. Let pyodbc take care of that using Python's datetime:

from datetime import datetime
...
STARTDATE = datetime(year=2017, month=3, day=1)
ENDDATE = datetime(year=2017, month=3, day=1, hour=0, minute=0, second=1)

then just pass the parameters as above. If you prefer string parsing, see this answer.

like image 50
themiurge Avatar answered Oct 26 '22 15:10

themiurge


i tryied and have a lot of different errors: 42000, 22007, 07002 and others

The work version is bellow:

import sys
import pyodbc
import datetime

# connection parameters
nHost = 'host'
nBase = 'DBname'
nUser = 'user'
nPasw = 'pass'

# make connection start
def sqlconnect(nHost,nBase,nUser,nPasw):
    try:
        return pyodbc.connect('DRIVER={SQL Server};SERVER='+nHost+';DATABASE='+nBase+';UID='+nUser+';PWD='+nPasw)
    except:
        print ("connection failed check authorization parameters")  
con = sqlconnect(nHost,nBase,nUser,nPasw)
cursor = con.cursor()
# make connection stop

STARTDATE = '11/2/2017'
ENDDATE = '12/2/2017'
params = (STARTDATE, ENDDATE)

# SQL with parameters start
sql = ('''
SELECT * FROM TABLE 
WHERE TABLE.TIMESTAMP BETWEEN CAST(? as datetime) AND CAST(? as datetime)
''')
# SQL with parameters stop

# print result to console start
query = cursor.execute(sql, params)
row = query.fetchone()
while row:
    print (row)
    row = query.fetchone()
# print result to console stop  
say = input ('everething is ok, you can close console')
like image 28
Aleks Avatar answered Oct 26 '22 15:10

Aleks


If you're trying to use pd.to_sql() like me I fixed the problem by passing a parameter called chunksize.

df.to_sql("tableName", engine ,if_exists='append', chunksize=50)

hope this helps

like image 5
Kevin Ruder Avatar answered Oct 26 '22 14:10

Kevin Ruder