mutegeki
mutegeki

Reputation: 1

python Mysql Delete

I am new to MYSQL and i am facing a very easy problem with MYSQL. I am creating a database that contains a school table , this table contains the sschool's name , contact, address, ID( primary key) . I need to delete a record based on the user's choice of id ( call this variable school_id) , so how to write this in a mysql statement using python ? i have tried this but i know it is wrong -->

print "Delete school"
school_id = int(input('Please, enter an ID: '))  
cursor.execute("DELETE FROM `schools` WHERE id = %s", (school_id,))

its no deleting.whats the error

Upvotes: 0

Views: 439

Answers (5)

Anna
Anna

Reputation: 281

From MySQL documentation on commit:

it is important to call this method after every transaction that modifies data for tables that use transactional storage engines.

You don't have to use it after literally every transaction - it's enough if you call it once after all execute are called:

def a():
    cursor.execute('insert ...')  # cursor object
    cursor.execute('update ...')
    connection.commit()  # connection object

def b():
    cursor.execute('delete ...')
    connection.commit()

Upvotes: 0

mutegeki
mutegeki

Reputation: 1

print "Delete school"
school_id = int(input('Please, enter an ID: '))  
cursor.execute("DELETE FROM `schools` WHERE id = %s", (school_id,))

Upvotes: 0

SudipM
SudipM

Reputation: 426

Unless you commit, it won't be executed:

cursor.commit()

Have fun.

Upvotes: 0

Łukasz Szczesiak
Łukasz Szczesiak

Reputation: 214

As already said by joof most likely you haven't commited the change. If it doesn't help I'd try first to execute this statment in mysql and see if it works, then check if you have right connection to your db.

Link to using sql in Python. https://docs.python.org/3.6/library/sqlite3.html

Upvotes: 0

joof
joof

Reputation: 31

Have you committed the delete?

cursor.commit()

Upvotes: 1

Related Questions