0

I want to export pandas dataframe to Sybase SQL databse. From here i found the code how to do it link

However, I have the following error.

pyodbc.ProgrammingError: ('42000', '[42000] [Sybase][ODBC Driver]Syntax error or access violation (0) (SQLPrepare)')

Could you help to solve it?

My code -

import pandas as pd
import pyodbc as db
from datetime import datetime

#set constants
DSN = 'DSN'
input_table = 'table'
run_timestamp = str(datetime.now())[:19]
test_date_start = '2020-09-09'
test_date_end = '2025-08-08'
input_data = pd.DataFrame({
    'model':['aaa','aaa'], 'result_type':['a','test_statistic'], 'test_name':['b', 'mwb'], 'input_variable_name':['c','pd'], 'segment':['car','book'],
    'customer_type':['le','le'], 'value':[60, 0.58], 'del_flag':[0,0]
})
query = 'insert into schema.table (data_input_time,test_date_start,test_date_end,model,result_type,test_name,input_variable_name,segment,customer_type,value,del_flag) values (?,?,?,?,?,?,?,?,?,?,?)'



cnxn = db.connect(DSN)
cursor = cnxn.cursor()
cursor.execute('SETUSER MYUSERNAME')

for row_count in range(0, input_data.shape[0]):
    #1 method
    chunk = input_data.iloc[row_count:row_count + 1, :].values.tolist()
    tuple_of_tuples = tuple(tuple(x) for x in chunk)
    cursor.executemany(query, tuple_of_tuples)

    #2 method
    params = [(i,) for i in chunk] #f'txt{i}'
    cursor.executemany(query, params)

1 Answer 1

0

You're using a 'f' string, but forgot the brackets. It should look like this

query = f'''insert into schema.table ({data_input_time},{test_date_start},{test_date_end,model},{result_type,test_name},{input_variable_name},{segment,customer_type},{value,del_flag}) values ('?,?,?,?,?,?,?,?,?,?,?)'''
Sign up to request clarification or add additional context in comments.

1 Comment

thanks. edited the question, 'f' string is changed to simple string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.