pycharm 控制台报错内容如下:
pgsqlSearch.py 连接pgsql代码如下:
import psycopg2
import ReportModel
if __name__ == '__main__':
# 创建连接对象
conn = psycopg2.connect(database="checkdb", user="postgres", password="postgres", host="11.13.71.71",
port="5432")
# 创建指针对象
cur = conn.cursor()
# 获取结果
sql = "SELECT * FROM ics_check_report where batch_id = '%s'" % '149457'
cur.execute(sql)
results = cur.fetchall()
thisset = set()
for row in results:
report = ReportModel(row[1], row[3], row[4], row[5], row[6], row[9], row[10], row[11], row[14])
thisset.add(report);
# 关闭连接
cur.close()
conn.close()
for data in thisset:
print(''.join(['{0}: {1} '.format(item[0], item[1]) for item in data.__dict__.items()]))
print(len(thisset))
ReportModel.py模块中的 ReportModel类:
class ReportModel:
def __init__(self, check_name, data_version, layer_name, feature_id, chk_id, error_desc, feature_wkt, geom,
location):
self.check_name = check_name
self.data_version = data_version
self.layer_name = layer_name
self.feature_id = feature_id
self.chk_id = chk_id
self.error_desc = error_desc
self.feature_wkt = feature_wkt
self.geom = geom
self.location = location
运行pgsqlSearch.py 模块,直接报错:TypeError: 'module' object is not callable
根本原因:
Python导入模块的方法有两种:import module 和 from module import
区别:import module所有导入的东西使用时需加上模块名的限定
from module import 则不需要模块名限定
——————————————————————————————————————————
修改pgsqlSearch.py代码中的调用方式:
report = ReportModel.ReportModel(row[1], row[3], row[4], row[5], row[6], row[9], row[10], row[11], row[14])
运行代码pass: