QT如果要进行网络编程首先需要在.pro中添加如下代码:QT += network
//logindlg.h
#ifndef LOGINDLG_H
#define LOGINDLG_H
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
class loginDlg : public QDialog
{
Q_OBJECT
public:
explicit loginDlg(QWidget *parent = 0);
signals:
public slots:
private:
QLabel *label1,*label2,*label3,*label4;
QLineEdit *edit1,*edit2,*edit3,*edit4;
QPushButton *btn1,*btn2;
private slots:
void btn1_click();
void btn2_click();
public:
QString username;
QString userpass;
QString dbname;
QString ipaddr;
/*判断用户是否点击登录按钮*/
bool islogin;
};
#endif // LOGINDLG_H
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMenu>
/*QMenuBar:菜单栏*/
#include <QMenuBar>
#include <QAction>
#include <QCloseEvent>
#include <QMdiArea>
#include "vmysql.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Vmysql *sqlhelper;
QMenu * menu1;
QMenu * menu2;
QMenu * menu3;
QMenu * menu4;
QAction *login_in,*login_out,*quit1;
QAction *addstation,*addstationcontrl,*addMSC;
QAction *delstation,*delstationcontrl,*delMSC;
QAction *sestation,*sestationcontrl,*seMSC;
QAction *upstation,*upstationcontrl,*upMSC,*exscript;
QAction *cascade1,*parallel;
QAction *helper,*mabout;
QMdiArea *mdiarea1;
/*初始化菜单项*/
void initmenubar();
/*退出函数*/
void closeEvent(QCloseEvent *event);
/*未登录设置所有SQL按钮不可用*/
void close_action();
/*设置所有SQL按钮可用*/
void open_action();
/*执行SQL操作*/
void exec_query(const char *strsql);
/*判断SQL操作的类型*/
bool judge_sqltype(const char *strsql);
/*创建显示view*/
void display_view(const char *actionname,QStandardItemModel *model);
private slots:
//登录事件
void login_inclick();
//注销事件
void login_outclick();
//退出事件
void quit_click();
/*添加基站*/
void add_station();
/*添加基站控制器*/
void add_stationcontrl();
/*添加MSC*/
void add_MSC();
/*删除基站*/
void del_station();
/*删除基站控制器*/
void del_stationcontrl();
/*删除MSC*/
void del_MSC();
/*查询基站*/
void sel_station();
/*查询基站控制器*/
void sel_stationcontrl();
/*查询MSC*/
void sel_MSC();
/*修改基站*/
void update_station();
/*修改基站控制器*/
void update_stationcontrl();
/*修改MSC*/
void update_MSC();
/*执行脚本*/
void exec_script();
/*层叠窗口*/
void cascade_window();
/*并列窗口*/
void parallel_window();
/*帮助*/
void helper_click();
/*关于*/
void mabout_click();
};
#endif // MAINWINDOW_H
【领QT开发教程学习资料,点击下方链接免费领取↓↓,先码住不迷路~】
点击→领取「链接」
//scriptdlg.h
#ifndef SCRIPTDLG_H
#define SCRIPTDLG_H
#include <QDialog>
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>
class ScriptDlg : public QDialog
{
Q_OBJECT
public:
explicit ScriptDlg(QWidget *parent = 0);
QString strsql;
bool isexec;
signals:
public slots:
private:
QLabel *label1;
QTextEdit *tedit1;
QPushButton *btn1,*btn2;
private slots:
void btn1_click();
void btn2_click();
};
#endif // SCRIPTDLG_H
//vmysql.h
#ifndef VMYSQL_H
#define VMYSQL_H
/*mysql属于TCP/IP协议,所以需要使用socket,
* windows下的socket与linux下不同,在windows.h文件下
* SOCKET listen_st;--windows下定义socket描述符*/
#include <windows.h>
#include <C:/mysql/include/mysql.h>
#include <QStandardItemModel>
/*封装所有和mysql相关函数*/
class Vmysql
{
public:
Vmysql();
~Vmysql();
/*连接数据库*/
int sql_connect(const char *hostname,const char *username,const char *passwd,const char *dbname);
/*断开数据库连接*/
void sql_disconnect();
/*执行SQL更新*/
int sql_exec(const char *strsql);
/*执行SQL查询*/
int sql_query(const char *strsql,QStandardItemModel **model);
/*错误信息*/
const char *ger_error();
private:
MYSQL mysql1;
MYSQL *connection;
char errorbuf[1024];
};
#endif // VMYSQL_H
//logindlg.cpp
#include "logindlg.h"
#include <QGridLayout>
#include <QHBoxLayout>
#include <QPalette>
loginDlg::loginDlg(QWidget *parent) :
QDialog(parent)
{
/*默认没有点击登录*/
islogin=false;
this->setWindowTitle(tr("登录"));
label1=new QLabel(tr("用户ID:"));
edit1=new QLineEdit();
label2=new QLabel(tr("密码:"));
edit2=new QLineEdit();
/*将QLineEdit设置成密码框*/
edit2->setEchoMode(QLineEdit::Password);
label3=new QLabel(tr("数据库名称:"));
edit3=new QLineEdit();
label4=new QLabel(tr("服务器IP:"));
edit4=new QLineEdit();
btn1=new QPushButton(tr("登录"));
connect(btn1,SIGNAL(clicked()),this,SLOT(btn1_click()));
btn2=new QPushButton(tr("取消"));
connect(btn2,SIGNAL(clicked()),this,SLOT(btn2_click()));
QHBoxLayout *lay2=new QHBoxLayout();
lay2->addWidget(btn1);
lay2->addWidget(btn2);
QGridLayout *lay1=new QGridLayout(this);
lay1->addWidget(label1,0,0);
lay1->addWidget(edit1,0,1);
lay1->addWidget(label2,1,0);
lay1->addWidget(edit2,1,1);
lay1->addWidget(label3,2,0);
lay1->addWidget(edit3,2,1);
lay1->addWidget(label4,3,0);
lay1->addWidget(edit4,3,1);
lay1->addLayout(lay2,4,0,1,2);
/*设置第0列占位比为1*/
lay1->setColumnStretch(0,1);
/*设置第1列占位比为1*/
lay1->setColumnStretch(1,1);
/*设置边距*/
lay1->setMargin(15);
/*设置控件间的间距*/
lay1->setSpacing(10);
/*设置窗口大小不可以随意改变*/
lay1->setSizeConstraint(QLayout::SetFixedSize);
/*设置图片填充满对话框背景*/
this->setAutoFillBackground(true);
/*设置对话框背景图片*/
QPalette palette1;
palette1.setBrush(QPalette::Background,QBrush(QPixmap("12.jpg")));
this->setPalette(palette1);
}
void loginDlg::btn1_click()
{
/*用户点击登录*/
islogin=true;
//获取用户输入信息
username=edit1->text();
userpass=edit2->text();
dbname=edit3->text();
ipaddr=edit4->text();
/*在登录方法只获取信息,在主函数中处理这些信息,这是为了安全*/
this->close();
/*不关闭对话框,Dialog的exec()方法会永远阻塞*/
}
void loginDlg::btn2_click()
{
/*用户点击取消*/
islogin=false;
this->close();
}
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.resize(700,500);
w.show();
return a.exec();
}
//scriptdlg.cpp
#include "scriptdlg.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
ScriptDlg::ScriptDlg(QWidget *parent) :
QDialog(parent)
{
isexec=false;
this->setWindowTitle("执行SQL");
label1=new QLabel(tr("请输入SQL"));
tedit1=new QTextEdit();
btn1=new QPushButton(tr("执行"));
connect(btn1,SIGNAL(clicked()),this,SLOT(btn1_click()));
btn2=new QPushButton(tr("取消"));
connect(btn2,SIGNAL(clicked()),this,SLOT(btn2_click()));
QHBoxLayout *lay1=new QHBoxLayout();
lay1->addWidget(btn1);
lay1->addWidget(btn2);
QVBoxLayout *lay2=new QVBoxLayout();
lay2->addWidget(tedit1);
lay2->addLayout(lay1);
QHBoxLayout *lay3=new QHBoxLayout(this);
lay3->addWidget(label1);
lay3->addLayout(lay2);
/*设置边距*/
lay3->setMargin(15);
/*设置间距*/
lay3->setSpacing(10);
}
void ScriptDlg::btn1_click()
{
isexec=true;
//获取用户输入
this->strsql=tedit1->toPlainText();
this->close();
}
void ScriptDlg::btn2_click()
{
this->close();
}
//mainwindow.cpp
#include "mainwindow.h"
#include "logindlg.h"
#include "scriptdlg.h"
#include <QMessageBox>
#include <QIcon>
#include <QMdiSubWindow>
#include <QStandardItemModel>
#include <QTableView>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
/*设置窗口标题*/
this->setWindowTitle(tr("CDMA无线基站管理系统"));
/*设置窗口光标(窗口左上角图片)*/
this->setWindowIcon(QIcon("main.png"));
/*QMdiArea控件只能在MainWindow控件中使用,是用来实现多文档界面的必备控件*/
mdiarea1=new QMdiArea();
/*设置QMdiArea控件的背景图片*/
mdiarea1->setBackground(Qt::NoBrush);
/*背景图片设置为11.jpg,子窗口大小可以调整*/
mdiarea1->setStyleSheet("background-image:url(10.jpg)");
/*当子窗口的范围超过父窗口的显示范围时,设置自动具有横向滚动条*/
mdiarea1->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
/*设置自动具有纵向滚动条*/
mdiarea1->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
this->setCentralWidget(mdiarea1);
/*初始化基本控件*/
initmenubar();
/*初始化数据库连接*/
sqlhelper=new Vmysql();
}
//初始化控件
void MainWindow::initmenubar()
{
/*管理菜单*/
menu1=this->menuBar()->addMenu(tr("管理"));
login_in=new QAction(tr("登录"),this);
login_in->setShortcut(tr("Ctrl+U"));
connect(login_in,SIGNAL(triggered()),this,SLOT(login_inclick()));
login_out=new QAction(tr("注销"),this);
login_out->setShortcut(tr("Ctrl+B"));
connect(login_out,SIGNAL(triggered()),this,SLOT(login_outclick()));
quit1=new QAction(tr("退出"),this);
quit1->setShortcut(tr("Ctrl+W"));
connect(quit1,SIGNAL(triggered()),this,SLOT(quit_click()));
menu1->addAction(login_in);
menu1->addAction(login_out);
/*加入空格*/
menu1->addSeparator();
menu1->addAction(quit1);
/*数据菜单*/
menu2=this->menuBar()->addMenu(tr("数据"));
addstation=new QAction(tr("添加基站"),this);
addstation->setShortcut(tr("Ctrl+D"));\
//addstation->setEnabled(false);
connect(addstation,SIGNAL(triggered()),this,SLOT(add_station()));
addstationcontrl=new QAction(tr("添加基站控制器"),this);
addstationcontrl->setShortcut(tr("Ctrl+E"));
connect(addstationcontrl,SIGNAL(triggered()),this,SLOT(add_stationcontrl()));
addMSC=new QAction(tr("添加MSC"),this);
addMSC->setShortcut(tr("Ctrl+F"));
connect(addMSC,SIGNAL(triggered()),this,SLOT(add_MSC()));
delstation=new QAction(tr("删除基站"),this);
delstation->setShortcut(tr("Ctrl+G"));
connect(delstation,SIGNAL(triggered()),this,SLOT(del_station()));
delstationcontrl=new QAction(tr("删除基站控制器"),this);
delstationcontrl->setShortcut(tr("Ctrl+H"));
connect(delstationcontrl,SIGNAL(triggered()),this,SLOT(del_stationcontrl()));
delMSC=new QAction(tr("删除MSC"),this);
delMSC->setShortcut(tr("Ctrl+I"));
connect(delMSC,SIGNAL(triggered()),this,SLOT(del_MSC()));
sestation=new QAction(tr("查询基站"),this);
sestation->setShortcut(tr("Ctrl+J"));
connect(sestation,SIGNAL(triggered()),this,SLOT(sel_station()));
sestationcontrl=new QAction(tr("查询基站控制器"),this);
sestationcontrl->setShortcut(tr("Ctrl+K"));
connect(sestationcontrl,SIGNAL(triggered()),this,SLOT(sel_stationcontrl()));
seMSC=new QAction(tr("查询MSC"),this);
seMSC->setShortcut(tr("Ctrl+L"));
connect(seMSC,SIGNAL(triggered()),this,SLOT(sel_MSC()));
upstation=new QAction(tr("修改基站"),this);
upstation->setShortcut(tr("Ctrl+M"));
connect(upstation,SIGNAL(triggered()),this,SLOT(update_station()));
upstationcontrl=new QAction(tr("修改基站控制器"),this);
upstationcontrl->setShortcut(tr("Ctrl+N"));
connect(upstationcontrl,SIGNAL(triggered()),this,SLOT(update_stationcontrl()));
upMSC=new QAction(tr("修改MSC"),this);
upMSC->setShortcut(tr("Ctrl+O"));
connect(upMSC,SIGNAL(triggered()),this,SLOT(update_MSC()));
exscript=new QAction(tr("执行脚本"),this);
exscript->setShortcut(tr("Ctrl+P"));
connect(exscript,SIGNAL(triggered()),this,SLOT(exec_script()));
menu2->addAction(addstation);
menu2->addAction(addstationcontrl);
menu2->addAction(addMSC);
menu2->addAction(delstation);
menu2->addAction(delstationcontrl);
menu2->addAction(delMSC);
menu2->addAction(sestation);
menu2->addAction(sestationcontrl);
menu2->addAction(seMSC);
menu2->addAction(upstation);
menu2->addAction(upstationcontrl);
menu2->addAction(upMSC);
menu2->addAction(exscript);
/*窗口菜单*/
menu3=this->menuBar()->addMenu(tr("窗口"));
cascade1=new QAction(tr("层叠窗口"),this);
cascade1->setShortcut(tr("Ctrl+Q"));
connect(cascade1,SIGNAL(triggered()),this,SLOT(cascade_window()));
parallel=new QAction(tr("并列窗口"),this);
parallel->setShortcut(tr("Ctrl+R"));
connect(parallel,SIGNAL(triggered()),this,SLOT(parallel_window()));
menu3->addAction(cascade1);
menu3->addAction(parallel);
/*帮助菜单*/
menu4=this->menuBar()->addMenu(tr("帮助"));
helper=new QAction(tr("帮助"),this);
helper->setShortcut(tr("Ctrl+s"));
connect(helper,SIGNAL(triggered()),this,SLOT(helper_click()));
mabout=new QAction(tr("关于"),this);
mabout->setShortcut(tr("Ctrl+T"));
connect(mabout,SIGNAL(triggered()),this,SLOT(mabout_click()));
menu4->addAction(helper);
menu4->addSeparator();
menu4->addAction(mabout);
/*未登录,禁用所有按钮*/
close_action();
}
/*登录*/
void MainWindow::login_inclick()
{
loginDlg logd1;
/*exec()方法会产生一个模式对话框,show()方法只会产生普通的对话框*/
logd1.exec();
/*
* 当模式对话框打开时,主窗口是不能接受输入输出的;
* 当非模式对话框打开时, 主窗口可以接受输入输出
* ----------------------------------
* 模式对话框与非模式对话框的重大区别:
* exec()函数会阻塞当前进程,
* 但是show()不会阻塞当前进程,会使login_inclick()函数直接执行完毕,
* logd1是一个局部变量,login_inclick()函数执行完毕,会导致logd1的内存被释放,
* 从而导致界面上对话框也随着消失,所以用show()看不到结果,
* 但是可以通过将logd1变量定义在堆内存上,避免这种情况,
* 但是这也会导致用户可以打开多个登陆对话框*/
//logd1.show();
if(!logd1.islogin)
{
/*判断用户是否点击了取消按钮*/
return;
}
if(logd1.username.isEmpty())
{
QMessageBox::critical(this,"错误信息","用户名不可以为空!");
return;
}
if(logd1.userpass.isEmpty())
{
QMessageBox::critical(this,"错误信息","密码不可以为空!");
return;
}
if(logd1.dbname.isEmpty())
{
QMessageBox::critical(this,"错误信息","数据库名称不可以为空!");
return;
}
if(logd1.ipaddr.isEmpty())
{
QMessageBox::critical(this,"错误信息","IP地址不可以为空!");
return;
}
/*连接数据库*/
int res=sqlhelper->sql_connect(logd1.ipaddr.toStdString().data()
,logd1.username.toStdString().data()
,logd1.userpass.toStdString().data()
,logd1.dbname.toStdString().data());
if(res<0)
{
QMessageBox::critical(this,"错误信息",sqlhelper->ger_error());
}else
{
QMessageBox::information(this,"提示信息","登录成功");
/*开启所有SQL按钮*/
open_action();
}
}
//注销
void MainWindow::login_outclick()
{
if(QMessageBox::question(this,"确认退出","您确定要注销吗?",QMessageBox::Ok|QMessageBox::No,QMessageBox::Ok)==QMessageBox::Yes)
{
//断开数据库连接
sqlhelper->sql_disconnect();
/*禁用所有按钮*/
close_action();
}
}
//退出
void MainWindow::quit_click()
{
this->close();
}
/*添加基站*/
void MainWindow::add_station()
{
//
}
/*添加基站控制器*/
void MainWindow::add_stationcontrl()
{
//
}
/*添加MSC*/
void MainWindow::add_MSC()
{
//
}
/*删除基站*/
void MainWindow::del_station()
{
//
}
/*删除基站控制器*/
void MainWindow::del_stationcontrl()
{
//
}
/*删除MSC*/
void MainWindow::del_MSC()
{
//
}
/*查询基站*/
void MainWindow::sel_station()
{
//
}
/*查询基站控制器*/
void MainWindow::sel_stationcontrl()
{
//
}
/*查询MSC*/
void MainWindow::sel_MSC()
{
//
}
/*修改基站*/
void MainWindow::update_station()
{
//
}
/*修改基站控制器*/
void MainWindow::update_stationcontrl()
{
//
}
/*修改MSC*/
void MainWindow::update_MSC()
{
//
}
/*执行脚本*/
void MainWindow::exec_script()
{
ScriptDlg w1;
w1.exec();
if(!w1.isexec) return;
exec_query(w1.strsql.toStdString().data());
}
/*执行SQL查询*/
void MainWindow::exec_query(const char *strsql)
{
if(!judge_sqltype(strsql))
{
//执行SQL更新
if(sqlhelper->sql_exec(strsql)<0)
{
QMessageBox::critical(this,"错误提示",sqlhelper->ger_error());
}
}else
{
//执行SQL查询
QStandardItemModel *model=NULL;
if(sqlhelper->sql_query(strsql,&model)<0)
{
QMessageBox::critical(this,"错误提示",sqlhelper->ger_error());
return;
}
if(model==NULL)
{
QMessageBox::critical(this,"错误提示","model 为空!");
return;
}
/*创建view*/
display_view("查询结果",model);
}
}
/*判断SQL操作的类型*/
bool MainWindow::judge_sqltype(const char *strsql)
{
bool flag=true;
//1.判断SQL执行的是查询还是更新
char tempbuf[100]={0};
/*遍历字符数据,将第一个空格前的所有字符转化成大写*/
for(int i=0;i<(int)strlen(strsql);i++)
{
if(strsql[i]!=' ')
{
if(strsql[i]>96&&strsql[i]<123)
{
//小写转化成大写
tempbuf[i]=strsql[i]-32;
}else
{
tempbuf[i]=strsql[i];
}
}else
{
break;
}
}
if(strncmp(tempbuf,"SET",3)==0)
{
flag=false;
}else if(strncmp(tempbuf,"INSERT",6)==0)
{
flag=false;
}else if(strncmp(tempbuf,"DELETE",6)==0)
{
flag=false;
}else if(strncmp(tempbuf,"UPDATE",6)==0)
{
flag=false;
}else if(strncmp(tempbuf,"USE",3)==0)
{
flag=false;
}
return flag;
}
/*创建显示view*/
void MainWindow::display_view(const char *actionname,QStandardItemModel *model)
{
/*在QT中,model中存放数据,view中显示数据*/
/*定义view,QTableView继承于QWidget,QTableView在被销毁的同时,会自动销毁对应的model*/
QTableView *view1=new QTableView();
/*代表关闭这个widget的时候,自动将这个widget delete*/
view1->setAttribute(Qt::WA_DeleteOnClose);
/*多文档控件QMdiArea通过addSubWindow()将一个widget变成一个子窗口*/
mdiarea1->addSubWindow(view1);
view1->setWindowTitle(actionname);
/*设置背景图片*/
view1->setStyleSheet("background-image:url(11.jpg)");
/*view1继承于QWidget,如果没有model,那么view不会显示数据*/
view1->setModel(model);
view1->show();
/*必须在show()方法之后使用activeSubWindow()获取在最前面的子窗口*/
/*多文档中的子窗口widget设置大小,用resize()方法没有效果*/
//w1->resize(300,200);
/*QMdiArea控件通过activeSubWindow()方法得到当前活动窗口(最前边的子窗口)*/
/*注意引用头文件QMdiSubWindow */
mdiarea1->activeSubWindow()->resize(this->width()-100,this->height()-100);
}
/*层叠窗口*/
void MainWindow::cascade_window()
{
mdiarea1->cascadeSubWindows();
}
/*并列窗口*/
void MainWindow::parallel_window()
{
mdiarea1->tileSubWindows();
}
/*帮助*/
void MainWindow::helper_click()
{
QMessageBox::information(this,"帮助信息","暂无帮助信息");
}
/*关于*/
void MainWindow::mabout_click()
{
QMessageBox::about(this,"关于","版权所有,侵权必究!");
}
/*退出程序事件*/
void MainWindow::closeEvent(QCloseEvent *event)
{
if(QMessageBox::question(this,"退出程序",tr("您确定要退出吗?"),QMessageBox::Yes|QMessageBox::No,QMessageBox::No)==QMessageBox::Yes)
{
event->accept();
}else{
event->ignore();
}
}
/*未登录设置所有SQL按钮不可用*/
void MainWindow::close_action()
{
addstation->setEnabled(false);
addstationcontrl->setEnabled(false);
addMSC->setEnabled(false);
delstation->setEnabled(false);
delstationcontrl->setEnabled(false);
delMSC->setEnabled(false);
sestation->setEnabled(false);
sestationcontrl->setEnabled(false);
seMSC->setEnabled(false);
upstation->setEnabled(false);
upstationcontrl->setEnabled(false);
upMSC->setEnabled(false);
exscript->setEnabled(false);
cascade1->setEnabled(false);
parallel->setEnabled(false);
}
/*设置所有SQL按钮可用*/
void MainWindow::open_action()
{
addstation->setEnabled(true);
addstationcontrl->setEnabled(true);
addMSC->setEnabled(true);
delstation->setEnabled(true);
delstationcontrl->setEnabled(true);
delMSC->setEnabled(true);
sestation->setEnabled(true);
sestationcontrl->setEnabled(true);
seMSC->setEnabled(true);
upstation->setEnabled(true);
upstationcontrl->setEnabled(true);
upMSC->setEnabled(true);
exscript->setEnabled(true);
cascade1->setEnabled(true);
parallel->setEnabled(true);
}
MainWindow::~MainWindow()
{
/*释放数据库连接*/
delete sqlhelper;
}
//vmysql.cpp
#include "vmysql.h"
#include <QMessageBox>
Vmysql::Vmysql()
{
mysql_init(&mysql1);
connection=NULL;
memset(errorbuf,0,sizeof(errorbuf));
}
Vmysql::~Vmysql()
{
}
/*连接数据库*/
int Vmysql::sql_connect(const char *hostname, const char *username, const char *passwd, const char *dbname)
{
if(hostname==NULL||username==NULL||passwd==NULL||dbname==NULL)
{
memset(errorbuf,0,sizeof(errorbuf));
strcpy(errorbuf,"sql_connect() 参数不可以为空!");
/*这里没有父窗口,所以第一个参数为0*/
//QMessageBox::critical(0,"错误信息","sql_connect() 参数不可以为空!");
return -1;
}
connection=mysql_real_connect(&mysql1,hostname,username,passwd,dbname,0,0,0);
if(connection==NULL)
{
memset(errorbuf,0,sizeof(errorbuf));
strcpy(errorbuf,mysql_error(&mysql1));
//QMessageBox::critical(0,"错误信息",mysql_error(&mysql1));
return -1;
}
/*连接成功,设置字符集*/
if(mysql_query(connection,"SET NAMES utf8")!=0)
{
memset(errorbuf,0,sizeof(errorbuf));
strcpy(errorbuf,mysql_error(&mysql1));
return -1;
}
return 0;
}
/*断开数据库连接*/
void Vmysql::sql_disconnect()
{
if(connection)
{
mysql_close(connection);
connection=NULL;
}
}
/*执行SQL更新*/
int Vmysql::sql_exec(const char *strsql)
{
if(strsql==NULL)
{
memset(errorbuf,0,sizeof(errorbuf));
strcpy(errorbuf,"sql_exec() 参数不可以为空!");
return -1;
}
if(mysql_query(connection,strsql)!=0)
{
memset(errorbuf,0,sizeof(errorbuf));
strcpy(errorbuf,mysql_error(&mysql1));
return -1;
}
return 0;
}
/*执行SQL查询*/
int Vmysql::sql_query(const char *strsql,QStandardItemModel **model)
{
if(strsql==NULL||model==NULL)
{
memset(errorbuf,0,sizeof(errorbuf));
strcpy(errorbuf,"sql_query() 参数不可以为空!");
return -1;
}
if(mysql_query(connection,strsql)!=0)
{
memset(errorbuf,0,sizeof(errorbuf));
strcpy(errorbuf,mysql_error(&mysql1));
return -1;
}
/*获取结果集*/
MYSQL_RES *result=mysql_store_result(connection);
/*mysql_affected_rows(connection);返回当前数据集有多少行
* mysql_field_count(connection);返回当前数据集有多少列*/
/*特别注意:mysql_affected_rows()或者mysql_field_count()方法必须在mysql_store_result()之后使用*/
int rowcount = mysql_affected_rows(connection);
int fieldcount=mysql_field_count(connection);
if(result==NULL)
{
memset(errorbuf,0,sizeof(errorbuf));
strcpy(errorbuf,mysql_error(&mysql1));
return -1;
}
/*创建返回model*/
/*创建一个rowcount行fieldcount列的model数据集;
* 根据SQL语句返回的行列总数,动态的创建model*/
QStandardItemModel *tmodel=new QStandardItemModel(rowcount,fieldcount);
/*创建列数据*/
MYSQL_FIELD *field=NULL;
for(int i=0;i<fieldcount;i++)
{
field=mysql_fetch_field(result);
/*设置第i列的列名,第一个参数是指第几列,第二个参数是个固定参数,第三个参数是列名*/
tmodel->setHeaderData(i,Qt::Horizontal,field->name);
}
/*创建单元格数据*/
MYSQL_ROW row=NULL;
for(int i=0;i<rowcount;i++)
{
row=mysql_fetch_row(result);
for(int j=0;j<fieldcount;j++)
{
/*设置model每个单元格的数据*/
/*model->index(0,0,QModelIndex()),index()函数第一个参数表示第几行,第二个参数表示第几列,第三参数是固定写法*/
tmodel->setData(tmodel->index(i,j,QModelIndex()),row[j]);
}
}
*model=tmodel;
/*释放mysql_store_result()函数分配的内存空间*/
mysql_free_result(result);
return 0;
}
/*错误信息*/
const char *Vmysql::ger_error()
{
return this->errorbuf;
}
本文暂时没有评论,来添加一个吧(●'◡'●)