I guys, I developing a utility in python and i have 2 object the main class and an database helper for get sqlserver data.
database.py
JavaScript
x
18
18
1
import _mssql
2
3
class sqlserver(object):
4
5
global _host, _userid, _pwd, _db
6
7
def __new__ (self, host, userid, pwd, database):
8
_host = host
9
_userid = userid
10
_pwd = pwd
11
_db = database
12
13
def GetDataStore(self, sql):
14
conn = _mssql.connect(server='(local)\sqlexpress', user='sa', password='xxx', database='Framework.Data2')
15
conn.execute_non_query('CREATE TABLE persons(id INT, name VARCHAR(100))')
16
conn.execute_non_query("INSERT INTO persons VALUES(1, 'John Doe')")
17
conn.execute_non_query("INSERT INTO persons VALUES(2, 'Jane Doe')")
18
gaemodel.py
JavaScript
1
31
31
1
import os
2
import sys
3
from fwk import system, types, databases
4
5
class helper(object):
6
pass
7
8
def usage(app_name):
9
return "Usage: %s <project name>" % (app_name)
10
11
def main(argv):
12
_io = system.io()
13
14
project_name = argv[1]
15
project_home = os.path.join(_io.CurrentDir(), project_name)
16
17
_db = databases.sqlserver('(local)sqlexpress', 'sa', 'xxx', 'Framework.Data2')
18
_db.GetDataStore("select name from sysobjects where xtype = 'U' and name not like 'Meta%'")
19
20
str = "from google.appengine.ext import db"
21
#for row in cur:
22
# str += "class %s" % row["name"]
23
24
print cur
25
26
if __name__ == "__main__":
27
if len(sys.argv) > 1:
28
main(sys.argv[1:])
29
else:
30
print usage(sys.argv[0]);
31
My problem is when i try run code return me this error
JavaScript
1
7
1
Traceback (most recent call last):
2
File "C:ProjectosFrameworkGAEsrcgaemodel.py", line 28, in <module>
3
main(sys.argv[1:])
4
File "C:ProjectosFrameworkGAEsrcgaemodel.py", line 18, in main
5
_ db. GetDataStore("select name from sysobjects where xtype = 'U' and name not like 'Meta%'")
6
AttributeError: 'NoneType' object has no attribute 'GetDataStore'
7
What is wrong ??
Advertisement
Answer
First of all:
- The
__new__
method should be named__init__
. - Remove the global _host etc. line
Then change the __init__
method:
JavaScript
1
4
1
self.host = host
2
self.userid = userid
3
etc.
4
And change GetDataStore
:
JavaScript
1
2
1
conn = _mssql.connect(server=self.host, user=self.userid, etc.)
2
That should do the trick.
I suggest you read a bit on object-oriented Python.