简体   繁体   中英

connect c++ & Mysql

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 to QSqlDatabase::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 to QSqlDatabase::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 to qDebug(char const*, ...)' /home/boldt/src/workspace/tester/Debug/../src/tester.cpp:41: undefined reference to QSqlDatabase::~QSqlDatabase()' /home/boldt/src/workspac e/tester/Debug/../src/tester.cpp:43: undefined reference to QSqlDatabase::~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 to QSqlDatabase::~QSqlDatabase()' ./src/tester.o: In function QString::QString(char const*)': /usr/include/QtCore/qstring.h:419: undefined reference to QString::fromAscii_helper(char const*, int)' ./src/tester.o: In function QString::QString(QLatin1String const&)': /usr/include/QtCore/qstring.h:694: undefined reference to QString::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*)'

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM