还好,网上弄到的,,没有dual的数据库,可以试图用select函数不带from数据表的方式来实现返回值。
一段测试代码:
try:conn = psycopg2.connect(database=db.service_name, user=db.username, password=password, host=db.ip, port=db.port)cursor = conn.cursor()except Exception, e:context_dict = {'msg': e, 'address': db.ip, 'port': db.port}return render(request, 'sar/release_info.html', context_dict)sql = "SELECT 'OK' AS txt"try:cursor.execute(sql)x = cursor.fetchone()[0]except Exception,e:context_dict = {'msg': "密码认证通过,但SELECT功能出错", 'address': db.ip, 'port': db.port}return render(request, 'sar/release_info.html', context_dict)finally:cursor.close()conn.close()if x == 'OK':context_dict = {'msg': "数据库连接正常", 'sql': sql, 'address': db.ip, 'port': db.port }return render(request, 'sar/release_info.html', context_dict)