query = "delete from mytable where id =10"
cursor = connection.cursor()
cursor.execute(query)
它返回None但我想知道查询的响应。我如何得到结果?
也与insert语句相同。有人能给我解决方案吗?
答案 0 :(得分:1)
我快速浏览了一下文档:
Django / DB docs :https://docs.djangoproject.com/en/dev/topics/db/sql/
对象django.db.connection表示默认数据库连接,django.db.transaction表示默认数据库事务。要使用数据库连接,请调用connection.cursor()以获取游标对象。然后,调用cursor.execute(sql,[params])执行SQL和cursor.fetchone()或cursor.fetchall()以返回结果行。
和Mysql / Python文档:http://www.tutorialspoint.com/python/python_database_access.htm
执行查询后提交:
# Prepare SQL query to DELETE required records
sql = "delete from mytable where id =10"
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()
我希望这会有所帮助。