【问题标题】:how to retrieve data from blob column data using cx_oracle?如何使用 cx_oracle 从 blob 列数据中检索数据?
【发布时间】:2020-11-09 16:45:56
【问题描述】:

我已将二进制数据存储到 blob 中的 oracle 数据库中。但我无法检索相同的数据。这是我的代码

app=input("\nEnter the name of the app: ")
username=input("\nEnter the username: ")
cur.execute("select password from passwordM where app=:va and username=:vu", va=app, vu=username)
if cur.fetchone()==None:
    print("\nNo entry exists with app: '{}' and username: '{}'\nCheck again and enter correct details!!".format(app,username))
    continue
for x in cur:
    ctext=x[0]
print(RSADecryption(ctext))

属性 'app' 和 'username' 是 varchar2 类型,而 'password' 是 blob。
这就是我得到的错误。

Traceback (most recent call last):
  File "C:\Users\admin\Desktop\MLI-22 Project\main.py", line 88, in <module>
    print(RSADecryption(ctext))
NameError: name 'ctext' is not defined

【问题讨论】:

    标签: python oracle blob


    【解决方案1】:

    虽然我不太明白您使用 BLOB 存储密码是什么,但无论如何您可能想使用utl_raw.cast_to_raw

    看这个例子

    SQL> create table my_test ( c1 varchar2(1) , c2 blob ) ;
    
    Table created.
    
    SQL> insert into my_test values ( 1 , empty_blob() ) ;
    
    1 row created.
    
    SQL> commit;
    
    Commit complete.
    
    SQL> update my_test set c2=utl_raw.cast_to_raw ( 'MyPassword' ) ;
    
    1 row updated.
    
    SQL> commit;
    
    Commit complete.
    
    SQL> select * from my_test ;
    
    C
    -
    C2
    --------------------------------------------------------------------------------
    1
    4D7950617373776F7264
    
    
    SQL> select utl_raw.cast_to_varchar2(c2) from my_test ;
    
    UTL_RAW.CAST_TO_VARCHAR2(C2)
    --------------------------------------------------------------------------------
    MyPassword
    
    SQL>
    

    包括在 Python cx_oracle 8.0 上,也许你需要使用 blobdata = xxxx

    with open('example.txt', 'r') as f:
    textdata = f.read()
    
    with open('image.png', 'rb') as f:
        imgdata = f.read()
    
    cursor.execute("""
            insert into lob_tbl (id, c, b)
            values (:lobid, :clobdata, :blobdata)""",
            lobid=10, clobdata=textdata, blobdata=imgdata)
    

    尽管您没有在代码中提供任何更新语句,但您可以尝试直接将 utl_raw.cast_to_raw 直接应用于将存储为 blob 的字符串。

    另一方面,它与问题本身没有适当的关系,以这种方式存储密码是一种非常糟糕的做法。您应该查看 DBMS_CRYPTO 以便使用密钥存储加密密码。

    【讨论】:

    • 我正在使用带有 pycryptodome 库的 RSA 算法。加密数据是二进制的,这就是我使用 blob 的原因。
    • 您是否尝试在更新之前将 utl_raw.cast_to_raw 应用于 blob?
    • 我使用绑定变量插入数据:cur.execute("insert into passwordM (app, username, password) values(:v1, :v2, :v3)", v1=app, v2=username, v3=ciphertext)
    • 改用这个来看看它是否有效:cur.execute("insert into passwordM (app, username, password) values(:v1, :v2, utl_raw.cast_to_raw(:v3) )", v1=应用程序,v2=用户名,v3=密文)
    【解决方案2】:

    我已经找到了我做错的地方。因为我已经调用了 fetchone(),所以 x 将为 None。当我这样写时它会起作用:

    app=input("\nenter the name of the app: ")
    username=input("\nEnter the username: ")
    cur.execute("select password from passwordM where app=:va and username=:vu", va=app, vu=username)
    if cur.fetchone()==None:
       print("\nNo entry exists with app: '{}' and username: '{}'\nCheck again and enter correct details!!".format(app,username))
       continue
    cur.execute("select password from passwordM where app=:va and username=:vu", va=app, vu=username)
    for x in cur:
       ciphertext=x[0]
    print(RSADecryption(ciphertext))
    

    【讨论】:

    • 这不是你问题的标题吗??? --> 如何使用 cx_oracle 更新 blob 列数据?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 2016-05-28
    • 2011-09-09
    • 2012-05-03
    • 1970-01-01
    • 2020-01-23
    相关资源
    最近更新 更多