Qt

Aus Mein MediaWiki

(Unterschied zwischen Versionen)
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „ == MySQL == //--- use a QTextStream: it makes it easier to output --- //--- QString to cout. --- QTextStream cout(st…“)
K (MySQL)
Zeile 6: Zeile 6:
     //--- QString to cout.                                ---
     //--- QString to cout.                                ---
     QTextStream cout(stdout, QIODevice::WriteOnly);
     QTextStream cout(stdout, QIODevice::WriteOnly);
-
 
+
     //--- define the database connection ---
     //--- define the database connection ---
     QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
     QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
Zeile 13: Zeile 13:
     db.setUserName("qtuser");                // user
     db.setUserName("qtuser");                // user
     db.setPassword("qtuser");              // password
     db.setPassword("qtuser");              // password
-
 
+
     //--- attempt to open it ---
     //--- attempt to open it ---
     bool ok = db.open();
     bool ok = db.open();
-
 
+
     if ( ok ) {
     if ( ok ) {
         //--- we're good! ---
         //--- we're good! ---
Zeile 31: Zeile 31:
                 cout << QString( "%1\t%2\n").arg( Id).arg( word );
                 cout << QString( "%1\t%2\n").arg( Id).arg( word );
                 }
                 }
-
 
+
         //--- add a new entry to the table ---
         //--- add a new entry to the table ---
         query.prepare( "INSERT INTO address (fname, lname) VALUES ( :word, :word ) ");
         query.prepare( "INSERT INTO address (fname, lname) VALUES ( :word, :word ) ");
         query.bindValue( ":word", "Banana" );
         query.bindValue( ":word", "Banana" );
         query.exec();
         query.exec();
-
 
+
         //--- close connection to database
         //--- close connection to database
         db.close();
         db.close();

Version vom 21:04, 13. Okt. 2012


MySQL

   //--- use a QTextStream: it makes it easier to output ---
   //--- QString to cout.                                ---
   QTextStream cout(stdout, QIODevice::WriteOnly);

   //--- define the database connection ---
   QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
   db.setHostName("localhost");  // host
   db.setDatabaseName("qttest");            // database
   db.setUserName("qtuser");                // user
   db.setPassword("qtuser");               // password

   //--- attempt to open it ---
   bool ok = db.open();

   if ( ok ) {
       //--- we're good! ---
       cout << "Database open\n";
       //--- run a query and print data returned ---
       QSqlQuery query( "select * from address" );
       if ( !query.isActive() )
           cout << "Query Error" + query.lastError().text()
                << endl;
       else while (query.next()) {
               int Id = query.value(0).toInt();
               QString word = query.value(1).toString();
               cout << QString( "%1\t%2\n").arg( Id).arg( word );
               }

       //--- add a new entry to the table ---
       query.prepare( "INSERT INTO address (fname, lname) VALUES ( :word, :word ) ");
       query.bindValue( ":word", "Banana" );
       query.exec();

       //--- close connection to database
       db.close();
   }
   else
       //--- something went wrong ---
       cout << "Error opening database\n";
Quelle: http://cs.smith.edu/dftwiki/index.php/Qt4/Qt-Creator_Read_MySql_Table_%28Console_Mode%29

QT MySQL driver

Unter Ubuntu muss das Paket libqt4-sql-mysql mit sudo apt-get install libqt4-sql-mysql installiert werden.

Persönliche Werkzeuge