默认数据库已经安装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!‘)
时间: 2024-10-06 09:18:32