Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clipping Command #1310

Closed
harshparekhau opened this issue Dec 7, 2023 · 6 comments
Closed

Clipping Command #1310

harshparekhau opened this issue Dec 7, 2023 · 6 comments

Comments

@harshparekhau
Copy link

harshparekhau commented Dec 7, 2023

Please first make sure you have looked at:

Environment

To diagnose, we usually need to know the following, including version numbers. On Windows, be
sure to specify 32-bit Python or 64-bit:

  • Python: 3.12.0
  • pyodbc: '5.0.1'
  • OS: Windows 10 64-bit:
  • DB: SQL Server 2016
  • driver: 2017.177.02.01

Issue

I have done some trouble shooting and found that there is a length limitation in cursor I tried the csv and it worked but couldnt upload a PDF.

Code

import pyodbc,os
import pandas as pd

# Replace the following variables with your actual database connection details
server_name = 'DC3-SQLDEV-01\\VPNR'
database_name = 'Viewpoint'
def pdf_to_byte_array(pdf_file_path):
    with open(pdf_file_path, 'rb') as file:
        byte_array = bytearray(file.read())
    return byte_array

def insert_attachment(byte_data, file_type):#vendor,original_invoice_no,inv_date,description,inv_total):
    # Connect to the SQL Server database using Trusted Connection (Windows Authentication)
    conn_str = f'DRIVER={{SQL Server}};SERVER={server_name};DATABASE={database_name};Trusted_Connection=yes;'
    db_connection = pyodbc.connect(conn_str)
    cursor = db_connection.cursor()

    try:
        # Stored procedure parameters


        # Execute the stored procedure
        cursor.execute("EXEC dbo.uspInsertViewpointAttachment1 @AttachmentData=?, @FileType=? ",(byte_data,file_type))
        #Commit the transaction (if needed)
        cursor.commit()
        print("Stored procedure executed successfully.")
        # cursor.execute("SELECT * from bHQAF WHERE APCo=? AND APRef=?", (pyodbc.Binary(byte_data),file_type))
        # Commit the transaction (if needed)
        
        # result=cursor.fetchval()
        # cursor.commit()
        # keyID=result
        # cursor.commit()
        # print(f"Stored query executed successfully.{keyID}")

        # cursor.close()
        # db_connection.close()
        # return result
    except pyodbc.Error as ex:
        print("Error executing the stored procedure:", ex)
        
    finally:
        # Close the cursor and database connection
        cursor.close()
        db_connection.close()
        return True #keyID
def pdf_to_byte_array(pdf_file_path):
    with open(pdf_file_path, 'rb') as file:
        byte_array = file.read()
    return byte_array
    
os.chdir('B:/ReprocessF/test')
pdf_file_path=r'INV-0111.csv'
byte_array = pdf_to_byte_array(pdf_file_path)
file_type='.csv'
Attachment_ID=insert_attachment(byte_array, file_type)
print(type(byte_array))

print(pdf_file_path)
#print(Attachment_ID)
@harshparekhau
Copy link
Author

using a larger file causes the issue small file works fine

@v-chojas
Copy link
Contributor

v-chojas commented Dec 7, 2023

SQL Server is the old driver that comes with Windows and only supports a subset of current SQL features. According to the driver version you mentioned, try using Driver=ODBC Driver 17 for SQL Server instead.

@gordthompson
Copy link
Collaborator

How large does the file have to be before you start to have problems?

@harshparekhau
Copy link
Author

harshparekhau commented Dec 7, 2023 via email

@harshparekhau
Copy link
Author

How large does the file have to be before you start to have problems?

Not sure exactly. however it worked with a 2kB file and didnt with a 150kB file

@gordthompson
Copy link
Collaborator

This proof-of-concept code shows that there is no such "length limitation in [a pyodbc] cursor", or at least not nearly as low as you report (150kb).

import pyodbc

cnxn = pyodbc.connect(
    "Driver=ODBC Driver 17 for SQL Server;"
    "Server=192.168.0.199;"
    "UID=scott;PWD=tiger^5HHH;"
    "Database=test;",
    autocommit=True,
)
crsr = cnxn.cursor()

crsr.execute("DROP TABLE IF EXISTS my_table")
crsr.execute("CREATE TABLE my_table (id int primary key, att varbinary(max))")

with open("test.pdf", "rb") as f:
    attachment_data = f.read()
print(f"attachment_data parameter is {len(attachment_data):,} bytes")
# attachment_data parameter is 8,407,659 bytes

crsr.execute(
    "INSERT INTO my_table (id, att) VALUES (?, ?)",
    (1, attachment_data),
)
db_len = crsr.execute(
    "SELECT LEN(att) FROM my_table WHERE id = 1"
).fetchval()
print(f"number of bytes stored in database: {db_len:,}")
# number of bytes stored in database: 8,407,659

Your problem is either a driver issue or something wrong with your code.

Repository owner locked and limited conversation to collaborators Dec 8, 2023
@gordthompson gordthompson converted this issue into discussion #1311 Dec 8, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

3 participants