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