标签:
默认数据库已经安装OK, 并且MySQLdb(Python连接MySQL的模块)已经安装ok
def accessDb(request):
import MySQLdb
db = MySQLdb.connect(db=‘jetty‘, user=‘root‘, passwd=‘mysql‘, host=‘localhost‘, charset=‘utf8‘)
cursor = db.cursor();
cursor.execute(‘SELECT name FROM book ORDER BY name‘)
names = [row[0] for row in cursor.fetchall()]
for name in names:
print ‘bookName:‘, name
return HttpResponse(‘access DB successful!‘)
标签:
原文地址:http://www.cnblogs.com/506740640zl/p/5926001.html