List MySql databases in a Gtk.TreeView
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #! /usr/bin/python
###########################################################
#
# List MySql databases in a Gtk.TreeView
#
###########################################################
from gi.repository import Gtk
import MySQLdb
import os
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title='My Window Title')
self.connect('delete-event', Gtk.main_quit)
store = Gtk.ListStore(str, str, str, str)
self.populate_store(store)
self.treeview = Gtk.TreeView(model=store)
renderer = Gtk.CellRendererText()
column_catalog = Gtk.TreeViewColumn('Catalog Name', renderer, text=0)
column_catalog.set_sort_column_id(0)
self.treeview.append_column(column_catalog)
column_dbname = Gtk.TreeViewColumn('Database Name', renderer, text=1)
column_dbname.set_sort_column_id(1)
self.treeview.append_column(column_dbname)
column_charset = Gtk.TreeViewColumn('Character Set', renderer, text=2)
column_charset.set_sort_column_id(2)
self.treeview.append_column(column_charset)
column_collation = Gtk.TreeViewColumn('Collation', renderer, text=3)
column_collation.set_sort_column_id(3)
self.treeview.append_column(column_collation)
scrolled_window = Gtk.ScrolledWindow()
scrolled_window.set_policy(
Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
scrolled_window.add(self.treeview)
scrolled_window.set_min_content_height(200)
self.add(scrolled_window)
self.show_all()
# Add data to ListStore
def populate_store(self, store):
try:
connection = None
connection = MySQLdb.connect('localhost', 'annon', 'pass')
cursor = connection.cursor()
cursor.execute("Select * From `INFORMATION_SCHEMA`.`SCHEMATA`")
rows = cursor.fetchall()
for row in rows:
store.append([row[0], row[1], row[2], row[3]])
except MySQLdb.Error, e:
store.append([str(e.args[0]), e.args[1], '', ''])
finally:
if connection != None:
connection.close()
win = MyWindow()
Gtk.main()
|