Added title for tables

master
Sergey Kalinin 2017-03-14 17:01:30 +03:00
parent 5c26c1b898
commit dc40c35424
3 changed files with 58 additions and 44 deletions

32
dm.py
View File

@ -2,20 +2,26 @@
import json
def createTables(tbl_list):
global tbl_descr_list
i = 0
tbl_names_list = []
tbl_descr_list = []
while i < len(tbl_list):
one_Table_descr = []
tbl_descr = tbl_list[i]["tableDescription"]
tbl_name = tbl_list[i]["tableName"]
field_list = tbl_list[i]["fieldList"][i].keys()
tbl_names_list.append(tbl_name)
tbl_names_list.append([tbl_name, tbl_descr])
one_Table_descr.append(tbl_name)
x = 0
# список всех полей таблицы
qwery_create = "CREATE TABLE " + tbl_name + " ("
qwery_select = "SELECT * FROM "
#qwery_select = "SELECT * FROM "
index = ""
field_names_list = []
while x < len(tbl_list[i]["fieldList"]):
if tbl_list[i]["fieldList"][x]["autoIncrement"] == "yes":
auto_increment = " AUTO_INCREMENT, "
@ -23,21 +29,26 @@ def createTables(tbl_list):
auto_increment = ", "
qwery_create = qwery_create + tbl_list[i]["fieldList"][x]["fName"] + " " + \
tbl_list[i]["fieldList"][x]["fType"] + auto_increment
if tbl_list[i]["fieldList"][x]["index"] == "PRIMARY KEY":
index = "PRIMARY KEY(" + tbl_list[i]["fieldList"][x]["fName"] + ")"
field_names_list.append(tbl_list[i]["fieldList"][x]["fDescription"])
x = x + 1
qwery_create = qwery_create + index + ");"
print(qwery_create)
one_Table_descr.append(field_names_list)
#print(qwery_create)
i = i + 1
tbl_descr_list.append(one_Table_descr)
#print(tbl_descr_list)
return tbl_names_list
def initDBstructure():
global tbl_descr_list
table_list = open("tables.json", "r", encoding="utf-8")
data = json.load(table_list, encoding="utf-8")
tbl_list = data["tables"]
tbl_names_list = createTables(tbl_list)
return tbl_names_list
def selectData(table_name):
@ -45,4 +56,13 @@ def selectData(table_name):
data_list = ("1","2","3","4","5")
return table_name
initDBstructure()
# получаем на вход имя таблицы и возвращаем список заголовков полей
def getTablesStructure(tbl):
global tbl_descr_list
for item in tbl_descr_list:
if item[0] == tbl:
return item[1]
#print(tbl_descr_list)
#return tbl_descr_list
#initDBstructure()

32
gui.py
View File

@ -111,19 +111,26 @@ class MainWin(QMainWindow):
self.show()
class MyTable(QTableWidget):
def __init__(self, thestruct, *args):
def __init__(self, tbl, thestruct, *args):
QTableWidget.__init__(self, *args)
self.data = thestruct
self.setmydata()
self.tbl = tbl
#print(self.tbl)
n = 0
for item in dm.getTablesStructure(self.tbl):
print(item)
# установка заголовков столбцов таблицы
#character = chr(ord('A') + n)
self.setHorizontalHeaderItem(n, QTableWidgetItem(item))
n = n+1
def setmydata(self):
n = 0
for key in self.data:
#print(key)
m = 0
for item in key:
#print(item)
newitem = QTableWidgetItem(item)
self.setItem(m, n, newitem)
n += 1
@ -134,12 +141,9 @@ class WorkArea(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
# список таблиц
listTablesWidget = QListWidget()
@ -161,7 +165,7 @@ class WorkArea(QWidget):
# выводим список таблиц в левом поле
for i in dm.initDBstructure():
listTablesWidget.addItem(i)
listTablesWidget.addItem(i[0])
# заполняем правый список данными из выбранной таблицы
def addData(tbl_name):
@ -172,10 +176,6 @@ class WorkArea(QWidget):
if item is not None:
#print(item)
item.deleteLater()
#tablesDataWidget.deleteLater()
# rows = message.count('id') # считает количество строк в таблице
# print(c.fetchall())
data = c.fetchall()
# проверка на наличие записей в таблице
if data:
@ -183,24 +183,18 @@ class WorkArea(QWidget):
rows = len(data)
# КОЛИЧЕСТВО КОЛОНОК
cols = len(data[0])
table = MyTable(data, rows, cols)
table = MyTable(tbl_name, data, rows, cols)
table.setFrameShape(QFrame.StyledPanel)
splitter1.addWidget(table)
#table.show()
#sys.exit(app.exec_())
# обработка нажатия мышой на списке
#listTablesWidget.itemClicked.connect(lambda: dm.selectData(listTablesWidget.currentItem))
listTablesWidget.itemClicked.connect(lambda: addData(listTablesWidget.model().data(listTablesWidget.currentIndex())))
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
if __name__ == '__main__':
app = QApplication(sys.argv)

View File

@ -2,46 +2,46 @@
"tables": [
{
"tableName": "users",
"tableDescription": "Список пользователей",
"tableDescription": "Пользователи",
"fieldList": [
{
"fName": "id",
"fDescr": "Номер п.п.",
"fDescription": "Номер п.п.",
"fType": "int(6)",
"index": "PRIMARY KEY",
"autoIncrement": "yes"
},
{
"fName": "login",
"fDescr": "Логин",
"fDescription": "Логин",
"fType": "char(20)",
"index": "yes",
"autoIncrement": "no"
},
{
"fName": "password",
"fDescr": "Пароль",
"fDescription": "Пароль",
"fType": "char(20)",
"index": "no",
"autoIncrement": "no"
},
{
"fName": "last_name",
"fDescr": "Фамилия",
"fDescription": "Фамилия",
"fType": "char(20)",
"index": "no",
"autoIncrement": "no"
},
{
"fName": "name",
"fDescr": "Имя",
"fDescription": "Имя",
"fType": "char(20)",
"index": "no",
"autoIncrement": "no"
},
{
"fName": "middle_name",
"fDescr": "Отчество",
"fDescription": "Отчество",
"fType": "char(20)",
"index": "no",
"autoIncrement": "no"
@ -50,39 +50,39 @@
},
{
"tableName": "docs",
"tableDescription": "Список документов",
"tableDescription": "Документы",
"fieldList": [
{
"fName": "id",
"fDescr": "Номер п.п.",
"fDescription": "Номер п.п.",
"fType": "int(6)",
"index": "PRIMARY KEY",
"autoIncrement": "yes"
},
{
"fName": "doc_name",
"fDescr": "Наименование",
"fDescription": "Наименование",
"fType": "varchar(100)",
"index": "yes",
"autoIncrement": "no"
},
{
"fName": "description",
"fDescr": "Описание",
"fDescription": "Описание",
"fType": "varchar(200)",
"index": "no",
"autoIncrement": "no"
},
{
"fName": "user_id",
"fDescr": "Владелец",
"fDescription": "Владелец",
"fType": "int(6)",
"index": "no",
"autoIncrement": "no"
},
{
"fName": "parent_id",
"fDescr": "Имя",
"fDescription": "Имя",
"fType": "int(6)",
"index": "no",
"autoIncrement": "no"
@ -91,39 +91,39 @@
},
{
"tableName": "address",
"tableDescription": "Список адресов",
"tableDescription": "Адреса",
"fieldList": [
{
"fName": "id",
"fDescr": "Номер п.п.",
"fDescription": "Номер п.п.",
"fType": "int(6)",
"index": "PRIMARY KEY",
"autoIncrement": "yes"
},
{
"fName": "region",
"fDescr": "Регион",
"fDescription": "Регион",
"fType": "varchar(100)",
"index": "yes",
"autoIncrement": "no"
},
{
"fName": "city",
"fDescr": "Населеннй пункт",
"fDescription": "Населеннй пункт",
"fType": "varchar(200)",
"index": "no",
"autoIncrement": "no"
},
{
"fName": "street",
"fDescr": "Улица",
"fDescription": "Улица",
"fType": "varchar(100)",
"index": "no",
"autoIncrement": "no"
},
{
"fName": "house",
"fDescr": "Дом",
"fDescription": "Дом",
"fType": "char(6)",
"index": "no",
"autoIncrement": "no"