首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何修正“OracleOra-01036有问题:非法变量名称/编号”的错误?

如何修正“OracleOra-01036有问题:非法变量名称/编号”的错误?
EN

Stack Overflow用户
提问于 2022-03-07 20:42:36
回答 1查看 112关注 0票数 1

我一直在编写关于为值Blob插入值的代码,因为我无法单独在oracle数据库中这样做,这使我不得不用Python编写它,这似乎是唯一可行的方法。我一直在寻找其他解决方案,但在尝试之后,我似乎还没有为我的程序找到解决方案。我要做的是插入。希望这有意义。

代码语言:javascript
运行
复制
# 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', 'MasterGrill0@gmail.com', '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", "MasterGrill0@gmail.com", "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")
EN

回答 1

Stack Overflow用户

发布于 2022-03-07 21:57:06

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

代码语言:javascript
运行
复制
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:key2等。

%s由MySQL驱动程序使用。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71387022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档