我试图调用不同文件中不同类的函数。
import test_db #test_db: name of different file
obj=test_db.testDb('config.ini') #testDb:Name of class
db_cmd = "Select top 1 * from db.name"
obj.query_exec(db_cmd,'db') #function in class testDb
我得到的错误是
AttributeError: 'module' object has no attribute 'testDb'
test_db文件
class testDB:
def __init__(self,config_file):
self.config_file=config_file
self.parser=SafeConfigParser()
self.parser.read(config_file)
# Create connection
def sql_db_conn(self,tst):
ser_name=self.parser.get(tst,'machine-name')
db_name=self.parser.get(tst,'database')
user=self.parser.get(tst,'username')
passwd=self.parser.get(tst,'password')
con = pypyodbc.connect(driver="{SQL Server}",server=ser_name,database=db_name,uid=user,pwd=passwd)
cur = con.cursor()
return cur
def query_exec(self,query,sample_db):
cur=self.sql_db_conn(sample_db)
res =cur.execute(query)
for r in res:
print (r)
答案 0 :(得分:0)
语句obj=test_db.testDb('config.ini')
中的类名是错误的。
它应该是声明obj=test_db.testDB('config.ini')
答案 1 :(得分:-1)
from testDb import *
a = testDB("config_file_name")
a.query_exec("db_cmd")
# using a object we can access all functions in that class