landonmkelsey
3rd September 2008, 19:12
Qt sql example sql QueryModel needs something...fails right off!
static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
// db.setDatabaseName(":memory:");
db.setDatabaseName("landonx");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
The code I got out of Qt 4 docs works:
#include <QtGui>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("landonx");
db.setUserName("landon4");
db.setPassword("141");
bool ok = db.open();
QSqlDatabase defaultDB = QSqlDatabase::database();
QSqlQuery query;
query.exec("SELECT * FROM log_book");
while (query.next()) {
int id = query.value(0).toInt();
QString fdate = query.value(1).toString();
QString actype = query.value(2).toString();
QString acid = query.value(3).toString();
double nlandings = query.value(4).toDouble();
double nhours = query.value(5).toDouble();
qDebug() << id <<"\t"<< fdate<<"\t" << actype<<"\t" << acid<<"\t" << nlandings<<"\t" << nhours;
}
db.close();
return app.exec();
}
figured it out!
Had to add:
db.setHostName("localhost");
db.setDatabaseName("landonx");
db.setUserName("landon4");
db.setPassword("Kateaux3141");
I see changing one "thing" doesn't fix all!
static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
// db.setDatabaseName(":memory:");
db.setDatabaseName("landonx");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
The code I got out of Qt 4 docs works:
#include <QtGui>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("landonx");
db.setUserName("landon4");
db.setPassword("141");
bool ok = db.open();
QSqlDatabase defaultDB = QSqlDatabase::database();
QSqlQuery query;
query.exec("SELECT * FROM log_book");
while (query.next()) {
int id = query.value(0).toInt();
QString fdate = query.value(1).toString();
QString actype = query.value(2).toString();
QString acid = query.value(3).toString();
double nlandings = query.value(4).toDouble();
double nhours = query.value(5).toDouble();
qDebug() << id <<"\t"<< fdate<<"\t" << actype<<"\t" << acid<<"\t" << nlandings<<"\t" << nhours;
}
db.close();
return app.exec();
}
figured it out!
Had to add:
db.setHostName("localhost");
db.setDatabaseName("landonx");
db.setUserName("landon4");
db.setPassword("Kateaux3141");
I see changing one "thing" doesn't fix all!