AttributeError: the "NoneType" object does not have the "GetDataStore" attribute

I guys are developing a utility in python, and I have 2 objects - the main class and the database helper to get sqlserver data.

database.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

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 to run the code, return this error to me

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's wrong?

+3
source share
3 answers

First of all:

  • The method __new__should be named __init__.
  • Delete global line _host, etc.

Then change the method __init__:

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

And change GetDataStore:

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

That should do the trick.

I suggest you read a little object oriented Python.

+3
source

, __new__ . , __init__, __new__. , _db - None.

+1

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

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')")
0
source

Source: https://habr.com/ru/post/1716106/


All Articles