$ pip install twisted $ pip install psycopg2 $ pip install txpostgres python from twisted.internet import defer from twisted.python import log from txpostgres import txpostgres dsn = "dbname='your_database' user='your_username' password='your_password' host='your_host' port='your_port'" @defer.inlineCallbacks def connect(): connection = yield txpostgres.Connection(dsn=dsn) result = yield connection.runQuery('SELECT * FROM your_table') connection.close() if __name__ == '__main__': log.startLogging(open('txpostgres.log', 'w')) connect().addBoth(lambda _: reactor.stop()) reactor.run() python result = yield connection.runQuery('SELECT column1, column2 FROM your_table') python for row in result: print(row) python connection.close()


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