data is not inserted into oracle database from python - python-3.x

Hi i'm a newbie to coding.i have written a code for registration form and the code run without an error.
def clicked():
try:
con = cx_Oracle.connect('app/reshmaa#10.23.129.216/XE')
cur = con.cursor()
emp_id=txt.get()
first_name=txt1.get()
last_name=txt2.get()
email_id=txt3.get()
"""if re.match("^[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$",email_id,re.IGNORECASE):
email_id=txt3.get()"""
mobile_no=int(txt4.get())
last_update=str(date.today())
statement="insert into registration(emp_id, first_name, last_name, email_id, mobile_no, last_update, pass_word) values('%s', '%s', '%s', '%s','%s',to_date('%s','dd/mm/yyyy'),'%s')"%
(emp_id, first_name,last_name,email_id,mobile_no, last_update, pass_word)
cur.execute(statement)
con.commit()
max_char= 8
allchar= string.ascii_letters + string.digits + '!##$%&*'
random.seed=(os.urandom(16))
pass_word= "".join(random.choice(allchar) for x in range(max_char))
messagebox.showinfo("Success","Your password is "+ pass_word)
except:
messagebox.showerror("Error","Check Your Details")
btn = Button(window, text="Register", bg="dodgerblue4", fg="silver", command=clicked)
btn.grid(column=5, row=7)
When i click register button it shows the error message box in except statement and stops. It does not show any error in the python shell(version 3.6.2) and the data entered is not inserted into my oracle database table.
can someone help me with what error has been made in my code?

Related

Python - Deleting Treeview record from table & SQLite

I'm currently trying to make a 'delete record' button that deletes a treeview record, this part works within the treeview table, however I am unable to get the record in the SQLite database to be deleted aswell.
def deleterec():
try:
curItem = tree1.focus()
valueList=tree1.item(curItem,'values')
global selectedItem2
selectedItem2=valueList[0]
prodID = tree1.selection()[0] # get selected item from tree
messageDelete = messagebox.askyesno("Confirmation", "Do you want to permanently delete this record?")
if messageDelete > 0:
# Remove one record
tree1.delete(prodID)
print(selectedItem2) #this prints the accurate productID that's in my SQLite database
global conn87
conn87 = sqlite3.connect('Sqlite_Python.db')
#Create cursor instant
global c87
c87 = conn87.cursor()
print("Successfully Connected to SQLite")
sqlite_delete_queryL = str(f"""DELETE from stockDatabase WHERE productID='%s';""") % selectedItem2
c87.execute(str(sqlite_delete_queryL))
c87.close()
except sqlite3.Error as error:
print("Error: ", error)
finally:
if conn87:
conn87.close()
print("sqlite connection is closed")
My code is not outputting any errors and it is giving the confirmation that the connection is successfully made then closed, however the query is not complete.............

relation does not exist in postgreSQL but already exist

I've read a lot of articles about my problem but no one solve it. So u can see my code here
DATABASE_URL = os.environ.get('url_of_my_db')
con = None
try:
con = psycopg2.connect(DATABASE_URL)
cur = con.cursor()
print('PostgreSQL database version:')
#cur.execute('SELECT version()')
#cur.execute('SELECT * FROM qwerty')
#cur.execute(sql.SQL('SELECT * FROM {}').format(sql.Identifier('qwerty')))
#cur.execute(sql.SQL("INSERT INTO {} (chat_id, username, created_on) VALUES (8985972942, vovakirdan, 2022-01-05)").format(sql.Identifier('users')))
cur.execute("""INSERT INTO users (chat_id, username, created_on)
VALUES (3131,
vovakirdan,
2022-01-05)""")
# display the PostgreSQL database server version
db_version = cur.fetchone()
print(db_version)
# close the communication with the HerokuPostgres
cur.close()
except Exception as error:
print('Cause: {}'.format(error))
finally:
# close the communication with the database server by calling the close()
if con is not None:
con.close()
print('Database connection closed.')
and in my DB table named "users" (named without quotes) are exist, but I still have this error:
error
...relation "users" does not exist
all the #commented code doesn't work and send the same error besides SELECT version(), it works perfectly that proves that connection works.
The problem was that PostgreDB wants me to use SELECT colum FROM schema.table instead of SELECT colum FROM table. And that's all. Thanks everyone

Updating part of sqlite3 table using variables

ulogin=str(self.t1.get())
med=str(self.mymed.get("1.0",END))
print(med)
conn = connect("med.db")
c = conn.cursor()
c.execute("UPDATE user_details SET reminder ="+ med +"WHERE username =" + ulogin)
conn.commit
conn.close()
im trying to get this to update a text wall however it breaks continuously
line 284 on GitHub
link: https://github.com/Polar001/medicine-reminder/commit/0f6e74a1103937a24c67af3e8f07a2bcf0975f0a
if possible be as basic as possible im not good at coding at all
Your SQL statement is incorrectly constructed, use placeholder as below:
c.execute("UPDATE user_details SET reminder = ? WHERE username = ?", (med, ulogin))

Error while fetching data from PostgreSQL column "user2" does not exist

My Django views.py passes a email address to a python function. The
python function queries a PostgreSQL table using a where clause. The
where clause is equal to the email address that was passed in
(someuser). My goal is to retrieve the userid value for this email
address. But I get an error message telling me that there is no column
called "user2" which is correct (there is not). But why does the
select see that as an existing column?
I print the email out only to verify that it got passed in. someuser
for select= user2#gmail.com
Error while fetching data from PostgreSQL
column "user2" does not exist LINE 1: SELECT userid from accounts_user
WHERE email = user2#gmail.c...
I have ran this locally on my Windows 10 box as well as on AWS Cloud9
with the same result. It tells me that no column exists that begins
with the email address up to the '#' sign.
import psycopg2
def get_userid(someuser):
try:
connection = psycopg2.connect(bla, bla")
cursor = connection.cursor()
print("someuser for select= ", someuser)
postgreSQL_select_Query = ("""SELECT userid from accounts WHERE email = %s;""" %(someuser))
cursor.execute(postgreSQL_select_Query)
mobile_records = cursor.fetchall()
for row in mobile_records:
print("userid = ", row[0], )
except (Exception, psycopg2.Error) as error :
print ("Error while fetching data from PostgreSQL", error)
finally:
if(connection):
cursor.close()
if __name__ == '__main__':
get_userid()
table has this:
id | email | userid
----+------------------------+--------
18 | user2#gmail.com | u48923
I expect to get the userid of "u48923" after running the select.
Change it to the following
postgreSQL_select_Query = ("""SELECT userid from accounts WHERE email = '%s';""" %(someuser))
But a better way is to use the built in functionality of psycopg2 and write it as follows
cursor.execute("""SELECT userid from accounts WHERE email = %s;""", (someuser,))
Psycopg2 Documentation
def getEmail(someuser): print("received someuser in 'run_select' from views.py= ", someuser) try:
connection = psycopg2.connect(user="my user",
password="xxxxxx",
host="127.0.0.1",
port="5432",
database="my_db")
cursor = connection.cursor()
print("again someuser for select= ", someuser)
someuser = str(someuser) # convert to string or you get: Error while fetching data from PostgreSQL can't adapt type 'User'
print("someuser= ", someuser)
cursor.execute("""SELECT userid from accounts_user WHERE email = %s""", (someuser,))
print("Selecting rows from c9_max_forms_kw using cursor.fetchall")
mobile_records = cursor.fetchall()
print("Print each row and it's columns values")
for row in mobile_records:
print("Id = ", row[0], )
#print("email = ", row[5])
#print("userid = ", row[9], "\n") # should be "u48923"
#save = ''.join(row[9]) # convert this tuple to a string except (Exception, psycopg2.Error) as error :
print ("Error while fetching data from PostgreSQL", error) finally:
#closing database connection.
if(connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed") return someuser
Joshua is the man. The string conversion was the key. The error given without it doesn't seem to make sense to me. Best to all. Ken.

Using python, how to attach a contact in Whatsapp and send to multiple numbers?

I am struggling to click over an element. I am not able to click on the contact element from the attach icon in whatsapp chat. i am using url= "web.whatsapp.com".
Also is there any other option of doing the task?
I get "NoSuchElementException" error when i run the below code. Please help me to select it correctly.
try:
chat_button = driver.find_element_by_xpath("//a[#id = 'action-button']").click()
time.sleep(2)
element_presence(By.XPATH,'//*[#id="main"]/footer/div[1]/div[2]/div/div[2]',30)
msg_box=driver.find_element(By.XPATH , '//*[#id="main"]/footer/div[1]/div[2]/div/div[2]')
msg_box.send_keys(message + Keys.ENTER)
time.sleep(2)
attach_icon = driver.find_element_by_xpath("//div[#title = 'Attach']").click()
# issue starts from here...
contact_icon = driver.find_element_by_xpath("//svg[#id = 'contact-Layer_1']")
contact_icon.click()
time.sleep(2)
search_contact = driver.find_element_by_xpath("//input[#title = 'search']")
search_contact.click()
search_contact.send_keys("pravin tcs")
driver.find_element_by_xpath("//div[#class = '_1kfc8_2uQfJ']").click()
driver.find_element_by_xpath("//span[#data-icon = 'send-light']").click()
except Exception as e:
print("Invalid phone no :"+str(phone_no))

Resources