I would to remove all customers who's names begin with SCH from my database. When I execute the code below it runs without error, but does not remove the data from the database.
cur = db.cursor()
sql = "DELETE FROM customers where IMAGE_ID like 'SCH%'"
cur.execute(sql)
after delete you need commit
conn = cx_Oracle.connect(...)
cur = db.cursor()
sql = "DELETE FROM customers where IMAGE_ID like 'SCH%'"
cur.execute(sql)
conn.commit()
cur.close()
conn.close()
Related
I am trying to execute some Postgres queries with psycopg2.
import psycopg2
conn = psycopg2.connect(
host="test.postgres.database.azure.com",
database="postgres",
user="test",
password="test")
cur = conn.cursor()
sql = "select site_panel from test.ws_sites"
cur.execute(sql)
rows = cur.fetchall()
The query above works well and returns the data but the following query does not delete the table as intended.
cur.execute ("DROP TABLE IF EXISTS test.ws_sites")
Anything wrong I'm doing here?
A query that modifies table data or schema structure needs to be committed:
cur.execute ("DROP TABLE IF EXISTS test.ws_sites")
conn.commit()
Alternatively, place
conn.autocommit = True
after creating the connection.
Python code part that contains query:
conn = pymysql.connect(host='127.0.0.1', user=sql_username,
passwd=sql_password, db=sql_main_database,
port=tunnel.local_bind_port)
query = '''select * from users;'''
df_user = pd.read_sql_query(query, conn)
I need to to modify query to exclude the soft deleted records. How modified query should look like?
I am trying to execute multiple query in a for-loop. So in each loop, if either query failed to execute, all queries in that loop will not commit to database and return the error message for failed query. Below is my code. Seems even there is one query failed to run, the others queries will still execute and commit the data to database. How can I change it to achieve what I want? Thanks
Code:
import mysql.connector
mydb = mysql.connector.connect()
cursor = mydb.cursor()
for i in country_list:
try:
query1 = "call_sp_insert_sth_1" //insert data to DB
query2 = "call_sp_insert_sth_2" //insert data to DB
query3 = "call_sp_insert_sth_3" //update data to DB
cursor.execute(query1)
cursor.execute(query2)
cursor.execute(query3)
mydb.commit()
except Exceptiion as err:
fail_list.append('error':err.msg)
continue
mysql.connector.close_connection(mydb)
This is how I would write your code.
import mysql.connector
mydb = mysql.connector.connect()
cursor = mydb.cursor()
query1 = "call_sp_insert_sth_1" //insert data to DB
query2 = "call_sp_insert_sth_2" //insert data to DB
query3 = "call_sp_insert_sth_3" //update data to DB
for i in country_list:
try:
with mydb.cursor() as curs:
cursor.execute(query1)
cursor.execute(query2)
cursor.execute(query3)
curs.close()
except Exception as err:
fail_list.append('error':err.msg)
else:
mydb.commit()
I tend to have my queries as variables outside the for loop with parameters, then populate the parameters in the loop.
myquery = '''select * from {}'''
for table in tables:
curs.execute(myquery.format(table))
The 'else' part only kicks off if there were no errors in the 'try' part.
You need to set the mydb connection to not auto-commit.
mydb.autocommit = False
I am inserting a couple thousand records into a table via the python code below:
values = ''
for row in cursor:
values = values + "(" + self.quoted_comma_separate(row) + "),"
values = values[:-1]
insert_statement = "INSERT INTO t1 ({0}) VALUES {1};".format(
self.comma_separate(members), values)
db = Database()
conn = db.get_db()
cursor = conn.cursor()
cursor.execute(insert_statement)
conn.commit()
conn.close()
When I check the database after it runs none of the records show up in the database. If I go into an MySQL editor and manually commit the transaction all of the records appear. Why is my conn.commit() not working?
The insert statements were fine. Turns out I had another database connection open and it was getting confused and committing to the wrong connection or something like that. Sorry for the pointless question :)
i am trying to send two SQL ( select and update) in one python file. but getting still error
cursor = cnx.cursor()
query = "select id, mail from `candidats`; UPDATE candidats SET statusmail=1 "
results = cursor.execute(query, multi=True)
for cur in results:
print('cursor:', cur)
print('result:', cur.fetchall())
cursor.close()
cnx.close()
getting this error:
mysql.connector.errors.InterfaceError: No result set to fetch from