Skip to content
Snippets Groups Projects
Commit 838d1cd6 authored by bence92's avatar bence92
Browse files

initial commit

parents
No related branches found
No related tags found
No related merge requests found
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
# Sphinx documentation
docs/_build/
# PyBuilder
target/
*.csv
\ No newline at end of file
main.py 0 → 100644
from __future__ import print_function
import csv
import wx
import wx.grid
from model import Model
def refit_gui(func):
def func_wrapper(self, event):
func(self, event)
self.vbox.Fit(self)
return func_wrapper
class MyFrame(wx.Frame):
def _on_quit(self, event):
self.Close(True)
def _on_save(self, event):
print('on_save called')
with open('eggs.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
@refit_gui
def _on_load_tables(self, event):
tables = self.model.get_all_table_names()
for table_name in tables:
self.my_grid.AppendRows(1)
self.my_grid.SetCellValue(self.my_grid.GetNumberRows()-1, 1, table_name)
def __init__(self, parent, title):
self.model = Model(host='localhost', db='mysql', user='root', password='asdasd1337')
wx.Frame.__init__(self, parent, title=title)
self.panel = wx.Panel(self, wx.ID_ANY)
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.CreateStatusBar()
file_menu = wx.Menu()
quit_action = file_menu.Append(wx.ID_EXIT, '&Exit', 'Exit')
menu_bar = wx.MenuBar()
menu_bar.Append(file_menu, '&File')
self.SetMenuBar(menu_bar)
self.my_grid = wx.grid.Grid(self.panel)
self.my_grid.CreateGrid(0, 2)
attr = wx.grid.GridCellAttr()
attr.SetEditor(wx.grid.GridCellBoolEditor())
attr.SetRenderer(wx.grid.GridCellBoolRenderer())
attr.SetAlignment(wx.ALIGN_CENTER, wx.ALIGN_CENTER)
self.my_grid.SetColAttr(0, attr)
self.my_grid.SetColLabelValue(0, 'Save')
self.my_grid.SetColLabelValue(1, 'Table name')
self.my_grid.SetColSize(0, 40)
self.my_grid.SetColSize(1, 120)
save_button = wx.Button(self.panel, wx.ID_ANY, 'Save')
load_button = wx.Button(self.panel, wx.ID_ANY, 'Load tables')
self.vbox.Add(self.my_grid, 0, wx.ALIGN_CENTER)
self.vbox.Add(wx.StaticLine(self.panel,), 0, wx.ALL|wx.EXPAND, 5)
hbox = wx.BoxSizer(wx.HORIZONTAL)
hbox.Add(save_button, 0, wx.ALL | wx.CENTER, 5)
hbox.Add(load_button, 0, wx.ALL | wx.CENTER, 5)
self.vbox.Add(hbox, 0, wx.ALL | wx.CENTER, 5)
self.Bind(wx.EVT_MENU, self._on_quit, quit_action)
self.Bind(wx.EVT_BUTTON, self._on_save, save_button)
self.Bind(wx.EVT_BUTTON, self._on_load_tables, load_button)
self.panel.SetSizer(self.vbox)
self.vbox.Fit(self)
if __name__ == '__main__':
app = wx.App(False)
frame = MyFrame(None, 'Database extractor')
frame.Show(True)
app.MainLoop()
model.py 0 → 100644
import MySQLdb
class Model:
def _get_connection(self):
"""Connects to the database if necessary, and returns the connection object."""
if self.connection is None:
self.connection = MySQLdb.connect(
host=self.host,
db=self.db,
user=self.user,
passwd=self.password
)
return self.connection
def _close_connection(self):
"""Closes the current db connection."""
if self.connection is not None:
self.connection.close()
self.connection = None
def _get_cursor(self):
"""Returns a cursor for the a new connection."""
db_connection = self._get_connection()
return db_connection.cursor()
def get_all_table_names(self):
"""Returns a list of the tables in the current database connection"""
cursor = self._get_cursor()
cursor.execute('SHOW TABLES')
return [element[0] for element in cursor.fetchall()]
def __init__(self, host, db, user, password):
self.host = host
self.db = db
self.user = user
self.password = password
self.connection = None
def __del__(self):
self._close_connection()
if __name__ == '__main__':
print('Connecting to a dummy database (root@localhost)')
m = Model(
host='localhost',
db='mysql',
user='root',
password='asdasd1337'
)
print('All the tables in the \'mysql\' database:')
print(m.get_all_table_names())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment