Novo

Forum kao aplikacija na telefonu

Dodajte forum na početni ekran i koristite ga kao aplikaciju, sa menijem na dnu ekrana.

  • 42 odgovora
  • 11.503 pregleda

Najnovija ↓

Re: C++/QT i mysql opet 30.07.2010. u 10:34 · pre 197 meseci #42
moze li mi neko pomoci oko povlacena podataka iz MySql bazu u QT aplikaciju
i isto tako u slanju podataka u bazu kao i o samom nacinu realizacije Qt aplikacije i MySql baze

za sada imam smo konektor koji radi ... a sad treba da idem dalje ali nikako mi ne ide da povucem podatke ili da ih vratim u bazu

inace koristim Linux Ubuntu 10 sa MySql 5 i Qt4

Potpis
Miroslav Gojic - miroslavgojic.blogspot.com
Re: C++/QT i mysql opet 30.07.2010. u 16:26 · pre 197 meseci #43
Evo jednostavnog primera koji pravi tabele, puni ih i cita:
Code (cpp):

/*

mysql.cpp
---------

Compile with
     /usr/local/qt-4.6.3/bin/qmake -project -o mysql.pro
     /usr/local/qt-4.6.3/bin/qmake
     make

*/



#include <QtSql>
#include <iostream>
#include <cstdlib>

using std::cout;
using std::cerr;
using std::endl;


int main()
{
     QString sql;
     bool status;

     QSqlDatabase connection = QSqlDatabase::addDatabase("QMYSQL");
     connection.setHostName("localhost");
     connection.setPort(3306);
     connection.setDatabaseName("test");
     if (!connection.open())
     {
          cerr << "cannot connect to database!" << connection.lastError().text().toStdString() << endl;
          exit(EXIT_FAILURE);
     }

     QSqlQuery query(connection);

     status = query.exec("DROP TABLE IF EXISTS phone");
     if (!status)
          cerr << "cannot drop 'phone' table!" << endl;

     status = query.exec("DROP TABLE IF EXISTS person");
     if (!status)
          cerr << "cannot drop 'person' table!" << endl;

     sql =
          "CREATE TABLE person"
          "("
               "id INT AUTO_INCREMENT PRIMARY KEY, "
               "surname VARCHAR(32), "
               "name VARCHAR(32), "
               "nickname varchar(16)"
          ") "
          "ENGINE=InnoDB DEFAULT CHARSET=utf8";
     status = query.exec(sql);
     if (!status)
          cerr << "cannot create 'person' table!" << endl;

     sql =
          "CREATE TABLE phone"
          "("
               "id INT AUTO_INCREMENT PRIMARY KEY, "
               "person INT, "
               "phone_number VARCHAR(32) NOT NULL, "
               "FOREIGN KEY(person) REFERENCES person(id)"
          ") "
          "ENGINE=InnoDB";
     status = query.exec(sql);
     if (!status)
          cerr << "cannot create 'phone' table!" << endl;

     sql = "INSERT INTO person(surname, name, nickname) VALUES('Smith', 'Jonathan', 'Joe')";
     status = query.exec(sql);
     if (!status)
          cerr << "cannot insert into 'person' table!" << endl;

     sql = "INSERT INTO person(surname, name, nickname) VALUES('Duval', 'Jean-Luc', '')";
     status = query.exec(sql);
     if (!status)
          cerr << "cannot insert into 'person' table!" << endl;

     sql = QString("INSERT INTO person(surname, name, nickname) VALUES('Petrovi") + QChar(0x107) + QString("', 'Petar', NULL)");
     status = query.exec(sql);
     if (!status)
          cerr << "cannot insert into 'person' table!" << endl;

     sql = "SELECT surname, name, nickname FROM person";
     status = query.exec(sql);
     if (!status)
          cerr << "cannot select from 'person' table!" << endl;
     else
          while (query.next())
               cout << query.value(0).toString().toStdString() << "," << query.value(1).toString().toStdString() << "," <<
                    query.value(2).toString().toStdString() << endl;

     sql = "INSERT INTO phone(person, phone_number) VALUES(3, '38111987654')";
     status = query.exec(sql);
     if (!status)
          cerr << "cannot insert into 'phone' table!" << endl;

     sql = "SELECT person, phone_number FROM phone";
     status = query.exec(sql);
     if (!status)
          cerr << "cannot select from 'phone' table!" << endl;
     else
          while (query.next())
               cout << query.value(0).toString().toStdString() << "," << query.value(1).toString().toStdString() << "," << endl;

     return EXIT_SUCCESS;
}
 
Potpis
Sveti Avgustin: "Dobar hrišćanin treba da se kloni matematičara i svih onih koji daju lažna proročanstva. Postoji opasnost da su matematičari već sklopili pakt sa Đavolom, da pomrače čovekov um i da ga okuju okovima pakla."
Odgovori
Prijavite se da biste odgovorili u ovoj temi.PrijavaRegistracija

Premeštanje teme