如何从Class外的RecordSet返回值?

时间:2014-08-29 05:07:42

标签: python python-2.7 ms-access qt4 recordset

我正在尝试使用RecordSet打印一个值。

如果我从方法内部的RecordSet中打印值,它就可以正常工作。 但是如果我从该方法返回值,那么它返回类似于:

的内容
<win32com.gen_py.Microsoft ActiveX Data Objects 6.1 Library.Field instance at 0x38897624>

请建议如何正确返回值。 在显示表单之前,我想要全局变量'data'的值,因为我需要将其值赋给self.RollNo(QLineEdit)。 代码如下:

import sys
import os
from win32com.client import Dispatch
from adoconstants import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Form(QDialog):
    def __init__(self,parent=None):
        super(Form,self).__init__(parent)

        self.RollNo = QLineEdit()
    ....
    ....

    def launch(self):
        oConn = Dispatch('ADODB.Connection')
        oConn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\Users\DELL\Student DB.accdb;Uid=Admin;Pwd=;"
        oConn.Open()

        if oConn.State == adStateOpen:
            oRS = Dispatch('ADODB.RecordSet')
            oRS.ActiveConnection = oConn
            oRS.Open("Select LAST(Enrollment_Number) from Student_Info")
            global data
            data = oRS.Fields.Item(0) # number 53 is fetched from the last record of Enrollment_Number field
            print data
            return data
            oRS.Close()
            oRS = None
        else:
            print "Connection Failed"
        if oConn.State == adStateOpen:
            oConn.Close()
        oConn = None

app=QApplication(sys.argv)
form=Form()
print form.launch()
app.exec_()

结果:

53
<win32com.gen_py.Microsoft ActiveX Data Objects 6.1 Library.Field instance at 0x38897624>

1 个答案:

答案 0 :(得分:1)

您确定无法在某处更改代码中的数据值吗? 尝试返回int(数据)以查看此结果是否仍然存在。像这样:

...
 if oConn.State == adStateOpen:
            oRS = Dispatch('ADODB.RecordSet')
            oRS.ActiveConnection = oConn
            oRS.Open("Select LAST(Enrollment_Number) from Student_Info")
            global data
            data = oRS.Fields.Item(0) # number 53 is fetched from the last record of Enrollment_Number field
            print data
            return int(data)
            oRS.Close()
            oRS = None
...

告诉我它是否有效。