having issue searching records in Database after incorporating time component into database...
QML search condition
//---Action for Search Button---//
Action {
id: action_search
enabled:!inSequence
onTriggered:{
/* Condition statement to search by:
* userName,
* singleDate,
* dateRange using textFields,
* userName & signleDate,
* userName & dateRange using TextFieldStyle
*/
//---Hides Calendars on Search---//
calendarRect1.visible = false
calendarRect2.visible = false
searchBtn.focus = false;
endDateTextField.focus = false;
//---Search by userName---/
if(beginDateTextField.text === "" && endDateTextField === "" && userNameDropDown.currentText !== ""){
UserEventLog.searchUserName(userNameDropDown.currentText);
console.log("Search by userName: ", userNameDropDown.currentText)
}
//---Search by signle date: beginDate OR endDate---//
else if(beginDateTextField.text !== "" && endDateTextField.text === "" && userNameDropDown.currentText === "" ||
beginDateTextField.text === "" && endDateTextField.text !=="" && userNameDropDown.currentText ===""){
console.log("Search by signle date beginDate: ", beginDateTextField.text.toString());
if(beginDateTextField.text !== "" && endDateTextField.text === ""){
UserEventLog.searchDate(beginDateTextField.text.toString("yyyy-MM-dd"));
}
else if(beginDateTextField.text === "" && endDateTextField.text !== ""){
UserEventLog.searchDate(endDateTextField.text.toString("yyyy-MM-dd"));
}
}
//---Search by beginDate & endDate text fields---//
else if(beginDateTextField.text !== "" && endDateTextField.text !== "" && userNameDropDown.currentText === ""){
console.log("Search by beginDate & endDate text fields: ", endDateTextField.text.toString());
console.log("Search by beginDate & endDate text fields: ", beginDateTextField.text.toString());
UserEventLog.searchDateRange(beginDateTextField.text.toString("yyyy-MM-dd"), endDateTextField.text.toString("yyyy-MM-dd"));
}
//---Search by userName & Single date (beginDate OR endDate)---//
else if(beginDateTextField.text !== "" && endDateTextField.text ==="" && userNameDropDown.currentText !== "" ||
beginDateTextField.text == "" && endDateTextField.text !=="" && userNameDropDown.currentText !== "" ){
console.log("Search by userName & beginDate text field: ", userNameDropDown.currentText);
console.log("Search by userName & beginDate text field: ", Qt.formatDateTime(beginDateTextField.text, "yyyy-MM-dd hh:mm:ss"));
if(beginDateTextField.text !== "" && endDateTextField.text === ""){
UserEventLog.searchUserNameDateText(userNameDropDown.currentText, beginDateTextField.text);
//UserEventLog.searchUserNameDateText(userNameDropDown.currentText, Qt.formatDateTime(beginDateTextField.text, "yyyy-MM-dd hh:mm:ss"));
}
else if(beginDateTextField.text === "" && endDateTextField.text !== ""){
UserEventLog.searchUserNameDateText(userNameDropDown.currentText, endDateTextField.text);
//UserEventLog.searchUserNameDateText(userNameDropDown.currentText, Qt.formatDateTime(beginDateTextField.text, "yyyy-MM-dd hh:mm:ss"));
}
}
//---Search by userName, & beginDate, endDate text fields---//
else if(beginDateTextField.text !== "" && endDateTextField.text !== "" && userNameDropDown.currentText !== ""){
console.log("Search by userName, & beginDate, endDate text fields: ", userNameDropDown.currentText);
console.log("Search by userName, & beginDate, endDate text fields: ", beginDateTextField.text.toString());
console.log("Search by userName, & beginDate, endDate text fields: ", endDateTextField.text.toString());
UserEventLog.searchDateRange(userNameDropDown.currentText, beginDateTextField.text, endDateTextField.text);
}
//---if search fails---//
else{
console.log("enter in date range or search by user name or both!");
}
//---Prepare listView: set focus, set index, set position---//
listView.forceActiveFocus();
listView.currentIndex = 0;
listView.positionViewAtBeginning();
}
}
//---Action for Search Button---//
Action {
id: action_search
enabled:!inSequence
onTriggered:{
/* Condition statement to search by:
* userName,
* singleDate,
* dateRange using textFields,
* userName & signleDate,
* userName & dateRange using TextFieldStyle
*/
//---Hides Calendars on Search---//
calendarRect1.visible = false
calendarRect2.visible = false
searchBtn.focus = false;
endDateTextField.focus = false;
//---Search by userName---/
if(beginDateTextField.text === "" && endDateTextField === "" && userNameDropDown.currentText !== ""){
UserEventLog.searchUserName(userNameDropDown.currentText);
console.log("Search by userName: ", userNameDropDown.currentText)
}
//---Search by signle date: beginDate OR endDate---//
else if(beginDateTextField.text !== "" && endDateTextField.text === "" && userNameDropDown.currentText === "" ||
beginDateTextField.text === "" && endDateTextField.text !=="" && userNameDropDown.currentText ===""){
console.log("Search by signle date beginDate: ", beginDateTextField.text.toString());
if(beginDateTextField.text !== "" && endDateTextField.text === ""){
UserEventLog.searchDate(beginDateTextField.text.toString("yyyy-MM-dd"));
}
else if(beginDateTextField.text === "" && endDateTextField.text !== ""){
UserEventLog.searchDate(endDateTextField.text.toString("yyyy-MM-dd"));
}
}
//---Search by beginDate & endDate text fields---//
else if(beginDateTextField.text !== "" && endDateTextField.text !== "" && userNameDropDown.currentText === ""){
console.log("Search by beginDate & endDate text fields: ", endDateTextField.text.toString());
console.log("Search by beginDate & endDate text fields: ", beginDateTextField.text.toString());
UserEventLog.searchDateRange(beginDateTextField.text.toString("yyyy-MM-dd"), endDateTextField.text.toString("yyyy-MM-dd"));
}
//---Search by userName & Single date (beginDate OR endDate)---//
else if(beginDateTextField.text !== "" && endDateTextField.text ==="" && userNameDropDown.currentText !== "" ||
beginDateTextField.text == "" && endDateTextField.text !=="" && userNameDropDown.currentText !== "" ){
console.log("Search by userName & beginDate text field: ", userNameDropDown.currentText);
console.log("Search by userName & beginDate text field: ", Qt.formatDateTime(beginDateTextField.text, "yyyy-MM-dd hh:mm:ss"));
if(beginDateTextField.text !== "" && endDateTextField.text === ""){
UserEventLog.searchUserNameDateText(userNameDropDown.currentText, beginDateTextField.text);
//UserEventLog.searchUserNameDateText(userNameDropDown.currentText, Qt.formatDateTime(beginDateTextField.text, "yyyy-MM-dd hh:mm:ss"));
}
else if(beginDateTextField.text === "" && endDateTextField.text !== ""){
UserEventLog.searchUserNameDateText(userNameDropDown.currentText, endDateTextField.text);
//UserEventLog.searchUserNameDateText(userNameDropDown.currentText, Qt.formatDateTime(beginDateTextField.text, "yyyy-MM-dd hh:mm:ss"));
}
}
//---Search by userName, & beginDate, endDate text fields---//
else if(beginDateTextField.text !== "" && endDateTextField.text !== "" && userNameDropDown.currentText !== ""){
console.log("Search by userName, & beginDate, endDate text fields: ", userNameDropDown.currentText);
console.log("Search by userName, & beginDate, endDate text fields: ", beginDateTextField.text.toString());
console.log("Search by userName, & beginDate, endDate text fields: ", endDateTextField.text.toString());
UserEventLog.searchDateRange(userNameDropDown.currentText, beginDateTextField.text, endDateTextField.text);
}
//---if search fails---//
else{
console.log("enter in date range or search by user name or both!");
}
//---Prepare listView: set focus, set index, set position---//
listView.forceActiveFocus();
listView.currentIndex = 0;
listView.positionViewAtBeginning();
}
}
To copy to clipboard, switch view to plain text mode
c++ function that performs sql script
update: updated if condition in fcn
bool UserEventLog
::searchUserNameDateText(QString userNameText,
QDate dateText
){ qDebug() << "UserEventLog::searchUserNameDateText() User Name: " << userNameText;
qDebug() << "UserEventLog::searchUserNameDateText() Date: " << dateText.toString();
QString begin
= dateText.
toString("yyyy-MM-dd 00:00:00");
QString end
= dateText.
toString("yyyy-MM-dd 23:59:59");
qDebug() << "UserEventLog::searchUserNameDateText() beginDate: " << begin;
qDebug() << "UserEventLog::searchUserNameDateText() endDate: " << end;
dbConnect();
emit showBusy(true);
QString selectQueryString
= "SELECT id, userName, eventMessage, dateTime FROM userlogevents WHERE userName = ? AND dateTime BETWEEN ? and ?";
selectQuery.addBindValue(userNameText);
selectQuery.addBindValue(begin);
selectQuery.addBindValue(end);
bool prepareSqlBool = selectQuery.prepare(selectQueryString);
bool selectSqlBool;
if(prepareSqlBool){
selectSqlBool = selectQuery.exec();
if(selectSqlBool){
qDebug() << "UserEventLog::searchUserNameDateText() searchUserNameDateText query executed fine!";
}
else{
return selectSqlBool;
}
}
else{
emit xmui
->alertMsg
(QMessageBox::Warning,
"Database Error Message 9",
"Error: searching database..."+selectQuery.
lastError().
text());
qDebug() << "UserEventLog::searchUserNameDateText() searchUserNameDateText query dind't execute";
return prepareSqlBool;
}
userEventLogMsg msg;
beginResetModel();
m_userEventList.clear();
while (selectQuery.next()){
msg.id = selectQuery.value(0).toString();
msg.username = selectQuery.value(1).toString();
msg.eventmessage = selectQuery.value(2).toString();
msg.datetime = selectQuery.value(3).toDateTime();
addEvent(msg);
}
endResetModel();
m_selectDataBase.close();
emit showBusy(false);
return selectSqlBool;
}
bool UserEventLog::searchUserNameDateText(QString userNameText, QDate dateText){
qDebug() << "UserEventLog::searchUserNameDateText() User Name: " << userNameText;
qDebug() << "UserEventLog::searchUserNameDateText() Date: " << dateText.toString();
QString begin = dateText.toString("yyyy-MM-dd 00:00:00");
QString end = dateText.toString("yyyy-MM-dd 23:59:59");
qDebug() << "UserEventLog::searchUserNameDateText() beginDate: " << begin;
qDebug() << "UserEventLog::searchUserNameDateText() endDate: " << end;
dbConnect();
emit showBusy(true);
QSqlQuery selectQuery(m_selectDataBase);
QString selectQueryString = "SELECT id, userName, eventMessage, dateTime FROM userlogevents WHERE userName = ? AND dateTime BETWEEN ? and ?";
selectQuery.addBindValue(userNameText);
selectQuery.addBindValue(begin);
selectQuery.addBindValue(end);
bool prepareSqlBool = selectQuery.prepare(selectQueryString);
bool selectSqlBool;
if(prepareSqlBool){
selectSqlBool = selectQuery.exec();
if(selectSqlBool){
qDebug() << "UserEventLog::searchUserNameDateText() searchUserNameDateText query executed fine!";
}
else{
return selectSqlBool;
}
}
else{
emit xmui->alertMsg(QMessageBox::Warning, "Database Error Message 9", "Error: searching database..."+selectQuery.lastError().text());
qDebug() << "UserEventLog::searchUserNameDateText() searchUserNameDateText query dind't execute";
return prepareSqlBool;
}
userEventLogMsg msg;
beginResetModel();
m_userEventList.clear();
while (selectQuery.next()){
msg.id = selectQuery.value(0).toString();
msg.username = selectQuery.value(1).toString();
msg.eventmessage = selectQuery.value(2).toString();
msg.datetime = selectQuery.value(3).toDateTime();
addEvent(msg);
}
endResetModel();
m_selectDataBase.close();
emit showBusy(false);
return selectSqlBool;
}
To copy to clipboard, switch view to plain text mode
update: query used to push data into database
m_insertQuery.prepare("INSERT INTO userlogevents (userName, eventMessage, dateTime) VALUES(:userName, :eventMessage, dateTime('now', 'localtime'))");
m_insertQuery.prepare("INSERT INTO userlogevents (userName, eventMessage, dateTime) VALUES(:userName, :eventMessage, dateTime('now', 'localtime'))");
To copy to clipboard, switch view to plain text mode
before I added in time component in to the dateTime I didn't seem to have any issue passing the date entered in the textField to the function call
UserEventLog.searchUserNameDateText(userNameDropDown.currentText, beginDateTextField.text);
UserEventLog.searchUserNameDateText(userNameDropDown.currentText, beginDateTextField.text);
To copy to clipboard, switch view to plain text mode
I have tried passing it different ways e.g.
beginDateTextField.text;
beginDateTextField.text.toString();
beginDateTextField.text.toString("yyyy-MM-dd");
beginDateTextField.text.toString("yyyy-MM-dd hh:mm:ss);
beginDateTextField.text;
beginDateTextField.text.toString();
beginDateTextField.text.toString("yyyy-MM-dd");
beginDateTextField.text.toString("yyyy-MM-dd hh:mm:ss);
To copy to clipboard, switch view to plain text mode
I tried creating a local var and try passing that as a Date type e.g.
var myVar = new Date();
myVar.setDate(beginDateTextField.text, "yyyy-MM-dd");
var mydate = new Date();
mydate.setDate(Qt.formatDateTime(beginDateTextField.text, "yyyy-MM-dd hh:mm:ss"));
var locale = Qt.locale();
var mydate = Date.fromLocaleString(locale, beginDateTextField.text, "yyyy-MM-dd hh:mm:ss");
UserEventLog.searchUserNameDateText(userNameDropDown.currentText, mydate);
var myVar = new Date();
myVar.setDate(beginDateTextField.text, "yyyy-MM-dd");
var mydate = new Date();
mydate.setDate(Qt.formatDateTime(beginDateTextField.text, "yyyy-MM-dd hh:mm:ss"));
var locale = Qt.locale();
var mydate = Date.fromLocaleString(locale, beginDateTextField.text, "yyyy-MM-dd hh:mm:ss");
UserEventLog.searchUserNameDateText(userNameDropDown.currentText, mydate);
To copy to clipboard, switch view to plain text mode
I have tried messing with the formating
UserEventLog.searchUserNameDateText(userNameDropDown.currentText, Qt.formatDateTime(beginDateTextField.text, "yyyy-MM-dd hh:mm:ss"));
UserEventLog.searchUserNameDateText(userNameDropDown.currentText, Qt.formatDateTime(beginDateTextField.text, "yyyy-MM-dd hh:mm:ss"));
To copy to clipboard, switch view to plain text mode
I'm not quite sure what I'm doing wrong, I figured I could just pass the text the user entered into the date text field as I did before beginDateTextField.text and on the C++ side convert the dates to string to re-format them and add in generic time stamps however when I debug the date passed to the function nothing out puts.... I'm not sure if passing a text(string) from the qml side is right but that’s how I did it before and it worked... I tried changing the functions parameters data types from QDate to QString but that didn't work either (its just a plain date being passed as a string from QML it worked before not sure why its not now)
QString begin
= dateText.
toString("yyyy-MM-dd 00:00:00");
QString end
= dateText.
toString("yyyy-MM-dd 23:59:59");
QString begin = dateText.toString("yyyy-MM-dd 00:00:00");
QString end = dateText.toString("yyyy-MM-dd 23:59:59");
To copy to clipboard, switch view to plain text mode
I was reading about qml Date type and found this in the doc:
When integrating with C++, note that any QDate value passed into QML from C++ is automatically converted into a date value, and vice-versa.
Not sure Why I wasn’t having a problem before the time aspect got in cooperated and not sure what the cause of my issue is now, if you can help that be great
Bookmarks