我的系统上有一个版本11g和python 3的oracle。但是,当我尝试运行某个插入查询时,它不会起作用。它总是抛出一个未连接的异常。我检查了我的连接,并且选择的查询工作正常。
我的最终目标是在数据库中注册用户,然后返回其行ID。
我做了什么?
def db_register_applicant(applicant_name,password,years_of_experience,prefered_technology,skill_set):
try:
con=DBConnectivity.create_connection()
cur=DBConnectivity.create_cursor(con)
skills=skill_set.split(",")
db_skills=[]
applicant_id_db=None
cur.execute("select skill_name from skills")
for skill_name in cur:
db_skills.append(skill_name)
final_skills=[]
for x in skills:
if x not in db_skills:
final_skills.append(x)
cur.execute("select nvl(max(applicant_id),0) from applicant")
last_row_id=int(cur.fetchone()[0]) // works till here, if i print this, it prints 7
query="insert into applicant (applicant_id,applicant_name,password,years_of_experience,prefered_technology) values ('%d','%s','%s','%d','%s')"%\
(last_row_id+1,applicant_name,password,years_of_experience,prefered_technology,skill_set)
cur.execute(query)
applicant_id_db=last_row_id+1
for x in final_skills:
cur.execute("select nvl(max(skill_id),0) from skills")
skill_max=int(cur.fetchone()[0])
print(skill_max)
query="insert into skills values (skill_id,skill_name) values ('%d','%s')"%\
(skill_max+1,x)
cur.execute(query)
con.commit()
finally:
con.close()
cur.close()
return applicant_id_db
此功能从此功能调用:
def register():
try:
print("Enter the applicant name:")
name=input()
print("Enter the password:")
password=input()
print("Enter your skill set:")
skill_set=input()
print("Enter the number of years of experience:")
experience=int(input())
print("Enter prefered technology:")
prefered_tech=input()
if check_experience(experience) and check_password(password):
applicant_id=db_register_applicant(name, password, experience, prefered_tech, skill_set)
print("Your applicant id is :"+applicant_id)
except Exception as e:
print("Sorry,Some system error occured")
print(e)
输出
抱歉,发生了一些系统错误
未连接
我的连接文件代码:
import cx_Oracle
def create_connection():
return cx_Oracle.Connection('something/something@ip/somethingother')
def create_cursor(con):
return cx_Oracle.Cursor(con)
我的代码出了什么问题?
更新
更改了db_register_applicant
中的finally语句,现在它不会抛出任何异常,但插入仍然无法正常工作。
finally:
cur.close()
con.close()
return applicant_id_db