当前位置: 代码迷 >> QT开发 >> qt中怎么用变量动态写入数据库
  详细解决方案

qt中怎么用变量动态写入数据库

热度:78   发布时间:2016-04-25 04:49:18.0
qt中如何用变量动态写入数据库
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

query.exec("INSERT INTO weatherstation (date, time, "
  "temperature, humidity, altitude) "
  "VALUES ( da, ti, te, hu, al)");
qtcreator下, 变量da , ti ,te ,hu , al , 要写入数据库,而且每隔两秒会更新一次,,并再次写入


大家帮忙看看怎么能实现

------解决方案--------------------
query.prepare("INSERT INTO weatherstation (date, time,temperature, humidity, altitude) "
"VALUES ( :da, :ti, :te, :hu, :al)");
query.bindValue(":da",yourValue);
...
query.exce();

preapar,bindValue函数是执行字符串替换的

或是用QString的arg()函数,都是可以的

记得结贴啊
------解决方案--------------------
定时写入,就用Qtimer吧
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(2000);
然后在update里面写数据库
------解决方案--------------------
同意楼上
  相关解决方案