Search code examples
pythonsql-serverdatabasetwistedpymssql

How can I get data from the twisted.enterprise.adbapi, Not the "Deferred" object


When I use the twisted.enterprise.adbapi with sql_server, I always get the "Deferred" object, but I need the available data in a list or tulpe.

import pymssql
from twisted.internet import reactor   
from twisted.enterprise import adbapi
db_settings = {
 "host" : "127.0.0.1",
 "port" : "1433",
 "user" : "sa",
 "password" : "sa",
 "database" : "dataDB",
 "cp_min" : "3",
 "cp_max" : 10,
 "cp_noisy" : "True",
 "charset" : "utf8"
}

dbpool = adbapi.ConnectionPool("pymssql", **db_settings)

def getData():
    return dbpool.runQuery("SELECT * FORM dataDB.Base")

reactor.callLater(4, reactor.stop)
print getData()
reactor.run()

The above code execution,just get "Deferred at 0x4ca2948" in command. What should I do in next?


Solution

  • Use follow code, Can get the data from the "Deferred"

    def getAge():
     return dbpool.runQuery("SELECT * FROM Base")
    
    def getValue(L):
        print L[0][0]  # type(L[0]) is Tuple
    
    getAge().addCallback(getValue)
    reactor.run()
    

    But, I still don't know the meaning of "L" in function getValue. Maybe the "L" is result of getAge(), I am not sure.