pip install txpostgres python # config.py HOST = 'your_host_name' PORT = 5432 DATABASE = 'your_database_name' USER = 'your_username' PASSWORD = 'your_password' python # main.py from twisted.internet import task from twisted.internet import reactor from twisted.enterprise import adbapi from txpostgres import txpostgres import config def fetch_data(connection): query = "SELECT * FROM table_name;" return connection.execute(query) def process_results(results): for row in results: print(row) def handle_error(error): print(f"An error occurred: {error}") def main(): connection_pool = adbapi.ConnectionPool( txpostgres.Connector, host=config.HOST, port=config.PORT, database=config.DATABASE, user=config.USER, password=config.PASSWORD ) d = connection_pool.runQuery("SELECT 1;") d.addCallback(fetch_data) d.addCallback(process_results) d.addErrback(handle_error) reactor.run() if __name__ == '__main__': main() python main.py


上一篇:
下一篇:
切换中文