CREATE TABLE db_termin ( `ID` int(11) NOT NULL auto_increment, `FULL_NAME` text, `SHORT_NAME` text, `RAZDEL` int(11) default NULL, `P_RAZDEL` int(11) default NULL, `OPREDELENIE` longblob default NULL, PRIMARY KEY (`ID`));CREATE TABLE db_razdel ( `R_ID` int(11) NOT NULL auto_increment, `R_NAME` text, PRIMARY KEY (`R_ID`));
QSqlSelectCursor *q = new QSqlSelectCursor("select * from db_termin, db_razdel where db_termin.RAZDEL=db_razdel.R_ID"); QDataTable *t = new QDataTable(q, TRUE, &w); q->select(); t->refresh();
void main_form::filter_by_razdel(){ int id; mapRazdel( razdelCBox->currentText(), id, FALSE ); if(id){ terminTable->setFilter( "RAZDEL='" + QString::number(id) + "'" ); } terminTable->refresh();}
void main_form::polish(){ if ( terminTable ) { QSqlCursor* cursor = terminTable->sqlCursor(); if ( !cursor ) { cursor = new QSqlCursor( "db_termin", TRUE, db_razdelConnection ); if ( terminTable->isReadOnly() ) cursor->setMode( QSqlCursor::ReadOnly ); terminTable->setSqlCursor( cursor, FALSE, TRUE ); } if ( !cursor->isActive() ) terminTable->refresh( QDataTable::RefreshAll ); } QMainWindow::polish();}
QString fieldList = toString( d->nm ); if ( fieldList.isEmpty() ) return FALSE; QString str= "select " + fieldList; str += " from " + d->nm; if ( !filter.isEmpty() ) { d->ftr = filter; str += " where " + filter; } else d->ftr = QString::null; if ( sort.count() > 0 ) str += " order by " + sort.toString( d->nm ); d->srt = sort; return exec( str );
terminTable->setFilter( "db_termin.RAZDEL='" + QString::number(id) + "'" );
select * from db_termin, db_razdel where db_termin.RAZDEL=db_razdel.R_ID
terminTable->setFilter( "db_termin.RAZDEL=db_razdel.R_ID and db_termin.RAZDEL='" + QString::number(id) + "'" );
CREATE VIEW db_termin_razdelAS select * from db_termin inner join db_razdel on db_termin.RAZDEL=db_razdel.R_ID
QSqlSelectCursor *q = new QSqlSelectCursor("select * from db_termin_razdel");
QVariant QSqlCursor::calculateField ( const QString & name ) [virtual protected]
QVariant YourSqlSelectCursor::calculateField(const QString & name){ QVariant res = QVariant::Invalid; if(name=="R_NAME") { QSqlQuery query("SELECT R_NAME from db_razdel WHERE R_ID="+value("RAZDEL")); if(query.exec()) { res = query.value(0); } } return res;}
q.addField(QSqlFieldInfo("R_NAME")); q.setCalculated ( "R_NAME", true );
YourSqlSelectCursor *q = new YourSqlSelectCursor("select * from db_termin);
InvoiceItemCursor::InvoiceItemCursor() : QSqlCursor( "db_termin" ){ QSqlFieldInfo productName( "razdel_name", QVariant::String ); append( productName ); setCalculated( productName.name(), TRUE );}QVariant InvoiceItemCursor::calculateField( const QString & name ){ if ( name == "razdel_name" ) { QSqlQuery query( "SELECT R_NAME FROM db_razdel WHERE R_ID=" + field( "RAZDEL" )->value().toString() ); if ( query.next() ){ return query.value( 0 );} if(!query.isActive()){ query.lastError().showMessage();} } return QVariant( QString::null );}
class InvoiceItemCursor : public QSqlCursor{ public: InvoiceItemCursor(); protected: QVariant calculateField( const QString & name );};
InvoiceItemCursor invoiceItemCursor;terminTable->setSqlCursor(&invoiceItemCursor);terminTable->addColumn("FULL_NAME", "NAME");terminTable->addColumn( "razdel_name", "R_NAME" );