2011-12-23 6 views
1
query = "delete from mytable where id =10" 
cursor = connection.cursor() 
cursor.execute(query) 

[なし]が返されますが、クエリの応答を知りたいと思います。どうすれば結果を得ることができますか?cursor.execute()の実行中にクエリが正しく実行されたかどうかを知る方法はありますか?

insert文と同じです。誰も私に解決策を与えることができますか?

+1

'cursor.fetchall()'?または 'cursor.fetchone()'? – DhruvPathak

答えて

1

私は、ドキュメントを簡単に見ていた:オブジェクトdjango.db.connectionは、デフォルトのデータベース接続を表し、django.db.transactionはデフォルトを表しhttps://docs.djangoproject.com/en/dev/topics/db/sql/

ジャンゴ/ DBのドキュメントをデータベーストランザクション。データベース接続を使用するには、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() 

私はこのことができます願っています。

関連する問題