Python的问题:AttributeError:'NoneType'对象没有属性'execute'

时间:2018-06-08 01:15:26

标签: python

我开始学习Python,但是我遇到了一些问题,而且我不确切知道为什么。我有这两个文件,据我所知,问题是两个文件之间的连接问题。 我已经浏览了一些与之相关的其他问题,但似乎没有一个能解决我的问题。

File1,MysqlDB.py

# -*- coding: utf-8 -*-
import MySQLdb
class DB:
    def __init__(self, DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME, DB_CHARSET):
        self.host = DB_HOST
        self.port = DB_PORT
        self.name = DB_NAME
        self.user = DB_USER
        self.password = DB_PASSWORD
        self.charset = DB_CHARSET

    def get_conn(self):
        try:
            if self.conn is None:
                self.conn = MySQLdb.connect(host = self.host,
                                            port = self.port,
                                            db = self.name,
                                            user = self.user,
                                            passwd = self.password,
                                            charset = self.charset)
                return self.conn
        except:
            print("Can't connect to database")

    def get_cursor(self):
        try:
            self.cursor = self.conn.cursor()
            return self.cursor
        except:
            print("Can't define cursor (cursor == None)")

    def close_conn(self):
        if self.conn:
            self.conn.close()

File2,users.py

# -*- coding: utf-8 -*-
import MysqlDB
from usersVO import UsersVO

class UsersDAO():
    def loginUser(self, tmpEmail, tmpPassword):
        OBJdb = MysqlDB.DB('127.0.0.1',3306,'root','','musica','utf8')
        OBJconn = OBJdb.get_conn()
        OBJcursor = OBJdb.get_cursor()

        queryLogin = "SELECT * FROM users WHERE email_USER LIKE '{}' and password_USER LIKE '{}'".format(tmpEmail, tmpPassword)

        OBJcursor.execute(queryLogin)

Class = UsersDAO()
Class.loginUser('test@email.com','test')

当我尝试在users.py上测试登录操作时,我总是这样:

"AttributeError: 'NoneType' object has no attribute 'execute'"

为什么OBJcursor获得None值?我怎么能解决这个问题?

0 个答案:

没有答案