|
| 1 | +import pymysql |
| 2 | + |
| 3 | +class DB: |
| 4 | + _conn = pymysql.connect(host='localhost', user='root', passwd=None, db='mysql') |
| 5 | + _nameDataBase = "" |
| 6 | + _nameTable = "" |
| 7 | + |
| 8 | + def __init__(self, whereGetInfo): |
| 9 | + cur = self._conn.cursor() |
| 10 | + if (whereGetInfo == "new"): |
| 11 | + columnsNames = [] |
| 12 | + name = input("Write name DB: ") |
| 13 | + nameTable = input("Write name table: ") |
| 14 | + countColumns = int(input("How many columns? " )) |
| 15 | + for row in range(countColumns): |
| 16 | + columnsNames.append(input("Name column and type #"+str(row)+" : ")) |
| 17 | + valTypesStr = ",".join(columnsNames) |
| 18 | + cur.execute("CREATE DATABASE "+name+";") |
| 19 | + cur.execute("use "+name+";") |
| 20 | + cur.execute("CREATE TABLE "+nameTable+"("+valTypesStr+");") |
| 21 | + self._nameDataBase = name |
| 22 | + self._nameTable = nameTable |
| 23 | + else: |
| 24 | + cur.execute("use "+whereGetInfo+";") |
| 25 | + self._nameTable = input("Write name table: ") |
| 26 | + self._nameDataBase = whereGetInfo |
| 27 | + |
| 28 | + cur.close() |
| 29 | + |
| 30 | + def closeConn(self): |
| 31 | + saves = input("Save changes? (y/n) ") |
| 32 | + if (saves=="y"): |
| 33 | + self._conn.commit() |
| 34 | + self._conn.close() |
| 35 | + |
| 36 | + def getAllTableLikeMatrix(self): |
| 37 | + cur = self._conn.cursor() |
| 38 | + cur.execute("use "+self._nameDataBase+";") |
| 39 | + res=[[]] |
| 40 | + |
| 41 | + cur.execute("SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = '"+self._nameDataBase+"' AND table_name ='"+self._nameTable+"';") |
| 42 | + for i in cur: |
| 43 | + cols = i[0] |
| 44 | + |
| 45 | + cur.execute("SELECT * FROM "+self._nameTable+";") |
| 46 | + it=0 |
| 47 | + for r in cur: |
| 48 | + for i in range(cols): |
| 49 | + res[it].append(str(r[i])) |
| 50 | + res.append([]) |
| 51 | + it+=1 |
| 52 | + res.remove([]) |
| 53 | + |
| 54 | + cur.close() |
| 55 | + return res |
| 56 | + |
| 57 | + def printAll(self): |
| 58 | + cur = DB.getAllTableLikeMatrix(self) |
| 59 | + empty = [] |
| 60 | + if (cur==empty): |
| 61 | + print("empty") |
| 62 | + for r in cur: |
| 63 | + print(r) |
| 64 | + |
| 65 | + def findById(self,value): |
| 66 | + cur = self._conn.cursor() |
| 67 | + cur.execute("use "+self._nameDataBase+";") |
| 68 | + cur.execute("SELECT * FROM "+self._nameTable+" WHERE id = "+str(value)+";") |
| 69 | + row=[] |
| 70 | + for i in cur: |
| 71 | + for j in i: |
| 72 | + row.append(j) |
| 73 | + print (row) |
| 74 | + cur.close() |
| 75 | + |
| 76 | + def deleteById(self,value): |
| 77 | + cur = self._conn.cursor() |
| 78 | + cur.execute("use "+self._nameDataBase+";") |
| 79 | + cur.execute("DELETE FROM "+self._nameTable+" WHERE id = "+str(value)+";") |
| 80 | + cur.close() |
| 81 | + |
| 82 | + def updateById(self,setId): |
| 83 | + cur = self._conn.cursor() |
| 84 | + column = input("Column - ") |
| 85 | + value = input("Value - ") |
| 86 | + cur.execute("use "+self._nameDataBase+";") |
| 87 | + cur.execute("UPDATE "+self._nameTable+" SET "+str(column)+"='"+str(value)+"' WHERE id = "+str(setId)+";") |
| 88 | + cur.close() |
| 89 | + |
| 90 | + def insertFromUser(self): |
| 91 | + cur = self._conn.cursor() |
| 92 | + cur.execute("use "+self._nameDataBase+";") |
| 93 | + cur.execute("SHOW COLUMNS FROM "+self._nameTable+";") |
| 94 | + |
| 95 | + #<UaStyle>> |
| 96 | + colsNamesList = [] |
| 97 | + colsValsList = [] |
| 98 | + cur.execute("SELECT COUNT(*) FROM "+self._nameTable+";") |
| 99 | + for j in cur: |
| 100 | + rows = j[0] |
| 101 | + cur.execute("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = '"+self._nameDataBase+"' AND table_name ='"+self._nameTable+"';") |
| 102 | + for r in cur: |
| 103 | + colsNamesList.append(str(r[0])) |
| 104 | + colsNamesStr = ",".join(colsNamesList) |
| 105 | + for t in colsNamesList: |
| 106 | + temp = input(str(t)+" - ") |
| 107 | + try: |
| 108 | + int(temp) |
| 109 | + except ValueError: |
| 110 | + temp = "'"+temp+"'" |
| 111 | + colsValsList.append(str(temp)) |
| 112 | + colsValsStr = ",".join(colsValsList) |
| 113 | + cur.execute("INSERT INTO "+self._nameTable+" ("+colsNamesStr+") VALUES ("+colsValsStr+");") |
| 114 | + #</UaStyle> |
| 115 | + |
| 116 | + cur.close() |
| 117 | + |
| 118 | +class menu: |
| 119 | + def __init__(self): |
| 120 | + pass |
| 121 | + |
| 122 | + def mainLoop(self, obj): |
| 123 | + print(" 'p' - print table \n 'f' - find by id \n 'i' - insert \n 'd' - delete by id \n 'u' - update by id \n 'h' - help \n 'e' - exit \n") |
| 124 | + while True: |
| 125 | + cmd = input("-> ") |
| 126 | + if (cmd=="p"): |
| 127 | + obj.printAll() |
| 128 | + if (cmd=="f"): |
| 129 | + obj.findById(int(input("ID - "))) |
| 130 | + if (cmd=="i"): |
| 131 | + obj.insertFromUser() |
| 132 | + if (cmd=="d"): |
| 133 | + obj.deleteById(int(input("ID - "))) |
| 134 | + if (cmd=="u"): |
| 135 | + obj.updateById(int(input("ID - "))) |
| 136 | + if (cmd=="h"): |
| 137 | + print(" 'p' - print table \n 'f' - find by id \n 'i' - insert \n 'd' - delete by id \n 'u' - update by id \n 'h' - help \n 'e' - exit \n") |
| 138 | + if (cmd=="e"): |
| 139 | + break |
| 140 | + |
| 141 | +if __name__ == "__main__": |
| 142 | + mainMenu = menu() |
| 143 | + database = DB(input("Write DB for open or 'new' for create ")) |
| 144 | + |
| 145 | + mainMenu.mainLoop(database) |
| 146 | + |
| 147 | + database.closeConn() |
| 148 | + del database |
0 commit comments