Fair warning: I’m a big time noob. Please handle with kid gloves.
Details:
- Python 3.2
- MySQL 5.5
- Tornado webframe installed
- pymysql installed
- Windows 7
Problem:
I’m following the Tornado documentation on connecting to a mysql database here. I only want to connect to localhost, but I’m getting the following error message:
Traceback (most recent call last):
File "C:\Python32\DIP3\tornado-test.py", line 5, in <module>
class Connection(localhost,re_project, user=root, password=mypassword, max_idle_time=25200):
NameError: name 'localhost' is not defined
This is the code I’m trying to run:
import tornado.ioloop
import tornado.web
import pymysql
class Connection(localhost,re_project, user=root, password=mypassword, max_idle_time=25200):
db = database.Connection("localhost", "re_project")
for Bogota in db.query("SELECT * FROM cities_copy"):
print(Bogota.title)
MySQL is currently running when I execute the code, so I don’t think that should be a problem. What else could I be doing wrong?
Okay, I think I understand the problem. In the documentation, the line
class tornado.database.Connection(host, database, user=None, password=None, max_idle_time=25200)is part of the documentation and is not meant to be copy/pasted. That describes how to do thedb = database.Connectionbit.The green code sample lines should work on their own, as long as 1) the tornado.database module is imported and 2) the
db =line is adjusted to pass values appropriate for your database to theConnectionmethod.So:
I haven’t tested this because I do not have Python 3.2 installed, so let me know if it doesn’t work and I’ll try to adjust.