我正在尝试使用python在我的django网站和MySql数据库之间建立连接。我正在使用visual studio。我试图使用网站上发布的解决方案,但是工作太多了,有人可以发帖告诉我们如何在两者之间建立联系! 我为连接写的代码是:
import MySQLdb
import cgi
form=cgi.FieldStorage()
# Open database connection
db = MySQLdb.connect("django.db.backends.mysql","127.0.0.1","root","123","project" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Prepare SQL query to INSERT a record into the database.
sql = """INSERT INTO entries(slno, \
dat1, dat2, dat3, dat4, dat5) \
VALUES ('%d', '%d', '%d', '%d', '%d', '%d' )""" % \
(form['Text0'].value, form['Text1'].value, form['Text2'].value, form['Text3'].value, formm['Text4'].value, form['Text5'].value)
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()
# disconnect from server
db.close()