笔记n多,好久没总结之前学的新东西了,惭愧!!!
Qt连接数据库超简单代码
#include <QApplication>
#include <QtSql/QSqlDatabase>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");//添加数据库驱动
db.setDatabaseName("mysql");//设置数据库名
db.setHostName("192.168.1.111");//设置主机名
db.setUserName("root");//设置用户名
db.setPassword("12345");//设置密码
bool ok = db.open();//连接
if(ok)
{
qDebug()<<"connect db success!";
}
mysql w;
w.show();
return a.exec();
}
特别提醒:QT += core gui sql(记得加上sql)
存储过程实例
工作中需建立存储过程,要求:保存一个月的数据,每天定点删除数据库中的数据。(从几年几月几日几点几分开始执行,每天执行一次, 删除以当天为基准的一个月前的数据)
⚠️必须要有//
-----------------------------------------------
|mysql>delimiter //
|mysql>create procedure mytest()
| ->begin
| ->delete from traffic_status wherestart_time<=date_sub(now(),interval 1 month);end;//
|Query OK, 0 rows affected (0.00 sec)
|
|mysql>delimiter ;
|mysql>set global event_scheduler=1;
|Query OK, 0 rows affected (0.00 sec)
|
|mysql>create event gogo on schedule every 1 day starts '2017-01-08 24:20:00' do call mytest();
|Query OK, 0 rows affected (0.00 sec)
|
|mysql>alter event gogo on completion preserve enable;
|Query OK, 0 rows affected (0.00 sec)
|
|------------------------------------------------------------------------------------------------------------
|-----------------------------------------------------------------------------------------
|mysql>delimiter //
|mysql>create procedure pro()
| ->begin
| ->delete from road_realtime_status where start_time<=date_sub(now(),interval 1 month);
| ->end;//
|Query OK, 0 rows affected (0.00 sec)
|
|mysql>delimiter ;
|mysql>set global event_scheduler=1;
|Query OK, 0 rows affected (0.00 sec)
|
|mysql>create event go on schedule every 1 day starts '2017-01-06 24:20:00' do call pro();
|Query OK, 0 rows affected (0.00 sec)
|
|mysql>alter event go on completion preserve enable;
|Query OK, 0 rows affected (0.00 sec)
|
|
|如果想关闭event事件
|alter event go on completion preserve disable;
|
|----------------------------------------------
扩展
N天内记录 :WHERE TO_DAYS(NOW()) - TO_DAYS(时间字段) <= N
今天的记录:where date(时间字段)=date(now())
或
where to_days(时间字段) = to_days(now());
查询一周:select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(column_time);
查询一个月:select * from table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(column_time);
CREATE EVENT test_event ON SCHEDULE EVERY 1 DAY STARTS '2012-09-24 00:00:00'
ON COMPLETION PRESERVE ENABLE DO CALL test_procedure();
EVERY 后面的是时间间隔,可以选 1 second,3 minute,5 hour,9 day,1 month,1 quarter(季度),1 year
从2013年1月13号0点开始,每天运行一次
ON SCHEDULE EVERY 1 DAY STARTS '2013-01-13 00:00:00'
从现在开始每隔九天定时执行
ON SCHEDULE EVERY 9 DAY STARTS NOW() ;
每个月的一号凌晨1 点执行
on schedule every 1 month starts date_add(date_add(date_sub(curdate(),interval day(curdate())-1 day),interval 1 month),interval 1 hour);
每个季度一号的凌晨1点执行
on schedule every 1 quarter starts date_add(date_add(date(concat(year(curdate()),'-',elt(quarter(curdate()),1,4,7,10),'-',1)),interval 1 quarter),interval 1 hour);
每年1月1号凌晨1点执行
on schedule every 1 quarter starts date_add(date_add(date(concat(year(curdate()),'-',elt(quarter(curdate()),1,4,7,10),'-',1)),interval 1 quarter),interval 1 hour);