fold all codes

mysql表结构查看

项目开发时候要看着表结构写,实在是受不了phpmyadmin或者是navicat,就用qt写了一个看表结构的小玩意,主要是看表字段的注释。

使用过程中发现过一段时间,连接就会被mysqld自动断开,就在每次查询前多执行一个“select 1;”,反正是开发时候用,浪费资源就浪费吧。

 1 #include <QApplication>
 2 #include "mainwindow.h"
 3 
 4 int main(int argc, char *argv[])
 5 {
 6     QApplication a(argc, argv);
 7     MainWindow w;
 8     w.show();
 9     
10     return a.exec();
11 }

 

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QtGui>
 5 #include <QtSql>
 6 
 7 class MainWindow : public QMainWindow
 8 {
 9     Q_OBJECT
10 
11 public:
12     MainWindow(QWidget *parent = 0);
13     ~MainWindow();
14 
15 private slots:
16     void databaseRefleshActionTriggered();
17 
18     void tableRefleshActionTriggered();
19     void tableActionGroupTriggered(QAction *);
20 
21     void removeTab(int i)
22     {
23         tabWidget->removeTab(i);
24     }
25 
26 private:
27     QSqlQuery *query(const QString &sql)
28     {
29         if ((!db.isOpen() || !QSqlQuery("select 1;", db).isActive()) && !db.open())
30             QMessageBox::warning(this, "Error", db.lastError().text());
31 
32         return new QSqlQuery(sql, db);
33     }
34 
35 private:
36     QWidget *widget;
37     QVBoxLayout *layout;
38     QTabWidget *tabWidget;
39     QMap<QString, QTableView *> commentViews;
40 
41     QMenu *databaseMenu;
42     QAction *databaseRefleshAction;
43     QActionGroup *databaseActionGroup;
44 
45     QMenu *tableMenu;
46     QAction *tableRefleshAction;
47     QActionGroup *tableActionGroup;
48 
49     QSqlDatabase db;
50 };
51 
52 #endif // MAINWINDOW_H
  1 #include "mainwindow.h"
  2 
  3 MainWindow::MainWindow(QWidget *parent)
  4     : QMainWindow(parent), db(QSqlDatabase::addDatabase("QMYSQL"))
  5 {
  6     db.setHostName("localhost");
  7     db.setUserName("root");
  8     db.setPassword("");
  9 
 10     setWindowTitle("Table Comment Viewer");
 11     resize(600, 400);
 12 
 13     databaseMenu = menuBar()->addMenu("Database");
 14     databaseRefleshAction = databaseMenu->addAction("Reflesh");
 15     databaseMenu->addSeparator();
 16     databaseActionGroup = new QActionGroup(this);
 17     databaseActionGroup->setExclusive(true);
 18 
 19     connect(databaseRefleshAction, SIGNAL(triggered()), this, SLOT(databaseRefleshActionTriggered()));
 20     connect(databaseActionGroup, SIGNAL(triggered(QAction*)), this, SLOT(tableRefleshActionTriggered()));
 21 
 22     tableMenu = menuBar()->addMenu("Table");
 23     tableRefleshAction = tableMenu->addAction("Reflesh");
 24     tableMenu->addSeparator();
 25     tableActionGroup = new QActionGroup(this);
 26 
 27     connect(tableRefleshAction, SIGNAL(triggered()), this, SLOT(tableRefleshActionTriggered()));
 28     connect(tableActionGroup, SIGNAL(triggered(QAction*)), this, SLOT(tableActionGroupTriggered(QAction *)));
 29 
 30     widget = new QWidget;
 31     setCentralWidget(widget);
 32 
 33     layout = new QVBoxLayout;
 34     widget->setLayout(layout);
 35 
 36     tabWidget = new QTabWidget;
 37     tabWidget->setMovable(true);
 38     tabWidget->setTabsClosable(true);
 39     layout->addWidget(tabWidget);
 40 
 41     connect(tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(removeTab(int)));
 42 
 43     databaseRefleshActionTriggered();
 44 }
 45 
 46 MainWindow::~MainWindow()
 47 {
 48 }
 49 
 50 void MainWindow::databaseRefleshActionTriggered()
 51 {
 52     QList<QAction *> acs = databaseActionGroup->actions();
 53     foreach (QAction *ac, acs)
 54     {
 55         databaseMenu->removeAction(ac);
 56         databaseActionGroup->removeAction(ac);
 57         delete ac;
 58     }
 59     acs.clear();
 60 
 61     QStringList ss;
 62     QSqlQuery *q = query("show databases;");
 63 
 64     while (q->next())
 65         ss << q->value(0).toString();
 66 
 67     foreach (const QString &s, ss)
 68     {
 69         databaseActionGroup->addAction(s)->setCheckable(true);
 70     }
 71 
 72     databaseMenu->addActions(databaseActionGroup->actions());
 73     tableRefleshActionTriggered();
 74 }
 75 
 76 void MainWindow::tableRefleshActionTriggered()
 77 {
 78     int c = tabWidget->count();
 79     for (int i = 0; i < c; i++)
 80     {
 81         tabWidget->removeTab(i);
 82     }
 83 
 84     foreach (QTableView *v, commentViews.values())
 85     {
 86         delete v;
 87     }
 88 
 89     commentViews.clear();
 90 
 91     QList<QAction *> acs = tableActionGroup->actions();
 92     foreach (QAction *ac, acs)
 93     {
 94         tableMenu->removeAction(ac);
 95         tableActionGroup->removeAction(ac);
 96         delete ac;
 97     }
 98     acs.clear();
 99 
100     QAction *ac = databaseActionGroup->checkedAction();
101     if (!ac)
102         return;
103 
104     QStringList ss;
105     QSqlQuery *q = query(QString("show tables from %1").arg(ac->text()));
106 
107     while (q->next())
108         ss << q->value(0).toString();
109 
110     foreach (const QString &s, ss)
111     {
112         tableActionGroup->addAction(s)->setWhatsThis("hahaha");
113     }
114 
115     tableMenu->addActions(tableActionGroup->actions());
116 }
117 
118 void MainWindow::tableActionGroupTriggered(QAction *ac)
119 {
120     QAction *dac = databaseActionGroup->checkedAction();
121     if (!dac)
122         return;
123 
124     QString table = ac->text();
125 
126     int c = tabWidget->count();
127     for (int i = 0; i < c; i++)
128     {
129         if (tabWidget->tabText(i) == table)
130         {
131             tabWidget->setCurrentIndex(i);
132             return;
133         }
134     }
135 
136     if (!commentViews.contains(table))
137     {
138         QString sql = QString("select column_name, concat(column_type, ' ', ifnull(column_default, '')),"
139         " concat(ifnull(character_set_name, ''), ' ', ifnull(collation_name, ''), ' ', column_key, ' ', extra), column_comment"
140         " from information_schema.columns"
141         " where table_schema = '%1' and table_name = '%2'"
142         " order by ordinal_position;").arg(dac->text()).arg(table);
143 
144         QSqlQueryModel *model = new QSqlQueryModel;
145 
146         QSqlQuery *q = query(sql);
147         model->setQuery(*q);
148 
149         model->setHeaderData(0, Qt::Horizontal, "Name");
150         model->setHeaderData(1, Qt::Horizontal, "Type & Default");
151         model->setHeaderData(2, Qt::Horizontal, "Extra");
152         model->setHeaderData(3, Qt::Horizontal, "Comment");
153 
154         QTableView *view = new QTableView;
155         view->setModel(model);
156         view->show();
157 
158         commentViews[table] = view;
159     }
160 
161     int i = tabWidget->addTab(commentViews[table], table);
162     tabWidget->setCurrentIndex(i);
163 }

 

posted @ 2012-07-18 17:58  hylent  阅读(3116)  评论(1编辑  收藏  举报