def __init__(self, args=None, parent=None, tbl='m12_fpr', id=0):
        super(dlg, self).__init__(parent)
        
        self.id = int(id)
        
        if parent:
            self.db = parent.db
        else:
            self.db = 'c:/ted/mis.sql3'
            
        self.tbl = tbl
        
        self.tname, self.filds = getFields(self.tbl,'../mispar.sql3')
        
        self.fldsNo = len(self.filds)
        
        selSql = "SELECT "
        tailSel= "FROM %s WHERE id='%s'" % (self.tname[1],id)
        
        insSql = "INSERT INTO %s (" % self.tname[1]
        tailSql = " VALUES ("
        
        self.updSql = "UPDATE %s SET " % self.tname[1]
        self.tailUpd = "WHERE id=%s" 
                
        self.labels = []
        self.flds   = []
        self.idvals = {}
        gridLayout = QtGui.QGridLayout()
        today = QtCore.QDate.currentDate()
        
        
        
        idLabel = QtGui.QLabel('id')
        gridLayout.addWidget(idLabel,0,0)
        self.LineEditid = QtGui.QLineEdit()
        self.LineEditid.setEnabled(False)
        idLabel.setBuddy(self.LineEditid)
        gridLayout.addWidget(self.LineEditid,0,1)
        self.LineEditid.setText('%s' % self.id)
        
        for i in range(self.fldsNo):
            self.labels.append(QtGui.QLabel(self.filds[i][1]))
            
            if self.filds[i][2] == 'text':
                self.flds.append(QtGui.QLineEdit())
                
            elif self.filds[i][2] == 'date':
                te = QtGui.QDateEdit()
                te.setCalendarPopup(True)
                te.setDate(today)
                self.flds.append(te)
                
            elif self.filds[i][2] == 'dec':
                self.flds.append(QtGui.QDoubleSpinBox())
                
            elif self.filds[i][2] == 'int':
                sb = QtGui.QSpinBox()
                sb.setMinimum(0)
                sb.setMaximum(2050)
                self.flds.append(sb)            
                
            elif self.filds[i][2] == 'fkey':
                self.idvals[self.filds[i][0]] = 0
                le = ButtonLineEdit() #MyLineEdit() #QtGui.QLineEdit()
                #le.textEdited.connect(functools.partial(self.con,i))
                le.register(self,i)
                le.setReadOnly(True)
                self.flds.append(le)
                
            else:
                self.flds.append(QtGui.QLineEdit())
                
            self.labels[i].setBuddy(self.flds[i])
            gridLayout.addWidget(self.labels[i],i+1,0)
            gridLayout.addWidget(self.flds[i],i+1,1)
            
            if i == (self.fldsNo-1):
                selSql += '%s ' % self.filds[i][0]
                insSql += "%s)" % self.filds[i][0]
                tailSql += "'%s')"
                self.updSql += "%s=" % self.filds[i][0]
                self.updSql += "'%s' "
            else:
                selSql += '%s, ' % self.filds[i][0]
                insSql += "%s, " % self.filds[i][0]
                tailSql += "'%s',"
                self.updSql += "%s=" % self.filds[i][0]
                self.updSql += "'%s', "
                
        self.selectSql = selSql + tailSel
        self.insertSql = insSql + tailSql   
          
        if self.id == 0:
            pass
        else:
            rowVal = dbutils.getDbOneRow(self.selectSql, self.db)
            if rowVal:
                for i in range(self.fldsNo):
                    
                    if self.filds[i][2] == 'date':
                        yr,mn,dt = rowVal[i].split('-')
                        qd = QtCore.QDate()
                        qd.setDate(int(yr),int(mn),int(dt))
                        self.flds[i].setDate(qd)
                        
                    elif self.filds[i][2] == 'dec':
                        self.flds[i].setValue(rowVal[i]) 
 
                    elif self.filds[i][2] == 'fkey':
                        self.idvals[self.filds[i][0]] = str(rowVal[i])
                        
                        if self.filds[i][6]:
                            sqlForField = self.filds[i][6] % rowVal[i]
                            sqlWhere = ''
                        else:
                            sqlForField = self.filds[i][4]
                            
                            if 'WHERE' in sqlForField:
                                sqlWhere = " AND id=%s" % rowVal[i]
                            else:
                                sqlWhere = " WHERE id=%s" % rowVal[i]
                        fkSql = sqlForField + sqlWhere
                        print fkSql
                        v = dbutils.getDbOneRow(fkSql, self.db)
                        self.flds[i].setText(v[1]) 

                    elif self.filds[i][2] == 'int':
                        self.flds[i].setValue(rowVal[i])
  
                    else:
                        self.flds[i].setText('%s' % rowVal[i])                           
               
            
        self.saveButton = QtGui.QPushButton(u'Αποθήκευση')
        self.saveButton.setIcon(QtGui.QIcon(iconSave))
        gridLayout.addWidget(self.saveButton,self.fldsNo+1,0,self.fldsNo+1,-1)
        layout = QtGui.QHBoxLayout()
        layout.addLayout(gridLayout)
        self.setLayout(layout)
        self.makeConnections()
        self.setWindowTitle(self.tname[2])
def getFields(tableName,db):
    sql1 = "SELECT id, tname, tper FROM tbl WHERE tname='%s'" % tableName
    tname = dbutils.getDbOneRow(sql1,db)
    sql2 = "SELECT fname, fper, ftyp, frequired, fsql, ftitles, sqlUpdate FROM tbl_d WHERE tbl_id='%s'" % tname[0]
    ar   = dbutils.getDbRows(sql2, db)
    return tname, ar