Gina
Gina

Reputation: 103

Delete from database in Python

I have deleteuser.py where i need to delete one user, here is the code:

# Import modules for CGI handling
import MySQLdb
import cgi, cgitb

# Open database connection
db = MySQLdb.connect("localhost", "root", "", "moviedb" )

# prepare a cursor object using cursor() method
cursor = db.cursor()

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
iduser = form.getvalue('iddelete')

# execute SQL query using execute() method.
try:
    cursor.execute("""DELETE FROM user WHERE
         ID = '%s'""",(iduser))
    # Commit your changes in the database
    db.commit()
except:
    db.rollback()

# disconnect from server
db.close()

print "Content-Type: text/plain;charset=utf-8"
print

I have no error but it doesn`t work. The database is still the same.

Thank you

Upvotes: 2

Views: 5471

Answers (1)

Gina
Gina

Reputation: 103

here is the answer:

#!/Python27/python
# -*- coding: UTF-8 -*-


# Import modules for CGI handling
import MySQLdb
import cgi, cgitb

# Open database connection
db = MySQLdb.connect("localhost", "root", "", "moviedb" )

# prepare a cursor object using cursor() method
cursor = db.cursor()

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
iduser = form.getvalue('iddelete')

# execute SQL query using execute() method.
query = "delete from user where id = '%s' " % iduser
cursor.execute(query)
    # Commit your changes in the database
db.commit()

# disconnect from server
db.close()

print "Content-Type: text/plain;charset=utf-8"
print

Upvotes: 1

Related Questions