yeah sorry didnt see your earlier post thanks for explaining this...
I want to be able to archive my old table rows older than certain number of days, I figured I could copy the data from one database table to another database table, not sure if this is the best way to do it ? could you possible help me get a proper archive setup?
Qt Code:
void sqliteModel::dbConnect(){ if(!m_selectDataBase.isValid()){ qDebug() << "error in opening DB"; m_selectDataBase.setDatabaseName("/home/amet/userLog.db"); } else{ qDebug() <<"connected to DB" ; } m_selectDataBase.open(); } void sqliteModel::archiveDbConnect(){ if(!m_archiveDataBase.isValid()){ qDebug() << "error in opening DB"; m_archiveDataBase.setDatabaseName("/home/amet/userLogArchive.db"); } else{ qDebug() <<"connected to DB" ; } m_archiveDataBase.open(); } void sqliteModel::archiveEvent(){ dbConnect(); //---archive db connecion---// archiveDbConnect(); if(archiveDataQry.exec()){ qDebug()<<"sql statement exicuted fine"; } else{ qDebug() << "Errors accured with sql statement"; qDebug() << archiveTableQry.lastError(); } QDate rowDate; qDebug() << "deleteDate: "+archiveDate.toString(); while(archiveDataQry.next()){ rowDate = archiveDataQry.value(3).toDate(); qDebug() << "results: "+rowDate.toString(); if(archiveDate < rowDate){ qDebug() << "made inside delete condition: "; archiveDataQry.prepare("INSERT INTO userlogarchive FROM userlogevents2 WHERE dateTime < ?"); archiveDataQry.addBindValue(archiveDate); deleteDataQry.prepare("DELETE FROM userlogevents2 WHERE dateTime < ?"); deleteDataQry.addBindValue(archiveDate); if(archiveDataQry.exec()){ qDebug()<<"archive sql statement exicuted fine"; } else{ qDebug() << "Errors accured with archive sql statement"; qDebug() << archiveDataQry.lastError(); } if(deleteDataQry.exec()){ qDebug()<<"delete sql statement exicuted fine"; } else{ qDebug() << "Errors accured with delete sql statement"; qDebug() << deleteDataQry.lastError(); } } } m_selectDataBase.close(); }To copy to clipboard, switch view to plain text mode
Much easier IMHO to attach your archive database to the main database so that you can insert the records into the archive table by selecting the records you want to select for archiving. A hypothetical example would be:
Qt Code:
ATTACH DATABASE '/home/amet/userLogArchive.db' as arc; INSERT INTO arc.userlogarchive select * from main.userlogevents2 where dateTime >= ?; DELETE FROM main.userlogevents2 where dateTime >= ?;To copy to clipboard, switch view to plain text mode
I write the best type of code possible, code that I want to write, not code that someone tells me to write!
I thought keeping the table in another database would be best in case one corrupts, however I can attach the database together to copy over old rows? what is arc?
I write the best type of code possible, code that I want to write, not code that someone tells me to write!
I see what your saying... I just get little lost on implementing this concept, let me see what I can figure out with the attach database and the transaction()
Added after 51 minutes:
haven't looked into the ATTACH DATABASE yet been playing with using two separate connections little more but still having trouble... If I cant get this way to work I will try the attached database way...
Qt Code:
void sqliteModel::dbConnect(){ if(!m_selectDataBase.isValid()){ qDebug() << "error in opening DB"; m_selectDataBase.setDatabaseName("/home/amet/userLog.db"); } else{ qDebug() <<"connected to DB" ; } m_selectDataBase.open(); } void sqliteModel::archiveDbConnect(){ if(!m_archiveDataBase.isValid()){ qDebug() << "error in opening DB"; m_archiveDataBase.setDatabaseName("/home/amet/userLogArchive.db"); } else{ qDebug() <<"connected to DB" ; }To copy to clipboard, switch view to plain text mode
Qt Code:
void sqliteModel::archiveEvent(){ dbConnect(); archiveDbConnect(); int id; QString userName; QString eventMessage; QString dateTime; if(archiveDataQry.exec()){ qDebug()<<"sql statement exicuted fine"; } else{ qDebug() << "Errors accured with sql statement"; qDebug() << archiveDataQry.lastError(); } QDate rowDate; qDebug() << "archiveDate: "+archiveDate.toString(); m_selectDataBase.transaction(); copyDataQry.prepare("INSERT INTO userlogarchive (id, userName, eventMessage, dateTime) FROM userlogevents7 VALUES (:id, :userName, :eventMessage, :dateTime) WHERE dateTime < ?"); while(archiveDataQry.next()){ rowDate = archiveDataQry.value(3).toDate(); qDebug() << "results: "+rowDate.toString(); if(archiveDate < rowDate){ qDebug() << "made inside delete condition: "; id = archiveDataQry.record().value(0).toInt(); userName = archiveDataQry.record().value(1).toString(); eventMessage = archiveDataQry.record().value(2).toString(); dateTime = archiveDataQry.record().value(3).toString(); copyDataQry.bindValue(0, id); copyDataQry.bindValue(1, userName); copyDataQry.bindValue(2, eventMessage); copyDataQry.bindValue(3, dateTime); copyDataQry.addBindValue(archiveDate); } } //archiveDataQry.prepare("DELETE FROM userlogevents7 WHERE dateTime < ?"); //archiveDataQry.addBindValue(archiveDate); //if(archiveDataQry.exec()){ // qDebug()<<"archive sql statement exicuted fine"; //} //else{ // qDebug() << "Errors accured with archive sql statement"; // qDebug() << archiveDataQry.lastError(); //} if(copyDataQry.exec()){ qDebug()<<"copy sql statement exicuted fine"; } else{ qDebug() << "Errors accured with copy sql statement"; qDebug() << copyDataQry.lastError(); } m_selectDataBase.commit(); m_selectDataBase.close(); }To copy to clipboard, switch view to plain text mode
Last edited by jfinn88; 12th September 2016 at 21:34.
keep getting "Parameter count mismatch" seems to be with binding values I think...
Qt Code:
void sqliteModel::dbConnect(){ if(!m_selectDataBase.isValid()){ qDebug() << "error in opening DB"; m_selectDataBase.setDatabaseName("/home/amet/userLog.db"); } else{ qDebug() <<"connected to DB" ; } m_selectDataBase.open(); } void sqliteModel::archiveDbConnect(){ if(!m_archiveDataBase.isValid()){ qDebug() << "error in opening DB"; m_archiveDataBase.setDatabaseName("/home/amet/userLogArchive.db"); } else{ qDebug() <<"connected to DB" ; } m_archiveDataBase.open(); } void sqliteModel::archiveEvent(){ dbConnect(); archiveDbConnect(); QString id; QString userName; QString eventMessage; QDate dateTime; if(archiveDataQry.exec()){ qDebug()<<"sql statement exicuted fine"; } else{ qDebug() << "Errors accured with sql statement"; qDebug() << archiveDataQry.lastError(); } QDate rowDate; qDebug() << "archiveDate: "+archiveDate.toString("yyyy-MM-dd");; m_selectDataBase.transaction(); copyDataQry.prepare("INSERT INTO userlogarchive FROM userlogevents7 WHERE id > 0"); while(archiveDataQry.next()){ rowDate = archiveDataQry.value(3).toDate(); qDebug() << "results: "+rowDate.toString("yyyy-MM-dd"); //if(archiveDate < rowDate){ //qDebug() << "made inside delete condition: "; id = archiveDataQry.record().value(0).toString(); qDebug() << "id: "+id; userName = archiveDataQry.record().value(1).toString(); qDebug() << "user name: "+userName; eventMessage = archiveDataQry.record().value(2).toString(); qDebug() << "event Message : "+eventMessage; dateTime = archiveDataQry.record().value(3).toDate(); qDebug() << "date : "+dateTime.toString(); copyDataQry.bindValue(0,id); copyDataQry.bindValue(1, userName); copyDataQry.bindValue(2, eventMessage); copyDataQry.bindValue(3, dateTime); //copyDataQry.addBindValue(archiveDate); if(copyDataQry.exec()){ qDebug()<<"copy sql statement exicuted fine"; } else{ qDebug() << "Errors accured with copy sql statement"; qDebug() << copyDataQry.lastError(); } //archiveDataQry.prepare("DELETE FROM userlogevents7 WHERE dateTime < ?"); //archiveDataQry.addBindValue(archiveDate); //if(archiveDataQry.exec()){ // qDebug()<<"archive sql statement exicuted fine"; //} //else{ // qDebug() << "Errors accured with archive sql statement"; // qDebug() << archiveDataQry.lastError(); //} //} } m_selectDataBase.commit(); m_selectDataBase.close(); }To copy to clipboard, switch view to plain text mode
Qt Code:
{ Q_OBJECT public: ~sqliteModel(); enum userEventRoles {idRole= Qt::UserRole + 220, nameRole, msgRole, dateRole}; QHash<int, QByteArray> roleNames() const; Q_INVOKABLE void addEvent(const userEventLogMsg &msg); Q_INVOKABLE void dbConnect(); Q_INVOKABLE void archiveDbConnect(); public slots: void archiveEvent(); private: QList<userEventLogMsg> m_msgList; QSqlDatabase m_selectDataBase; QSqlDatabase m_archiveDataBase; QSqlQuery m_selectQuery; };To copy to clipboard, switch view to plain text mode
Added after 48 minutes:
going to try attaching database to existing connection
Qt Code:
void sqliteModel::archiveEvent(){ dbConnect(); //archiveDbConnect(); archiveDataQry.prepare("ATTACH DATABASE '/home/amet/userLogArchive.db' as db2"); archiveDataQry.exec(); if(archiveDataQry.exec()){ qDebug()<<"sql statement exicuted fine"; } else{ qDebug() << "Errors accured with sql statement"; qDebug() << archiveDataQry.lastError(); } m_selectDataBase.transaction(); archiveDataQry.prepare("INSERT INTO 'db2.usereventarchive' SELECT * FROM main.usereventlog7"); if(archiveDataQry.exec()){ qDebug()<<"copy sql statement exicuted fine"; } else{ qDebug() << "Errors accured with copy sql statement"; qDebug() << archiveDataQry.lastError(); } m_selectDataBase.commit(); m_selectDataBase.close(); }To copy to clipboard, switch view to plain text mode
Added after 7 minutes:
been trying to copy table contents from one database table to a separate database table but having issue with sql statement.
I have a database connection established already and I'm trying to attach another database to that connection but not sure If I have the attach sql script correct....
I get error "unable to fetch row", "No Qry"
Qt Code:
void sqliteModel::archiveEvent(){ dbConnect(); archiveDataQry.prepare("ATTACH DATABASE '/home/amet/userLogArchive.db' as db2"); if(archiveDataQry.exec()){ qDebug()<<"sql statement exicuted fine"; } else{ qDebug() << "Errors accured with sql statement"; qDebug() << archiveDataQry.lastError(); } m_selectDataBase.transaction(); archiveDataQry.prepare("INSERT INTO db2.usereventarchive SELECT * FROM main.usereventlog7"); if(archiveDataQry.exec()){ qDebug()<<"copy sql statement exicuted fine"; } else{ qDebug() << "Errors accured with copy sql statement"; qDebug() << archiveDataQry.lastError(); } m_selectDataBase.commit(); m_selectDataBase.close(); }To copy to clipboard, switch view to plain text mode
Last edited by jfinn88; 13th September 2016 at 00:25.
Not clear at all which line of code produces that output, so please be specific regarding the error you're receiving and which line of code produces it...
A couple of other general comments about your code. QSqlQuery::prepare returns a bool, don't ignore the return value and assume everything worked. You also seem to be changing your table names from example to example, so I have no clue if you're using the right table names from the main database, same for your archive database. Double check those names to ensure you are specifying tables that exist and have the expected number of columns, etc.
Edit: Other portions of your post don't make any sense, i.e. you are using QSqlQuery::bindValue when your SQL statement has no positional or named arguments???
Last edited by jefftee; 13th September 2016 at 02:24.
I write the best type of code possible, code that I want to write, not code that someone tells me to write!
jfinn88 (13th September 2016)
its the sql exec that throws this error in the if condition
Qt Code:
if(archiveDataQry.exec()){ qDebug()<<"copy sql statement exicuted fine"; } else{ qDebug() << "Errors accured with copy sql statement"; qDebug() << archiveDataQry.lastError(); }To copy to clipboard, switch view to plain text mode
I messed around with the tablenames have them set now how I want them. The two databases are: userLog.db with table usereventlog7 and userLogArchive.db with table usereventarchive and there is only four fields id, userName eventMessage, dateTime, both tables are set up the same data types. databases and names are now setup correctA couple of other general comments about your code. QSqlQuery::prepare returns a bool, don't ignore the return value and assume everything worked. You also seem to be changing your table names from example to example, so I have no clue if you're using the right table names from the main database, same for your archive database. Double check those names to ensure you are specifying tables that exist and have the expected number of columns, etc.
I was trying to use two different connections at first and using the first connections query select values and binding them with the second connections insert statement.... I see what your saying doesn’t make sense with the prepare statement I have in the post above. Below is the statement I meant to include with that snippet of code but wasn't workingEdit: Other portions of your post don't make any sense, i.e. you are using QSqlQuery::bindValue when your SQL statement has no positional or named arguments???
Qt Code:
void sqliteModel::archiveEvent(){ dbConnect(); archiveDbConnect(); QString id; QString userName; QString eventMessage; QDate dateTime; if(archiveDataQry.exec()){ qDebug()<<"sql statement exicuted fine"; } else{ qDebug() << "Errors accured with sql statement"; qDebug() << archiveDataQry.lastError(); } m_selectDataBase.transaction(); copyDataQry.prepare("INSERT INTO userLogArchive.usereventarchive (id, userName, eventMessage, dateTime) VALUES (:id, :userName, :eventMessage, :dateTime) FROM userLog.userlogevents7"); while(archiveDataQry.next()){ id = archiveDataQry.record().value(0).toString(); userName = archiveDataQry.record().value(1).toString(); eventMessage = archiveDataQry.record().value(2).toString(); dateTime = archiveDataQry.record().value(3).toDate(); copyDataQry.bindValue(0,id); copyDataQry.bindValue(1, userName); copyDataQry.bindValue(2, eventMessage); copyDataQry.bindValue(3, dateTime); if(copyDataQry.exec()){ qDebug()<<"copy sql statement exicuted fine"; } else{ qDebug() << "Errors accured with copy sql statement"; qDebug() << copyDataQry.lastError(); } } m_selectDataBase.commit(); m_selectDataBase.close(); }To copy to clipboard, switch view to plain text mode
I tried setting it up using ATTACH DATABASE sqlite cmd but cant get it to work either get error on exec() in if condition for insert statement unable to fetch row
Qt Code:
void sqliteModel::archiveEvent(){ dbConnect(); archiveDataQry.prepare("ATTACH DATABASE '/home/amet/userLogArchive.db' as db2"); if(archiveDataQry.exec()){ qDebug()<<"sql statement exicuted fine"; } else{ qDebug() << "Errors accured with sql statement"; qDebug() << archiveDataQry.lastError(); } m_selectDataBase.transaction(); archiveDataQry.prepare("INSERT INTO db2.usereventarchive SELECT * FROM main.usereventlog7"); if(archiveDataQry.exec()){ qDebug()<<"copy sql statement exicuted fine"; } else{ qDebug() << "Errors accured with copy sql statement"; qDebug() << archiveDataQry.lastError(); } m_selectDataBase.commit(); m_selectDataBase.close(); }To copy to clipboard, switch view to plain text mode
Added after 6 minutes:
update the issues seems to be in my prepare statement its not able to find my usereventlog7 table and not sure why......
Qt Code:
archiveDataQry.prepare("INSERT INTO db2.usereventarchive SELECT * FROM main.usereventlog7");To copy to clipboard, switch view to plain text mode
Added after 5 minutes:
update noticed simple typo fixed an works fine now thanks for suggesting checking bool value of prepare statement!
Added after 16 minutes:
update: Okay I got both ways working and found my mistakes in both of them where issue with prepare statement I think I will use the attach database way cleaner faster less code
===two connection=====
Qt Code:
dbConnect(); archiveDbConnect(); int id; QString userName; QString eventMessage; QDate dateTime; if(archiveDataQry.exec()){ qDebug()<<"sql statement exicuted fine"; } else{ qDebug() << "Errors accured with sql statement"; qDebug() << archiveDataQry.lastError(); } m_selectDataBase.transaction(); if(copyDataQry.prepare("INSERT INTO usereventarchive (id, userName, eventMessage, dateTime) VALUES (:id, :userName, :eventMessage, :dateTime)")) { qDebug()<<"prepare sql statement exicuted fine"; } else{ qDebug() << "Errors accured with prepare sql statement"; qDebug() << copyDataQry.lastError(); } while(archiveDataQry.next()){ id = archiveDataQry.record().value(0).toInt(); userName = archiveDataQry.record().value(1).toString(); eventMessage = archiveDataQry.record().value(2).toString(); dateTime = archiveDataQry.record().value(3).toDate(); copyDataQry.bindValue(0,id); copyDataQry.bindValue(1, userName); copyDataQry.bindValue(2, eventMessage); copyDataQry.bindValue(3, dateTime); if(copyDataQry.exec()){ qDebug()<<"copy sql statement exicuted fine"; } else{ qDebug() << "Errors accured with copy sql statement"; qDebug() << copyDataQry.lastError(); } } m_selectDataBase.commit(); m_selectDataBase.close(); }To copy to clipboard, switch view to plain text mode
====ATTACH DATABASE====
Qt Code:
void sqliteModel::archiveEvent(){ //---connect to DB---// dbConnect(); //---attach archive DB to connection---// archiveDataQry.prepare("ATTACH DATABASE '/home/amet/userLogArchive.db' as db2"); //---execute attach DB---// if(archiveDataQry.exec()){ qDebug()<<"sql statement exicuted fine"; } else{ qDebug() << "Errors accured with sql statement"; qDebug() << archiveDataQry.lastError(); } //---prepare sql copy---// if(archiveDataQry.prepare("INSERT INTO db2.usereventarchive SELECT * FROM main.userlogevents7")){ qDebug()<<"prepare sql statement exicuted fine"; } else{ qDebug() << "Errors accured with prepare sql statement"; qDebug() << archiveDataQry.lastError(); } //---execute sql copy---// if(archiveDataQry.exec()){ qDebug()<<"copy sql statement exicuted fine"; } else{ qDebug() << "Errors accured with copy sql statement"; qDebug() << archiveDataQry.lastError(); } //---commit & close---// m_selectDataBase.commit(); m_selectDataBase.close(); }To copy to clipboard, switch view to plain text mode
Last edited by jfinn88; 13th September 2016 at 17:21.
update: here is my archive function works okay so far...
Copies rows from one database table that are older than 30 days and stores them in another database table for archiving.
Qt Code:
void sqliteModel::archiveEvent(){ //---connect to DB---// dbConnect(); //---attach archive DB to connection---// archiveDataQry.prepare("ATTACH DATABASE '/home/amet/userLogArchive.db' as db2"); //---execute attach DB---// if(archiveDataQry.exec()){ qDebug()<<"sql statement exicuted fine"; } else{ qDebug() << "Errors accured with sql statement"; qDebug() << archiveDataQry.lastError(); } m_selectDataBase.transaction(); //---get archive date vlaue--// QString dateStr; //QDate::fromString(archiveDate, "yyyy-MM-dd"); qDebug() << "archiveDate: "+archiveDate.toString("yyyy-MM-dd"); bool prepareSqlBool; prepareSqlBool = archiveDataQry.prepare("INSERT INTO db2.usereventarchive SELECT * FROM main.userlogevents7 WHERE dateTime < ?"); archiveDataQry.addBindValue(archiveDate); //---prepare sql copy---// if(prepareSqlBool){ qDebug()<<"prepare sql statement exicuted fine"; } else{ qDebug() << "Errors accured with prepare sql statement"; qDebug() << archiveDataQry.lastError(); } //---execute sql copy---// if(archiveDataQry.exec()){ qDebug()<<"copy sql statement exicuted fine"; } else{ qDebug() << "Errors accured with copy sql statement"; qDebug() << archiveDataQry.lastError(); } //---delete old rows---// while(dateQry.next()) { dateStr = dateQry.value(3).toString(); //qDebug() << "results: "+rowDate.toString(); if(rowDate < archiveDate){ qWarning() << rowDate; qDebug() << archiveDate; qDebug() << "made inside delete condition: "; archiveDataQry.prepare("DELETE FROM userlogevents7 WHERE dateTime < ?"); archiveDataQry.addBindValue(archiveDate); //---execute delete---// if(archiveDataQry.exec()){ qDebug()<<"delete sql statement exicuted fine"; } else{ qDebug() << "Errors accured with delete sql statement"; qDebug() << archiveDataQry.lastError(); } } else{ qDebug() << "no old records to delte"; } } //---commit & close---// m_selectDataBase.commit(); m_selectDataBase.close(); }To copy to clipboard, switch view to plain text mode
having issue hiding and showing my calendar objects using if condition inside a mouseArea onclicked I set the calendar visibility to false and set it true onClciked however i cant get it to hide after showing.....
update: okay never mind just tried it again seems to be working able to show and hide calendar based of visible property and if condition
Qt Code:
ColumnLayout { id: calendarColumnLayout x: 8 y: 54 width: 259 height: 540 Rectangle { id: calendarRect1 width: 247 height: 247 anchors.top: parent.top visible: false Calendar { id: calendar1 width: 247 height: 247 anchors.rightMargin: -11 anchors.bottomMargin: -8 anchors.leftMargin: 2 anchors.topMargin: 8 anchors.fill: parent anchors.top: parent.top style: CalendarStyle { dayDelegate: Item { Rectangle { id: rect1 anchors.fill: parent Label { id: dayDelegateText1 text: styleData.date.getDate() anchors.centerIn: parent horizontalAlignment: Text.AlignRight font.pixelSize: Math.min(parent.height/3, parent.width/3) color: styleData.selected ? "red" : "black" font.bold: styleData.selected } MouseArea { anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter width: styleData.selected ? parent.width / 2 : 0 height: styleData.selected ? parent.height / 2 : 0 Rectangle { anchors.fill: parent color: "transparent" border.color: "darkorange" } } } } } } } Rectangle { id: calendarRect2 x: 14 y: 350 width: 247 height: 247 anchors.bottomMargin: -11 anchors.bottom: parent.bottom visible: false Calendar { id: calendar2 width: 247 height: 247 anchors.rightMargin: -5 anchors.bottomMargin: 28 anchors.leftMargin: -5 anchors.topMargin: -28 anchors.fill: parent anchors.bottom: parent.bottom style: CalendarStyle { dayDelegate: Item { Rectangle { id: rect2 anchors.fill: parent Label { id: dayDelegateText2 text: styleData.date.getDate() anchors.centerIn: parent horizontalAlignment: Text.AlignRight font.pixelSize: Math.min(parent.height/3, parent.width/3) color: styleData.selected ? "red" : "black" font.bold: styleData.selected } MouseArea { anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter width: styleData.selected ? parent.width / 2 : 0 height: styleData.selected ? parent.height / 2 : 0 Rectangle { anchors.fill: parent color: "transparent" border.color: "darkorange" } onClicked: { //---emit the submitBegnDate signal---// //sqliteModel.searchDateRange(); } } } } } } } } Label { id: calendarLabel x: 73 y: 8 text: qsTr("Select Date Range") } Label { id: endDatelabel x: 104 y: 309 text: qsTr("End Date") MouseArea { id: endDateLabelMouseArea anchors.fill: parent onClicked: { if(calendarRect2.visible == false) { calendarRect2.visible = true } else { calendarRect2.visible = false } } } } Label { id: beginDatelabel x: 97 y: 31 text: qsTr("Begin Date") MouseArea { id: beginDateLabelMouseArea anchors.fill: parent onClicked: { if(calendarRect1.visible == false) { calendarRect1.visible = true } else { calendarRect1.visible = false } } } }To copy to clipboard, switch view to plain text mode
Last edited by jfinn88; 14th September 2016 at 00:23.
You should abort/rollback the transaction if you have an error with the insert for example, you would not want to continue and delete those records from the source table if they were not properly inserted into the archive table. While you do test success/failure you are only issuing success/failure messages but continue to fall through code to the next step, etc.
I write the best type of code possible, code that I want to write, not code that someone tells me to write!
Thanks for bringing this up, I noticed this as well just a little bit ago and I'm using an if condition to check to make sure the insert sql statement gets executed correctly so it only executes delete if copy goes threw... here is updated code not sure if its the best way but it seem to be working fine wont delete tell copy is complete... I'll have to look into "abort/rollback" I have never done this before... I'm guessing you would jsut call the function in the else part of the clauseQt Code:
m_selectDataBase.rollBack();To copy to clipboard, switch view to plain text mode
Qt Code:
void UserEventLog::archiveEvent(){ //---connect to DB---// dbConnect(); //---attach archive DB to connection---// attachDbQry.prepare("ATTACH DATABASE '/home/amet/git/rnd/userLogArchive.db' as db2"); //---execute attach DB---// if(attachDbQry.exec()){ qDebug()<<"sql statement exicuted fine"; } else{ qDebug() << "Errors accured with sql statement"; qDebug() << attachDbQry.lastError(); } //---start DB transaction---// m_selectDataBase.transaction(); //---get archive date vlaue--// //---get currentDate minus 30days---// QString dateStr; qDebug() << "archiveDate: "+archiveDate.toString("yyyy-MM-dd"); //---copy from one Db table to another---// bool prepareSqlBool; prepareSqlBool = copyDataQry.prepare("INSERT INTO db2.userlogarchive (userName, eventMessage, dateTime) SELECT userName, eventMessage, dateTime FROM main.userlogevents WHERE dateTime < ?"); copyDataQry.addBindValue(archiveDate); //---prepare sql copy---// if(prepareSqlBool){ qDebug()<<"prepare sql statement exicuted fine"; } else{ qDebug() << "Errors accured with prepare sql statement"; qDebug() << copyDataQry.lastError(); } bool copySqlBool; copySqlBool = copyDataQry.exec(); //---execute sql copy---// if(copySqlBool){ qDebug()<<"copy sql statement exicuted fine"; } else{ qDebug() << "Errors accured with copy sql statement"; qDebug() << copyDataQry.lastError(); } QDate rowDate; //---Copy rows to archive then delete old rows---// while(dateQry.next()){ //---Get date value from dateQry---// dateStr = dateQry.value(3).toString(); //---bool sql copy---// if(copySqlBool){ //qDebug()<<"copy sql statement exicuted fine"; //---Executes only if copy qry executes---// if(rowDate < archiveDate){ //qWarning() << rowDate; //qDebug() << archiveDate; //qDebug() << "made inside delete condition: "; //---Sql delete statement---// archiveDataQry.prepare("DELETE FROM userlogevents WHERE dateTime < ?"); archiveDataQry.addBindValue(archiveDate); //---execute delete---// if(archiveDataQry.exec()){ qDebug()<<"delete sql statement exicuted fine"; } else{ qDebug() << "Errors accured with delete sql statement"; qDebug() << archiveDataQry.lastError(); } } else{ //qDebug() << "no old records to delte"; //m_selectDataBase.rollBack(); } } else{ qDebug() << "Errors accured with copy sql statement"; qDebug() << archiveDataQry.lastError(); } } //---commit transaction & close---// m_selectDataBase.commit(); m_selectDataBase.close(); }To copy to clipboard, switch view to plain text mode
Last edited by jfinn88; 14th September 2016 at 23:33.
I read the whole story and the question arises: is SQLite mandatory or you can use another database ?
I have been asked to do this in Sqlite3 I believe its used in another part of the project. I believe we pick sqlite for this project because it gets embedded inside the application, and do to its speed and efficiency is a better choice and we dont need different user to access the DB. This has been a good project for me because I don’t have much experience with C++ and have never used Qt frame work, IDE or QML
Added after 1 26 minutes:
I want to emit a mesageBox if any errors our thrown but messageBox is not showing up... calling it in my c++ fcn
===userEventLog.h====
Qt Code:
signals:To copy to clipboard, switch view to plain text mode
and then call it like this but when I run the program messageBox wont display.... is there a stack setting needs to be set to 1 or something to put it on top level ?
====userEventLog.cpp====
Qt Code:
To copy to clipboard, switch view to plain text mode
need to connect signal in qml I tried
====UserEventDialog===
Qt Code:
signal alertMsg(int icon, string title, string msg_text) //---Get error here---// onAlertMsg: { msgDialog.text = msg_text msgDialog.title = title msgDialog.icon = icon msgDialog.open() } Connections{ target: UserEventLog onAlertMsg:{ alertDialog.msg_text = msg_text alertDialog.title = title alertDialog.icon = icon viewRect.state = "ALERT" } } states: [ State { name: "ALERT" PropertyChanges {target: userevent_item; enabled: false; opacity: .7} PropertyChanges {target: alertDialog; visible: true} } ] AlertDialog{ id: alertDialog anchors.verticalCenterOffset: -75 anchors.horizontalCenterOffset: -50 title: "Alert" msg_text: "" icon: 1 onClose:{ viewRect.state = "" } }To copy to clipboard, switch view to plain text mode
Last edited by jfinn88; 16th September 2016 at 00:41.
update: cleaned up qml code was getting confused on signal based off example I was using had dead code in it ...
====qml==========
Qt Code:
Connections{ target: UserEventLog onAlertDbMsg:{ alertDialog.msg_text = msg_text alertDialog.title = title alertDialog.icon = icon view_rect.state = "ALERT" console.log("made it inside alert"); } } states: [ State { name: "ALERT" PropertyChanges {target: view_rect; enabled: false; opacity: .7;} PropertyChanges {target: alertDialog; visible: true;} } ] AlertDialog{ id: alertDialog anchors.verticalCenterOffset: -75 anchors.horizontalCenterOffset: -50 title: "Alert" msg_text: "" icon: 1 onClose:{ view_rect.state = "" } }To copy to clipboard, switch view to plain text mode
=====c++=====
Qt Code:
emit alertDbMsg(QMessageBox::Warning, "Database Error Message", "Error pulling data from database...");To copy to clipboard, switch view to plain text mode
=====header file=====
Qt Code:
signals:To copy to clipboard, switch view to plain text mode
not able to get a border around my table view ?
Qt Code:
Rectangle { id: tableViewRect width: 880 height: 490 anchors.verticalCenter: parent.verticalCenter anchors.left: parent.left anchors.leftMargin: 10 border.width: 10 border.color: "lightsteelblue" color: "transparent" TableView { id: tableView anchors.fill: parent sortIndicatorVisible: true sortIndicatorOrder: 1 sortIndicatorColumn: 1 frameVisible: true model: UserEventLog style: TableViewStyle { // frame: Rectangle { // border.color: "lightsteelblue" // border.width: 50 // } headerDelegate: Rectangle { height: textItem.implicitHeight * 1.2 width: textItem.implicitWidth color: "lightsteelblue" Text { id: textItem anchors.centerIn: parent text: styleData.value } Rectangle { anchors.right: parent.right anchors.top: parent.top anchors.bottom: parent.bottom width: 3 color: "yellow" } } } TableViewColumn { role: "id" title: "id" width: 100 } TableViewColumn { role: "userName" title: "User Name" width: 200 } TableViewColumn { role: "eventMessage" title: "Event Message" width: 372 } TableViewColumn { role: "dateTime" title: "Date Time" width: 201 } } }To copy to clipboard, switch view to plain text mode
found a solution: just messed around with my outer object rectangle and anchoring got a border around my tableView now! I made the rectangle little larger than tableView then enter tableView in the Rect.
Qt Code:
Rectangle { width: 880 height: 490 anchors.verticalCenter: parent.verticalCenter anchors.left: parent.left anchors.leftMargin: 10 color: "transparent" radius: 7 border.color: "lightsteelblue" border.width: 5 visible: true TableView { id: tableView width: 870 height: 480 anchors.centerIn: parent sortIndicatorVisible: true sortIndicatorOrder: 1 sortIndicatorColumn: 1 //frameVisible: true model: UserEventLog style: TableViewStyle { headerDelegate: Rectangle { height: textItem.implicitHeight * 1.2 width: textItem.implicitWidth color: "lightsteelblue" Text { id: textItem anchors.centerIn: parent text: styleData.value } Rectangle { anchors.right: parent.right anchors.top: parent.top anchors.bottom: parent.bottom width: 3 color: "yellow" } } } TableViewColumn { role: "id" title: "id" width: 100 } TableViewColumn { role: "userName" title: "User Name" width: 200 } TableViewColumn { role: "eventMessage" title: "Event Message" width: 372 } TableViewColumn { role: "dateTime" title: "Date Time" width: 201 } } }To copy to clipboard, switch view to plain text mode
You may have better success starting new threads for different subjects.
I write the best type of code possible, code that I want to write, not code that someone tells me to write!
Bookmarks