1
query = "delete from mytable where id =10"
cursor = connection.cursor()
cursor.execute(query)
它返回None,但我想知道查询的响应。我如何得到结果?执行cursor.execute()时,如何知道查询是否正确执行?
同样也是插入语句。任何人都可以给我解决方案吗?
query = "delete from mytable where id =10"
cursor = connection.cursor()
cursor.execute(query)
它返回None,但我想知道查询的响应。我如何得到结果?执行cursor.execute()时,如何知道查询是否正确执行?
同样也是插入语句。任何人都可以给我解决方案吗?
我有一个快速浏览一下文档:
的Django/DB文档: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()
我希望这有助于。
'cursor.fetchall()'?或'cursor.fetchone()'? – DhruvPathak 2011-12-23 06:42:09