2011-12-23 248 views

回答

1

我有一个快速浏览一下文档:

的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() 

我希望这有助于。