#include <QtGui>#include <QtSql>#include <QMessageBox>static bool createConnection(){ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("addressbook"); db.setUserName("elton"); db.setHostName("epica"); db.setPassword("password"); // if( db.open()) { if(db.tables().isEmpty()) { QMessageBox::critical(0,QObject::tr("Database Error"), QString(db.lastError().text())); } if(db.tables().isEmpty()) {db.close();db.removeDatabase("addressbook");} return false; } return true;}// ----------------------------------------------------------------------int main(int argc, char** argv){ QApplication app(argc, argv); if (!createConnection()) { return -1; } /* QTableView view; QSqlTableModel model; model.setTable("addressbook"); model.select(); model.setEditStrategy(QSqlTableModel::OnFieldChange); view.setModel(&model); view.show();*/ return app.exec();}
#include <QtGui>#include <QtSql>#include <QMessageBox>static bool createConnection(){ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("addressbook"); db.setUserName("elton"); db.setHostName("epica"); db.setPassword("password"); if( db.isOpenError()) { QMessageBox::critical(0,QObject::tr("Database Error"), QString(db.lastError().text())); return false; } return true;}// ----------------------------------------------------------------------int main(int argc, char** argv){ QApplication app(argc, argv); if (!createConnection()) { return -1; } QTableView view; QSqlTableModel model; model.setTable("addressbook"); model.select();// model.setEditStrategy(QSqlTableModel::OnManualSubmit); model.setEditStrategy(QSqlTableModel::OnFieldChange); model.insertRow(0); model.setData(model.index(0,0),223213123); model.submitAll(); view.setModel(&model); view.show(); return app.exec();}
#include <QtGui>#include <QtSql>#include <QMessageBox>static bool createConnection(){ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("addressbook"); db.setUserName("elton"); db.setHostName("epica"); db.setPassword("password"); if( db.isOpenError()) { QMessageBox::critical(0,QObject::tr("Database Error"), QString(db.lastError().text())); return false; } return true;}// ----------------------------------------------------------------------int main(int argc, char** argv){ QApplication app(argc, argv); if (!createConnection()) { return -1; } QTableView view; QSqlTableModel model; model.setTable("addressbook"); model.select();// model.setEditStrategy(QSqlTableModel::OnManualSubmit); model.setEditStrategy(QSqlTableModel::OnFieldChange); model.insertRow(3); model.setData(model.index(0,0),223213123); model.submitAll(); view.setModel(&model); view.show(); return app.exec();}
QSqlQuery query; query.exec("create table person (id int primary key, " "firstname varchar(20), lastname varchar(20))"); query.exec("insert into person values(101, 'Danny', 'Young')"); query.exec("insert into person values(102, 'Christine', 'Holand')"); query.exec("insert into person values(103, 'Lars', 'Gordon')"); query.exec("insert into person values(104, 'Roberto', 'Robitaille')"); query.exec("insert into person values(105, 'Maria', 'Papadopoulos')");
#include <QtGui>#include <QtSql>static bool createConnection(){ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("addressbook"); db.setUserName("elton"); db.setHostName("epica"); db.setPassword("password"); if (!db.open()) { qDebug() << "Cannot open database:" << db.lastError(); return false; } return true;}int main(int argc, char** argv){ QApplication app(argc, argv); if (!createConnection()) { return -1; } QTableView view; QSqlTableModel model; model.setTable("addressbook"); model.select(); model.setEditStrategy(QSqlTableModel::OnFieldChange); //!!!!!!!!!!!!!!! model.submit();//!!!!!!!!!!!!!! view.setModel(&model); view.show(); return app.exec();