Reputation: 101
anyone can tell me what's wrong in my prg ? It say's me : With this prg i would view a table from MySQL.
This is my code :
int main(void)
{
//QString mydb = "mydb";
QSqlDatabase database = QSqlDatabase::addDatabase("mydb");
//Setup the database
database.setDatabaseName( "mydb" );
database.setUserName( "root" );
database.setPassword( "testpw" );
if ( !database.open() )
qDebug("Couldn't open DB");
}
but i have the errors :
and i dont know why, its my first c++ / mysql prg and i think i have forget anything to download or to include .
/home/boldt/src/workspace/tester/Debug/../src/tester.cpp:33: undefined reference to
QSqlDatabase::defaultConnection' /home/boldt/src/workspace/tester/Debug/../src/tester.cpp:33: undefined reference to
QSqlDatabase::addDatabase(QString const&, QString const&)' /home/boldt/src/workspace/tester/Debug/../src/tester.cpp:36: undefined reference toQSqlDatabase::setDatabaseName(QString const&)' /home/boldt/src/workspace/tester/Debug/../src/tester.cpp:37: undefined reference to
QSqlDatabase::setUserName(QString const&)' /home/boldt/src/workspace/tester/Debug/../src/tester.cpp:38: undefined reference toQSqlDatabase::setPassword(QString const&)' /home/boldt/src/workspace/tester/Debug/../src/tester.cpp:40: undefined reference to
QSqlDatabase::open()' /home/boldt/src/workspace/tester/Debug/../src/tester.cpp:41: undefined reference toqDebug(char const*, ...)' /home/boldt/src/workspace/tester/Debug/../src/tester.cpp:41: undefined reference to
QSqlDatabase::~QSqlDatabase()' /home/boldt/src/workspace/tester/Debug/../src/tester.cpp:43: undefined reference toQSqlDatabase::~QSqlDatabase()' /home/boldt/src/workspace/tester/Debug/../src/tester.cpp:43: undefined reference to
QSqlDatabase::~QSqlDatabase()' /home/boldt/src/workspace/tester/Debug/../src/tester.cpp:41: undefined reference toQSqlDatabase::~QSqlDatabase()' ./src/tester.o: In function
QString::QString(char const*)': /usr/include/QtCore/qstring.h:419: undefined reference toQString::fromAscii_helper(char const*, int)' ./src/tester.o: In function
QString::QString(QLatin1String const&)': /usr/include/QtCore/qstring.h:694: undefined reference toQString::fromLatin1_helper(char const*, int)' ./src/tester.o: In function
QString::~QString()': /usr/include/QtCore/qstring.h:880: undefined reference to `QString::free(QString::Data*)'
Upvotes: 1
Views: 243
Reputation: 2305
Here is some code I use for QT -> MySql. I personally try to stay completely clear of boost, QT is a much cleaner and complete solution.
Firstly, you'll want these includes:
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
Inside your project file, please add sql to your Qt variable:
QT += core sql
I believe this logic needs to run inside a Qt event loop. I recommend using a single shot timer. Qt offers many example programs that will get you started.
Open up the database. You don't need to store the database instance variable unless you need to connect to more than 1 database at a time. QT will keep an internal pointer for you.
QSqlDatabase database = QSqlDatabase::addDatabase( "QMYSQL" );
//Setup the database
database.setDatabaseName( db );
database.setUserName( usr );
database.setPassword( pass );
if ( !database.open() )
qDebug("Couldn't open DB");
Here is some code for reading data out of the DB. In this code I only take the first row returned. This could be changed by calling query->next() multiple times:
QString sql = QString("SELECT id, type, name FROM table WHERE id = %1").arg( id );
//Go through looking for my mac address
QSqlQuery query( sql);
if ( query.next() )
{
int id = query.value( 0 ).toInt();
int type = query.value( 1 ).toInt();
QString str = query.value( 2 ).toString();
return true;
}
Here is a function to insert bulk data. Note in many implementations there is a limit to the number of rows you can insert by a single execBatch() call. I don't know what the limit is, or how to find it. However <= 128 should be safe:
bool Database::storeTable( QString table, QStringList &field_list,
QHash<QString, QVariantList> &fields )
{
QString sql;
QStringList q_marks;
QSqlQuery query;
int i;
//Create my question marks
for ( i = 0; i < field_list.size(); i++ )
q_marks.append( "?" );
//Create my query
sql = QString::fromUtf8("INSERT INTO %1 ( %2 ) VALUES ( %3 )")
.arg( table)
.arg( field_list.join(','))
.arg( q_marks.join(','));
//Setup my query
if ( !query.prepare( sql ) )
{
qDebug("Couldn't prepare SQL store statement\r");
return false;
}
//Attach my values and finish
for ( i = 0; i < field_list.size(); i++ )
query.addBindValue( fields[ field_list[i]] );
//Setup my query lookup
bool result = query.execBatch();
if ( !result )
qDebug() << query.lastError().text();
return result;
}
Upvotes: 1
Reputation: 361
You need to set the include additional directories for boost library.
"Additional Include Directories"
C:\Users\user\Desktop\boost_1_53_0\boost_1_53_0
Here's a detailed explanation with examples http://www.boost.org/doc/libs/1_47_0/more/getting_started/windows.html#build-from-the-visual-studio-ide
Upvotes: 0