我在尝试使用 python 连接到数据库时遇到问题。它编译没有错误,但似乎没有做任何事情。我不确定我是否错误地实例化了该类或问题可能是什么。有人能指出我正确的方向吗?
import _mysql
import MySQLdb
class Operations:
def open():
db=_mysql.connect("127.0.0.1","root","admin","test")
c=db.cursor()
#deletes the cursor
def close(self):
c.close()
#verifies the credentials and advances
def login(self):
print "Welcome to the online bookstore login!"
x = raw_input('Please enter your user id. ')
y = raw_input('Please enter your password. ')
c.execute("""SELECT userid,password FROM members WHERE userid = %s""", (x,))
z = c.password
if y == z:
member_menu()
else:
close()
def new_user(self):
print "Welcome to the Online book store new user registration page!"
print "To begin, please enter your first name: "
fName = raw_input('Please enter your first name: ')
lName = raw_input('Please enter your last name: ')
address = raw_input('Please enter your street address: ')
city = raw_input('Please enter your city: ')
state = raw_input('Please enter your state: ')
zip_code = raw_input('Please enter your zip code: ')
phone = raw_input('Please enter your phone number: ')
email = raw_input('Please enter your email: ')
user_ID = raw_input('Please enter your user id: ')
password = raw_input('Please enter your password: ')
c.executemany("""INSERT INTO members(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,) VALUES (fName, lName, address, city, state, zip_code, phone, email, user_id, password,)""")
print "Your account has been created. returning to the welcome menu. "
welcome()
def welcome(self):
choice = NONE;
print "**********************************************************************\n"
print "***\t\t\t\t\t\t\t\t ***\n"
print "***\t\tWelcome to the Online Book Store\t\t ***\n"
print "***\t\t\t\t\t\t\t\t ***\n"
print "**********************************************************************\n"
print "1. Member Login\n"
print "2. New Member Registration\n"
print "3. Quit\n"
choice = raw_input('Type in your option: ')
if choice == 1:
login()
elif x == 2:
new_user()
else:
close()
def member_menu(self):
x = NONE
print "**********************************************************************\n"
print "***\t\t\t\t\t\t\t\t ***\n"
print "***\t\t Welcome to the Online Book Store \t\t ***\n"
print "***\t\t\t Member Menu \t\t\t ***\n"
print "***\t\t\t\t\t\t\t\t ***\n"
print "**********************************************************************\n"
print "1. Search by Author/Title/Subject\n"
print "2. View/Edit Shopping Cart\n"
print "3. Check Order Status\n"
print "4. One Click Check Out\n"
print "5. Logout\n"
print "Type in your option: "
x = raw_input('Please enter your choice. ')
if x == 1:
close_conn(),
elif x == 2:
close_conn(),
elif x == 3:
close_conn(),
elif x == 4:
close_conn(),
else:
close_conn()
def main():
start = Operations()
print "Opening conenction to database"
start.welcome
if __name__ == '__main__':
main()
最佳答案
嗯,你的代码有很多问题,我可能会错过其中的一些问题。
什么也没有发生,因为你的 main() 函数和条件都是类定义的一部分,所以解释器看到的实际上只是两个导入和一个类定义。
假设我们取消了 main() 定义和条件的缩进。然后会发生的就是创建一个 Operations 实例(没有特殊效果,因为您没有定义自定义构造函数)并将“打开与数据库的连接”打印到屏幕上,因为 main() 中的最后一行所做的就是获取引用welcome()方法并忽略它。您需要调用它:start.welcome()
当你调用它时,会出现更多问题。名称错误可能会首先出现,因为您使用的标识符在给定范围内不存在。您似乎对 Python 的对象模型不熟悉,并且可能来自采用不同方法的语言,例如 C++。在Python中,所有非静态和非类实例方法都将对它们正在操作的对象的引用作为第一个参数,传统上称为“self”。如果要访问对象的任何字段,则需要通过“self”来执行此操作,否则解释器将看不到它们。例如:您打开一个连接并将光标保留为 c,稍后您可以在其他方法中重用它:
def open(): # ... c=db.cursor() # ... def login(self): # ... c.execute("...")
这是不正确的,原因有两个:
- 您的 open() 方法不将 self 作为参数
- 您正在将 c 创建为 open() 方法范围内的局部变量,然后尝试在 login() 中访问它,这本质上会导致“赋值前引用”错误。
为了正确,应该这样写:
def open(self): # ... self.c = db.cursor() # ... def login(self): # ... self.c.execute("...")
你在很多地方都犯了同样的错误。需要调用self.login()、self.new_user()、self.close()等。
您正在使用 Python 2,至少根据问题的标签来看,并且在 Python 2 中声明类时您需要记住一件事。存在所谓的旧式类和新式类,以及您需要做什么。想要做的就是使用新式的。因此你的类必须继承自对象:
class Operations(object): # ...
他们最终决定在 Python 3 中放弃对旧式类的支持,并且不再需要显式地从对象继承,但在 Python 2 中,您需要应对它。
虽然仍然存在一些错误或潜在错误(什么是 close_connection()?),但我认为这足以作为一个良好的开始;)。祝你好运。
关于MySql 数据库与 python 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13675753/