使用pymssql将字节插入到Sql服务器变量二进制(Max)列中

2022-04-18 00:00:00 python python-3.5 sql-server pymssql
我正在尝试使用Python3.5和pymssql包将字节或字节数组插入到SQL Server表中。尝试插入后,我不断收到pymssql.ProgrammingError

我已使用以下链接和问题作为参考,但仍有问题:

Explicit Convert of Varbinary(max) column

Insert binary file into MSSQL db (varbinary) with python pymssql

我已验证我有权限并且可以将其他数据类型插入到SQL服务器数据库中。下面是表结构。

Table Structure

尝试01:

sql_cursor = m_sql_conn.cursor()

byte_test_01 = b'This is a byte test'
hex_01 = '0x'.encode('ascii') + binascii.hexlify(byte_test_01)

string_sql_insert = "INSERT INTO CPBB_DevClusterObjs(str_cluster_id, 
    Covert(varbinary(max), obj_cluster_empty)) 
    VALUES('BatchKm|20|k-means++|1'," + hex_01 + ')'

sql_cursor.execute(string_sql_insert)

结果01

pymssql.ProgrammingError

hex_01 = b'0x54686973206973206120627974652074657374'

尝试02

sql_cursor = m_sql_conn.cursor()

byte_test_01 = b'This is a byte test'
hex_01 = '0x'.encode('ascii') + binascii.hexlify(byte_test_01)

list_insert_many = [('BatchKm|20|k-means++|1', hex_01)]

string_sql_insert = "INSERT INTO 
    CPBB_DevClusterObjs(str_cluster_id,Covert(varbinary(max), obj_cluster_empty))  
    VALUES (%s,%b)"

sql_cursor.executemany(str_sql_statement, list_insert_many)

结果02

pymssql.ProgrammingError

hex_01 = b'0x54686973206973206120627974652074657374'

尝试03

我取出Convert()函数和...

sql_cursor = m_sql_conn.cursor()

byte_test_01 = b'This is a byte test'
hex_01 = '0x'.encode('ascii') + binascii.hexlify(byte_test_01)

string_sql_insert = "INSERT INTO CPBB_DevClusterObjs(str_cluster_id,obj_cluster_empty)  
     VALUES ('BatchKm|20|k-means++|1',"  
    + hex_01 + ')'

sql_cursor.execute(string_sql_insert)

结果03

pymssql.ProgrammingError

hex_01 = b'0x54686973206973206120627974652074657374'

我错过了什么,但我不知道是什么。我确实需要一些帮助。

更新01

下面是我在测试方法中的完整代码:

def Test():
    # lists
    list_sql_insert_data_type = ['%s', '%b']
    list_return = list()

    # variables
    string_sql_table = r'CPBB_DevClusterObjs'
    str_sql_error = ''
    user = r'user_me'
    host = r'server_me'
    pswd = r'pswd_me'
    db_name = r'db_me'
    bool_insert_into_table = False

    # sql connection
    list_sql_conn = SqlMethods.SqlGenConnection(user, host, pswd, db_name)
    sql_cursor = list_sql_conn[1].cursor()

    # byte test variables
    byte_test_01 = b'This is a byte test'
    hex_01 = '0x'.encode('ascii') + binascii.hexlify(byte_test_01)

    # create insert string
    string_sql_insert = 'INSERT INTO ' + string_sql_table
    string_sql_insert += '(string_cluster_id, CONVERT(varbinary(max), obj_cluster_empty)) '
    string_sql_insert += "VALUES ('BatchKm|20|k-means++|1'," + "'" + str(hex_01) + "'" + ')'

    # explicit string conversation of hex_01
    string_hex_01 = "'" + str(hex_01) + "'"

    # take out the CONVERT() statement
    #string_sql_insert = 'INSERT INTO ' + string_sql_table
    #string_sql_insert += '(string_cluster_id, obj_cluster_empty) '
    #string_sql_insert += "VALUES ('BatchKm|20|k-means++|1'," + "'" + str(hex_01) + "'" + ')'

    try:
        sql_cursor.execute(string_sql_insert)
    except pymssql.OperationalError:
        str_sql_error = 'Operational error was raised'
    except pymssql.ProgrammingError:
        str_sql_error = 'A program error was raised.'
    except pymssql.Error:
        str_sql_error = 'General error raised.'
    except pymssql.DatabaseError:
        str_sql_error = 'Database error raised.'
    except pymssql.DataError:
        str_sql_error = 'Data error raised.'
    except pymssql.IntegrityError:
        str_sql_error = 'Integrity error raised.'
    except pymssql.InterfaceError:
        str_sql_error = 'Interface error raised.'
    except pymssql.InternalError:
        str_sql_error = 'Internal error raised.'
    except pymssql.NotSupportedError:
        str_sql_error = 'Not supported error raised.'
    except pymssql.StandardError:
        str_sql_error = 'Standard error raised.'
    else:
        bool_insert_into_table = True
        list_sql_conn[1].commit()
    finally:
        list_return.append(bool_insert_into_table)
        list_return.append(str_sql_error)

    # return list
    return list_return

更新01结果

我尝试了您最初推荐的内容,但收到错误"无法隐式将字节转换为字符串",因此我将其显式转换为字符串。

而不是在获取pymssql.OperationalError时出现pymssql.ProgrammingError。

如果我取出Convert()函数,则会得到pymssql.ProgrammingError

hex_01 = 0x54686973206973206120627974652074657374

string_hex_01 = "'" + str(hex_01) + "'" = 'b'0x54686973206973206120627974652074657374''

string_sql_insert = INSERT INTO CPBB_DevClusterObjs(string_cluster_id, 
                    CONVERT(varbinary(max), obj_cluster_empty)) VALUES   
                    ('BatchKm|20|k-means++','b'0x54686973206973206120627974652074657374'')

是否应为STRING_HEX_01:

string_hex_01 = str(hex_01)[1:] = '0x54686973206973206120627974652074657374'

这还会产生pymssql.OperationalError

更新02

我验证了我能够插入到数据库和表中。注意:请确保我的列名是正确的(str_cluster_id,否则就是string_cluster_id)。我仍然收到pymssql.OperationalError。Convert()函数如何适应这种情况。如果我取出它,我会得到一个pymssql。ProgrammingError。

已验证SQL INSERT是否正常工作:

# test insert into string_cluster_id
string_sql_insert = 'INSERT INTO ' + string_sql_table
string_sql_insert += ' (str_cluster_id) '
string_sql_insert += "VALUES ('BatchKm|20|k-means++|1')"

字符串_群集_表=r‘CPBB_DevClusterObjs’

更新03

尝试插入以下字符串:

string_sql_insert = 'INSERT INTO CPBB_DevClusterObjs'
string_sql_insert += '(obj_cluster_empty) '
string_sql_insert += "VALUES ('0x5468697320697320612062797‌​4652074657374')"

结果更新03

pymssql.OperationalError

更新04

成功!!以下是有效的方法。

string_sql_insert = 'INSERT INTO CPBB_DevClusterObjs'
string_sql_insert += '(obj_cluster_empty) '
string_sql_insert += "VALUES (CONVERT(varbinary(max), '0x5468697320697320612062797‌​4652074657374'))"

更新04结果

成功插入到varinary(Max)列中 我将发布其他更新,因为我发现还有哪些可以工作,哪些不能

_MSSQL包&;异常处理

为了理解我如何能够认识到我的错误在哪里,我绕了一圈。我使用_msql包重写了我的测试方法,并按照_http://pymssql.org/en/stable/_mssql_examples.html.

的文档中的指示利用了异常处理

对我来说,关键是了解如何将异常处理作为对象使用,例如:

except _mssql.MSSQLDatabaseException as db_e:
    str_sql_error = 'mssql database error raised'
exc_db_number = db_e.number
exc_db_msg = db_e.message

db_e.text给我以下消息:

不允许从数据类型varchar隐式转换为varinary(Max)。使用CONVERT函数运行此查询。DB-LIB错误消息20018,严重性16:常规SQL Server错误:检查来自SQL Server的消息。

这导致我更改了需要Convert()函数来转换数据的位置,因为我试图转换目标数据库中的列,而该列在目标数据库中已经是varinary(Max)列。

有效的插入字符串为:

string_sql_insert = 'INSERT INTO CPBB_DevClusterObjs'
string_sql_insert += '(obj_cluster_empty) '
string_sql_insert += "VALUES (CONVERT(varbinary(max), '0x5468697320697320612062797‌​4652074657374'))"

吸取的教训

  1. 我是个‘白痴’,没有确保我的列变量是正确的。一定要在发帖前做到这一点。这会有很大帮助的。

  2. 将异常处理用作对象。它将帮助您解决您的错误所在。如果您在离开十多年后重新投入开发/编码,这将有所帮助。

  3. 如果你找不到与你想要实现的目标相匹配的答案,不要害怕寻求帮助。我找到了两个推荐人,但我找不到其中的联系。这个问题会有人作为证据帮助你的。

  4. 请在寻求帮助时尽可能详细和具体。我本来可以在发布我的确切测试代码时做得更好。这可能不会加快这一过程。

    /li>

测试代码的最终版本

下面是测试方法的最终版本,该方法用于将二进制数据插入到Microsoft SQL Server的varinary(Max)列中。

def Test_01():

    # lists
    list_sql_insert_data_type = ['%s', '%b']
    list_return = list()

    # variables
    string_sql_table = r'CPBB_DevClusterObjs'
    str_sql_error = ''
    user = r'user_me'
    host = r'server_me'
    pswd = r'pswd_me'
    db_name = r'db_me'
    bool_insert_into_table = False

    # sql connection
    list_sql_conn = SqlMethods.SqlGenConnection(user, host, pswd, db_name)
    sql_cursor = list_sql_conn[1].cursor()

    # _mssql connection
    _mssql_conn = _mssql.connect(server = host, user = user, password = pswd, database = db_name)

    # byte test variables
    byte_test_01 = b'This is a byte test'
    hex_01 = '0x'.encode('ascii') + binascii.hexlify(byte_test_01)

    # explicit string conversion of hex_01
    string_hex_01 = "'" + str(hex_01) + "'"
    string_hex_02 = str(hex_01)[1:]

    # create insert string
    #string_sql_insert = 'INSERT INTO CPBB_DevClusterObjs'
    #string_sql_insert += '(str_cluster_id, obj_cluster_empty) '
    #string_sql_insert += "VALUES ('BatchKm|20|k-means++|1','0x5468697320697320612062797‌​4652074657374')"

    # sucess!!  below insert works
    string_sql_insert = 'INSERT INTO CPBB_DevClusterObjs'
    string_sql_insert += '(obj_cluster_empty) '
    string_sql_insert += "VALUES (CONVERT(varbinary(max), '0x5468697320697320612062797‌​4652074657374'))"

    # test insert into string_cluster_id
    #string_sql_insert = 'INSERT INTO ' + string_sql_table
    #string_sql_insert += ' (str_cluster_id) '
    #string_sql_insert += "VALUES ('BatchKm|20|k-means++|1')"

    # take out the CONVERT() statement
    #string_sql_insert = 'INSERT INTO ' + string_sql_table
    #string_sql_insert += '(str_cluster_id, obj_cluster_empty) '
    #string_sql_insert += "VALUES ('BatchKm|20|k-means++|1'," + "'" + str(hex_01) + "'" + ')'

    try:
        sql_cursor.execute(string_sql_insert)
    except pymssql.OperationalError as oe:
        str_sql_error = 'Operational error was raised'
    except pymssql.ProgrammingError:
        str_sql_error = 'A program error was raised.'
    except pymssql.Error:
        str_sql_error = 'General error raised.'
    except pymssql.DatabaseError:
        str_sql_error = 'Database error raised.'
    except pymssql.DataError:
        str_sql_error = 'Data error raised.'
    except pymssql.IntegrityError:
        str_sql_error = 'Integrity error raised.'
    except pymssql.InterfaceError:
        str_sql_error = 'Interface error raised.'
    except pymssql.InternalError:
        str_sql_error = 'Internal error raised.'
    except pymssql.NotSupportedError:
        str_sql_error = 'Not supported error raised.'
    except pymssql.StandardError:
        str_sql_error = 'Standard error raised.'
    else:
        bool_insert_into_table = True
        list_sql_conn[1].commit()
    finally:
        list_return.append(bool_insert_into_table)
        list_return.append(str_sql_error)

    # return list
    return list_return

解决方案

尝试将您的字符串_SQL_INSERT修改为:

string_sql_insert = "INSERT INTO CPBB_DevClusterObjs(str_cluster_id, 
CONVERT(varbinary(max), obj_cluster_empty)) 
VALUES('BatchKm|20|k-means++|1'," + "'" + hex_01 + "'"')'

您正在插入以下内容:

INSERT INTO CPBB_DevClusterObjs(str_cluster_id, obj_cluster_empty)     VALUES('BatchKm|20|k-means++|1',0x54686973206973206120627974652074657374)

而不是这个:

INSERT INTO CPBB_DevClusterObjs(str_cluster_id, obj_cluster_empty)     VALUES('BatchKm|20|k-means++|1','0x54686973206973206120627974652074657374')

如果查看值,就会发现它引发错误的原因。

根据https://github.com/pymssql/pymssql/pull/179/files

def insert_and_select(self, cname, value, vartype, params_as_dict=False):

vartype为"%s"

def test_binary_string(self):
    bindata = '{z
x03x07x194;x034lE4ISo'.encode('ascii')
    testval = '0x'.encode('ascii') + binascii.hexlify(bindata)
    colval = self.insert_and_select('data_binary', testval, 's')
    self.typeeq(bindata, colval)
    eq_(bindata, colval)

相关文章