如何修复错误“Oracle ORA-01036 存在问题:非法变量名/编号”?

发布于 2025-01-12 06:15:04 字数 2435 浏览 0 评论 0原文

我一直在编写有关为 Blob 值插入值的代码,因为我无法在 Oracle 数据库中单独完成它,这让我用 Python 编写它,这似乎是唯一可能的方法。我一直在寻找其他解决方案,但在尝试之后,我似乎还没有找到适合我的程序的解决方案。我正在做的是插入。希望这是有道理的。

# importing module
import cx_Oracle


def convertToBinaryData(filename):
    # Convert digital data to binary format
    with open(filename, 'rb') as file:
        binaryData = file.read()
    return binaryData


def insertBLOB(r_id, r_name, r_email, r_address, r_description, r_hpno, r_whatsapp, r_peakhour, r_rating, r_cert):
    # Create a table in Oracle database
    try:

        con = cx_Oracle.connect('Hazim11/87097980@localhost')
        # Now execute the sqlquery
        cursor = con.cursor()
        cert_pic = convertToBinaryData(r_cert)
        print(con.version)

        sql_insert_blob_query = """INSERT INTO RESTAURANT (R_ID,R_NAME,R_EMAIL,R_ADDRESS,R_DESCRIPTION,R_HPNO,R_WHATSAPP,R_PEAKH,R_RATING,R_CERT) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"""

        insert_blob_tuple = (r_id, r_name, r_email, r_address, r_description,
                             r_hpno, r_whatsapp, r_peakhour, r_rating, cert_pic)

        cursor.execute(sql_insert_blob_query, insert_blob_tuple)

        con.commit()

        # Creating a table employee
        # cursor.execute("INSERT INTO RESTAURANT (R_ID, R_NAME, R_EMAIL, R_ADDRESS, R_DESCRIPTION, R_HPNO, R_WHATSAPP, R_PEAKH, R_RATING, R_CERT) VALUES ('R100', 'MasterGrill_0', '[email protected]', '145, Jalan Desa Taman Desa, 58100, Kuala Lumpur', 'Clean and Friendly Staff', '030-1110 3333', '020-1110 3333', '8:00 PM', '8/10',", cert_pic)

        print("INSERT VALUES OF RESTAURANT SUCCESSFUL")

    except cx_Oracle.DatabaseError as e:
        print("There is a problem with Oracle", e)

    # by writing finally if any error occurs
    # then also we can close the all database operation
    finally:
        if cursor:
            cursor.close()
        if con:
            con.close()


insertBLOB("R100", "MasterGrill_0", "[email protected]", "145, Jalan Desa Taman Desa, 58100, Kuala Lumpur", "Clean and Friendly Staff",
           "030-1110 3333", "020-1110 3333", "8:00 PM", "8/10", r"C:\Users\HP\Pictures\Dota 2 Wallpapers\Wallpaper PC.jpg")

I have been working on this code about inserting values for the value Blob because I can't do it inside the oracle database by itself which brought me to write it in Python and that seems to be the only possible way. I have been looking at other solutions but after trying them I haven't seemed to find the solution for my program. What I'm doing is INSERTING. Hope it makes sense.

# importing module
import cx_Oracle


def convertToBinaryData(filename):
    # Convert digital data to binary format
    with open(filename, 'rb') as file:
        binaryData = file.read()
    return binaryData


def insertBLOB(r_id, r_name, r_email, r_address, r_description, r_hpno, r_whatsapp, r_peakhour, r_rating, r_cert):
    # Create a table in Oracle database
    try:

        con = cx_Oracle.connect('Hazim11/87097980@localhost')
        # Now execute the sqlquery
        cursor = con.cursor()
        cert_pic = convertToBinaryData(r_cert)
        print(con.version)

        sql_insert_blob_query = """INSERT INTO RESTAURANT (R_ID,R_NAME,R_EMAIL,R_ADDRESS,R_DESCRIPTION,R_HPNO,R_WHATSAPP,R_PEAKH,R_RATING,R_CERT) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"""

        insert_blob_tuple = (r_id, r_name, r_email, r_address, r_description,
                             r_hpno, r_whatsapp, r_peakhour, r_rating, cert_pic)

        cursor.execute(sql_insert_blob_query, insert_blob_tuple)

        con.commit()

        # Creating a table employee
        # cursor.execute("INSERT INTO RESTAURANT (R_ID, R_NAME, R_EMAIL, R_ADDRESS, R_DESCRIPTION, R_HPNO, R_WHATSAPP, R_PEAKH, R_RATING, R_CERT) VALUES ('R100', 'MasterGrill_0', '[email protected]', '145, Jalan Desa Taman Desa, 58100, Kuala Lumpur', 'Clean and Friendly Staff', '030-1110 3333', '020-1110 3333', '8:00 PM', '8/10',", cert_pic)

        print("INSERT VALUES OF RESTAURANT SUCCESSFUL")

    except cx_Oracle.DatabaseError as e:
        print("There is a problem with Oracle", e)

    # by writing finally if any error occurs
    # then also we can close the all database operation
    finally:
        if cursor:
            cursor.close()
        if con:
            con.close()


insertBLOB("R100", "MasterGrill_0", "[email protected]", "145, Jalan Desa Taman Desa, 58100, Kuala Lumpur", "Clean and Friendly Staff",
           "030-1110 3333", "020-1110 3333", "8:00 PM", "8/10", r"C:\Users\HP\Pictures\Dota 2 Wallpapers\Wallpaper PC.jpg")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御守 2025-01-19 06:15:04

cx-oracle 不使用 %s 作为占位符。它使用编号或命名的占位符。如果参数在序列中,请使用数字::1:2 等。

sql_insert_blob_query = """
    INSERT INTO RESTAURANT (R_ID,R_NAME,R_EMAIL,R_ADDRESS,R_DESCRIPTION,R_HPNO,R_WHATSAPP,R_PEAKH,R_RATING,R_CERT) 
    VALUES (:1, :2, :3, :4, :5, :6, :7, :8, :9)"""

如果参数在字典中,请使用名称::key1< /code>、:key2 等。

%s 由 MySQL 驱动程序使用。

cx-oracle doesn't use %s for placeholders. It uses either numbered or named placeholders. If the parameters are in a sequence, use numbers: :1, :2, etc.

sql_insert_blob_query = """
    INSERT INTO RESTAURANT (R_ID,R_NAME,R_EMAIL,R_ADDRESS,R_DESCRIPTION,R_HPNO,R_WHATSAPP,R_PEAKH,R_RATING,R_CERT) 
    VALUES (:1, :2, :3, :4, :5, :6, :7, :8, :9)"""

If the parameters are in a dictionary, use names: :key1, :key2, etc.

%s is used by MySQL drivers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文