Python取出SQL表单中的字段名

def ReturnInfo(self, avalue, akey):
        cursor = connection.cursor()
        Sql = "select * from %s where %s=%s" % (self.table, akey, avalue)
        cursor.execute(Sql)
        SqlDomain = cursor.description  # 下文说明cursor.description的作用
        DomainNum = len(SqlDomain)
        SqlDomainName = [None]*DomainNum
        for i in range(DomainNum):
            SqlDomainName[i] = SqlDomain[i][0]
        cursor.close()

     return SqlDomainName

cursor.description方法会将每个字段的字段名,字段类型,字段长度...等等字段的属性列出来.

SqlDomain是个二元数组. SqlDomain[0]数组描述第一个字段的属性,即SqlDomain[0][0]是第一个字段的名字,SqlDomain[0][1]是第一个字段的数据类型,SqlDomain[0][2]是第一个字段的.....

那么SqlDomain[1]数组描述第二个字段的属性.

以此类推SqlDomain[2]数组描述第三个字段的属性.

故取字段名,只要取SqlDomain[0][0], SqlDomain[1][0], SqlDomain[2][0], ...即可。

原文地址:https://www.cnblogs.com/haoshine/p/5258641.html