python - 在 Python 中从数据库中删除

标签 python mysql sql mysql-python

我有 deleteuser.py 我需要删除一个用户,这里是代码:

# 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

我没有错误,但它不起作用。 数据库还是一样。

谢谢

最佳答案

这里是答案:

#!/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

关于python - 在 Python 中从数据库中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37036682/

相关文章:

python - 为什么发送电子邮件时没有出现消息? -smtplib-Python

python - Pygame电影每隔一次播放就没有声音

MySQL:以逗号分隔的项目数列表

mysql - 尝试从表中检索信息并编译到前 10 名列表中

mysql - 为什么MYSQL中WHERE语句中的子查询这么慢?

PHP/SQL - mysqli_result 类的对象无法转换为字符串

sql - Hive 将字符串转换为浮点十进制

python - 简化 SymPy 中的条件积分

python - 类型错误 : tan^-1 (tan inverse) of the ratio of two dataframe column

mysql - MySQL查询中动态生成字段的总和