Reputation: 29
How to avoid creating table again and again in python using Oracle database? Every time I call the function CREATE table query is executed and data is not inserted because the table already exists.
import cx_Oracle
import time
def Database(name,idd,contact):
try:
con = cx_Oracle.connect('arslanhaider/12345@AHS:1521/XE')
cur = con.cursor()
cur.execute("CREATE TABLE Mazdoor(Name varchar(255),EmpID INT,ContactNo INT)")
cur.execute("INSERT INTO Mazdoor VALUES(:1, :2, :3)",( name,idd,contact))
con.commit()
cur.execute("SELECT * FROM Mazdoor")
data = cur.fetchall()
for row in data:
print(row)
except cx_Oracle.Error:
if con:
con.rollback()
finally:
if con:
con.close()
if__name__="__main__"
while True:
n=input("Enter Name::")
i=input("Enter Idd::")
c=input("Enter Contact No::")
Database(n,i,c)
time.sleep(3)
print("Record Successfully Stored......\n\n")
Upvotes: 0
Views: 61
Reputation: 8945
"Obviously, (koff, koff ...) you must know what you are doing!"
If you ask Oracle to CREATE TABLE
, knowing in advance that the table might already exist, then your logic should at least be prepared ... through the use of multiple try..except..finally
blocks as appropriate, to handle this situation.
If the CREATE TABLE
statement fails because the table already exists, then you can be quite sure that an exception will be thrown, and that you, in the relevant except
clause, can determine that "this, indeed, is the reason." You might reasonably then choose to ignore this possibility, and to "soldier on."
Upvotes: 1