python AttributeError: 'NoneType' 对象没有属性 'GetDataStore'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1334607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:58:46  来源:igfitidea点击:

AttributeError: 'NoneType' object has no attribute 'GetDataStore'

python

提问by pedrofernandes

I guys, I developing a utility in python and i have 2 object the main class and an database helper for get sqlserver data.

伙计们,我在 python 中开发了一个实用程序,我有 2 个对象主类和一个用于获取 sqlserver 数据的数据库助手。

database.py

数据库.py

import _mssql

class sqlserver(object):

    global _host, _userid, _pwd, _db

    def __new__ (self, host, userid, pwd, database):
        _host = host
        _userid = userid
        _pwd = pwd
        _db = database

    def GetDataStore(self, sql):
        conn = _mssql.connect(server='(local)\sqlexpress', user='sa', password='xxx', database='Framework.Data2')
        conn.execute_non_query('CREATE TABLE persons(id INT, name VARCHAR(100))')
        conn.execute_non_query("INSERT INTO persons VALUES(1, 'John Doe')")
        conn.execute_non_query("INSERT INTO persons VALUES(2, 'Jane Doe')") 

gaemodel.py

gaemodel.py

import os
import sys
from fwk import system, types, databases

class helper(object):
    pass

def usage(app_name):
    return "Usage: %s <project name>" % (app_name)

def main(argv):
    _io = system.io()

    project_name = argv[1]
    project_home = os.path.join(_io.CurrentDir(), project_name)

    _db = databases.sqlserver('(local)\sqlexpress', 'sa', 'P1lim07181702', 'Framework.Data2')    
    _db.GetDataStore("select name from sysobjects where xtype = 'U' and name not like 'Meta%'")

    str = "from google.appengine.ext import db"
    #for row in cur:
    #    str += "class %s" % row["name"]

    print cur

if __name__ == "__main__":
    if len(sys.argv) > 1:
        main(sys.argv[1:])
    else:
        print usage(sys.argv[0]);

My problem is when i try run code return me this error

我的问题是当我尝试运行代码时返回此错误

Traceback (most recent call last):
  File "C:\Projectos\FrameworkGAE\src\gaemodel.py", line 28, in <module>
    main(sys.argv[1:])
  File "C:\Projectos\FrameworkGAE\src\gaemodel.py", line 18, in main
_    db. GetDataStore("select name from sysobjects where xtype = 'U' and name not like 'Meta%'")
AttributeError: 'NoneType' object has no attribute 'GetDataStore'

What is wrong ??

怎么了 ??

采纳答案by codeape

First of all:

首先:

  • The __new__method should be named __init__.
  • Remove the global _host etc. line
  • __new__方法应命名为__init__
  • 删除全局 _host 等行

Then change the __init__method:

然后改变__init__方法:

self.host = host
self.userid = userid
etc.

And change GetDataStore:

并改变GetDataStore

conn = _mssql.connect(server=self.host, user=self.userid, etc.)

That should do the trick.

这应该够了吧。

I suggest you read a bit on object-oriented Python.

我建议你阅读一些关于面向对象的 Python。

回答by SilentGhost

Have a look at what __new__is supposed to be doing and what arguments it's supposed to have. I think you wanted to use __init__and not __new__. The reason for your particular error is that _dbis None.

看看应该做什么__new__以及它应该有什么论点。我想你想使用__init__而不是__new__. 您的特定错误的原因_dbNone.

回答by Ned Batchelder

I think you want to change databases.py like this:

我认为您想像这样更改 databases.py:

import _mssql

class sqlserver(object):

    def __init__ (self, host, userid, pwd, database):
        self.host = host
        self.userid = userid
        self.pwd = pwd
        self.db = database

    def GetDataStore(self, sql):
        conn = _mssql.connect(server=self.host, user=self.userid, password=self.pwd, database=self.db)
        conn.execute_non_query('CREATE TABLE persons(id INT, name VARCHAR(100))')
        conn.execute_non_query("INSERT INTO persons VALUES(1, 'John Doe')")
        conn.execute_non_query("INSERT INTO persons VALUES(2, 'Jane Doe')")