python数据库错误(这里不允许列)

时间:2017-11-19 09:34:35

标签: python cx-oracle

cx_Oracle.DatabaseError:ORA-00984:此处不允许列

cur.execute(insert ...)显示此处不允许列的错误

import cx_Oracle

class Student:
    def __init__(self,studentname,studentperformance):

        connection = cx_Oracle.connect('saif/saif')
        cur = connection.cursor()

        cur.execute('create table student (studentrollno number(10),studentname varchar2(20),studenttype varchar2(20),studentperformance number(5,2),category varchar2(20),bookbank number(1))')

        self.rollno = 171641000
        self.studentname = studentname
        self.studentperformance = studentperformance
        # Line below is showing database error
        cur.execute('insert into student(studentrollno,studentname,studentperformance) values(self.rollno,self.studentname,self.studentperformance)')

s = Student("saif",75)

1 个答案:

答案 0 :(得分:1)

此行尝试在SQL语句中使用Python变量self.rollno等:

 cur.execute('insert into student(studentrollno,studentname,studentperformance) values(self.rollno,self.studentname,self.studentperformance)')

但是因为它们在引号内,所以Python值不会被替换。该字符串逐字发送到数据库,数据库无法理解。

要使用绑定变量,在每个变量的文本中使用如下:bvid之类的占位符,请参阅examples中的一些例如:

sql = 'select * from SampleQueryTab where id = :bvid'

print("Query results with id = 4")
for row in cursor.execute(sql, bvid = 4):
    print(row)
print()