for my BOT I'm using a DB to get and store some stuff. Now I wanna only output everything in the DB that is newer/after today. Thatfor I wrote this code, but it isn't working.
ts = time.gmtime()
tsy = str(ts[0])
tsm = str(ts[1])
tsd = str(ts[2])
todaysdate = tsy + '-' + tsm + '-' + tsd
selDBcmd = "SELECT UT, SJ, HW FROM `homework` WHERE DATE(UT) >= '%s';"
cur.execute(selDBcmd, (todaysdate))
msg = 'homework:\n\n'
selDBc = cur.fetchall()
await client.send_message(message.channel, selDBc)
Does anyone has got an idea why its anyway outputting all of the DB data, and not only them where the date is after todays one?
I'm not as new to python, but to this mysql-connector thing in python
SO here is the answer I found out after some more trial and error working
ts = time.gmtime()
tsy = int(ts[0])
tsm = int(ts[1])
tsd = int(ts[2])
selDBcmd = "SELECT UT, SJ, HW FROM `homework` WHERE UT >= '%s-%s-%s';"
cur.execute(selDBcmd, (tsy, tsm, tsd))
Related
Ok so I've been pretty much learning on my own and I ran into a problem that I cant seem to find a solution for.
The main goal is I have a google sheet with a start and end date that the user can change, the script pulls those dates and uses them in a MySQL query to pull the data between that date range.
Example: start date = 10/1/2021, end date = 10/22/21
Query: "select * from Table, where table.date >= start date AND table.dat <= end date"
See my code example below:
======================================================
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName('DR_Campaign_Report');
var getStartDate = sheet.getRange(1,2).getValue();
var startDate = Utilities.formatDate(getStartDate,"GTM","MM/dd/yyyy");
var getEndDate = sheet.getRange(1,5).getValue();
var endDate = Utilities.formatDate(getEndDate,"GTM","MM/dd/yyyy");
var conn = Jdbc.getConnection(url, username, password);
var stmt = conn.createStatement();
var results = stmt.executeQuery
( 'SELECT m.Campaign as "Campaign",\n' +
'count(m.Campaign) as "Leads",\n' +
'count(m.Duplicate) as "Dups",\n' +
'count(m.Campaign) - count(m.Duplicate) as "Valid Leads",\n' +
'count(m.AppSet) as "Appts",\n' +
'SUM(IF(m.ZepID != "",1,0)) as "Transferred Appts"\n' +
'FROM intakeMani m\n' +
'WHERE date(m.IncomingDate) >= date('startDate') and date(m.IncomingDate) <= date('endDate')\n' +
'OR date(m.AppSet) >= date('startDate') and date(m.AppSet) <= date('endDate')\n' +
'GROUP BY m.Campaign with rollup'
);
The Error is here in the WHERE clause when its attempting to pull the google script variables startDate and endDate.
'WHERE date(m.IncomingDate) >= date('startDate') and date(m.IncomingDate) <= date('endDate')\n' +
'OR date(m.AppSet) >= date('startDate') and date(m.AppSet) <= date('endDate')\n' +
I attempted the double "'startDate'" and is still errors. see attached pic.
I fixed it, I had double " in the var getStartDate/GetEndDate fields and need to add " to the query "'+startdate+'"
Thank you for your help.
I have a piece of code like this:
isbn = 4567
c.execute("SELECT * FROM book WHERE Book_d = %s;",(isbn,))
search = c.fetchone()
print(search)
But I want to change the attribute to a variable like this:
isbn = 4567
bisbn = 'Book_d'
c.execute("SELECT * FROM book WHERE %s = %s;",(bisbn, isbn,))
search = c.fetchone()
print(search)
But I guess the syntax is wrong here.
I just wanted to ask whether it is possible to do something like this and if so how?
Thanks
Please check this.
isbn = 4567
bisbn = 'Book_d'
sql_query = "SELECT * FROM book WHERE %s = %s;"%(bisbn, isbn,)
Or if you are using python3
sql_query = f"SELECT * FROM book WHERE {bisbn} = {isbn}"
print(sql_query)
I have a web app built in .NETCore 2.1 which calls an API built in .Net 4.6. The API calls a services layer which is using entity framework 6. I'm connecting to a MySql database. I'm using the Pomelo.EntityFrameworkCore.MySql connector. When using a LINQ query to get records based on a date comparison, EF6 appears to be cmparing the dates as string literals.
I've tried all different types of variations of the LINQ query and looked online for extension libraries that do date comparisons, but to no avail
Here's the LINQ query
int year = DateTime.Now.Year;
DateTime firstDayOfYear = new DateTime(year, 1, 1);
DateTime lastDayOfYear = new DateTime(year, 12, 31);
var startDate = firstDayOfYear.AddYears(-2);
var endDate = lastDayOfYear.AddYears(-1);
var company = dbContext.Companies.Where(c =>
c.Updating &&
c.UpdatedBy == username &&
c.LastUpdate > startDate &&
c.LastUpdate < endDate)
.FirstOrDefault();
Here's part of the SQL that is generated. note the # symbols before the date values
`Extent2`.`ID` AS `ID1`,
`Extent2`.`Name` AS `Name1`
FROM Company AS `Extent1` INNER JOIN `Table_2` AS `Extent2` ON `Extent1`.`ID` = `Extent2`.`ID`
WHERE ((((`Extent1`.`SomeFlag` != 1) AND (`Extent1`.`LastUpdate` > #'01/01/2017 00:00:00')) AND (`Extent1`.`LastUpdate` < #'31/12/2017 00:00:00')) AND ((`Extent1`.`ID` IN ( '1','2','6','7','8' )) AND (`Extent1`.`ID` IS NOT NULL))) AND (NOT ((`Extent1`.`Telephone` IS NULL) OR ((LENGTH(`Extent1`.`Telephone`)) = 0)))) AS `Project1`
Running the sql query manually in MySql workbench returns no rows. if I remove the # symbols it does return rows, as expected. What am I doing wrong?
I couldn't figure out why EF was translating the LINQ that way so I just used a stored procedure call to get round it. At least then I could write the SQL myself.
int year = DateTime.Now.Year;
DateTime firstDayOfYear = new DateTime(year, 1, 1);
DateTime lastDayOfYear = new DateTime(year, 12, 31);
var startDate = firstDayOfYear.AddYears(QueueConfig.UpdateQueueStartDateInYears);
var endDate = lastDayOfYear.AddYears(QueueConfig.UpdateQueueStartDateInYears);
var userParam = new MySql.Data.MySqlClient.MySqlParameter("userName", username);
var sdParam = new MySql.Data.MySqlClient.MySqlParameter("startDate", startDate);
var edParam = new MySql.Data.MySqlClient.MySqlParameter("endDate", endDate);
var lockedCompany = dbContext.Database.SqlQuery<CompanyDto>("CALL GetLockedCompany(#userName,#startDate,#endDate)", userParam, sdParam, edParam);
var companyDto = lockedCompany.SingleOrDefault();
i am totally stuck with this error, please do help me..
i create a json file, user can select from start date to end date..
so button1 function as to generate the file..
the program goes well until i select a larger data to generate.. then this memory error comes out.. Here is my code:
Using writer As JsonWriter = New JsonTextWriter(sw)
writer.Formatting = Formatting.Indented
With writer
.WriteStartObject()
.WritePropertyName("LiveValue")
.WriteStartArray()
Do
liveValue.Time_Stamp = u
liveValue.Current = Generator.Next(MyMin_Current, MyMax_Current + 1)
liveValue.Voltage = Generator.Next(MyMin_Voltage, MyMax_Voltage + 1)
liveValue.Power = liveValue.Current * liveValue.Voltage
.WriteStartObject()
.WritePropertyName("ID")
.WriteValue(i)
.WritePropertyName("TimeStamp")
.WriteValue(liveValue.Time_Stamp)
.WritePropertyName("MotorID")
.WriteValue(liveValue.MotorID)
.WritePropertyName("Current")
.WriteValue(liveValue.Current)
.WritePropertyName("Voltage")
.WriteValue(liveValue.Voltage)
.WritePropertyName("Power")
.WriteValue(liveValue.Power)
.WriteEndObject()
i = i + 1
If liveValue.MotorID < 20 Then
liveValue.MotorID = liveValue.MotorID + 1
Else
liveValue.MotorID = 1
End If
'If endTime > startTime Then
' liveV.Time_Stamp = u.AddMinutes(+1)
'Else
' liveV.Time_Stamp = endTime
'End If
'(Time Stamp) Time changed every 7secs
If i = w Then
u = u.AddMinutes(+1)
w = w + 20
End If
Loop Until (liveValue.Time_Stamp = endTime)
.WriteEndArray()
.WriteEnd()
End With
file1.WriteLine(sb.ToString)
pBar.Style = ProgressBarStyle.Continuous
End Using
please do help me to solve it..Thank you
Recently have designed a SSRS report for CRM 2011 on-premise using the #CRM_EntityName parameters to enable the Prefiltering option. But for some strange reason when I choose any value on the prefiltering window the report exits with an exception. Then I created a SQL Profiler and this is what the SQL receive:
exec sp_executesql N'declare #binUserGuid varbinary(128)
declare #userGuid uniqueidentifier
select #userGuid = N''{552d241b-0347-e311-9f1e-00155d039706}''
set #binUserGuid = cast(#userGuid as varbinary(128))
set context_info #binUserGuid;
DECLARE #SQL2 AS nVarchar(max)
SET #SQL2 = ''SELECT StudentsRecords.xrmsm_students_id, StudentsRecords.xrmsm_studentsfullname, StudentProgress.TotalPoints, StudentProgress.PointsObtained,
StudentProgress.xrmsm_subjectscode, StudentProgress.Average,
CASE WHEN StudentProgress.Average >= 90 THEN ''''AN'''' WHEN StudentProgress.Average >= 80 THEN ''''PR'''' WHEN StudentProgress.Average >= 60 THEN ''''EP'''' WHEN StudentProgress.Average
> - 1 THEN ''''I'''' ELSE NULL END AS Progress, CONVERT(nvarchar(50), StudentsRecords.xrmsm_studentsid) + StudentProgress.xrmsm_subjectscode AS VLookupData,
StudentsRecords.xrmsm_studentsgrade, StudentsRecords.xrmsm_studentsgradename as Value
FROM Filteredxrmsm_students AS StudentsRecords INNER JOIN
(SELECT st.xrmsm_studentsid, su.xrmsm_subjectscode,
SUM(sc.xrmsm_scoresresults) AS PointsObtained, SUM(eva.xrmsm_evaluationstotal) AS TotalPoints,
SUM(sc.xrmsm_scoresresults) / SUM(eva.xrmsm_evaluationstotal) * 100.00 AS Average
FROM Filteredxrmsm_scores as sc INNER JOIN
Filteredxrmsm_evaluations as eva ON sc.xrmsm_evaluationlookup = eva.xrmsm_evaluationsid INNER JOIN
Filteredxrmsm_students as st ON sc.xrmsm_studentlookup = st.xrmsm_studentsid INNER JOIN
(''
+ #CRM_Filteredxrmsm_sessions + '')
AS se ON
se.xrmsm_sessionsid = eva.xrmsm_sessionlookup INNER JOIN
Filteredxrmsm_institutionCourses as ic ON
ic.xrmsm_institutioncoursesid = se.xrmsm_institutioncourselookup INNER JOIN
Filteredxrmsm_courses as co ON co.xrmsm_coursesid = ic.xrmsm_courselookup INNER JOIN
(''
+ #CRM_Filteredxrmsm_subjects + '') AS su ON
su.xrmsm_subjectsid = co.xrmsm_subjectlookup INNER JOIN
Filteredxrmsm_sessionEnrollments as sen ON sen.xrmsm_studentlookup = sc.xrmsm_studentlookup AND
eva.xrmsm_sessionlookup = se.xrmsm_sessionsid AND
eva.xrmsm_termsessionlookup = sen.xrmsm_termsessionlookup INNER JOIN
(''
+ #CRM_Filteredxrmsm_educationalstructure + '') as ed ON
eva.xrmsm_yearlookup = ed.xrmsm_yearlookup AND
se.xrmsm_institutionlookup = ed.xrmsm_institutionlookup AND
ed.xrmsm_programlookup = se.xrmsm_programlookup
WHERE (eva.xrmsm_evaluationsdate >= ''''''+ convert(varchar(10),#termStartDate,120) + '''''') AND (eva.xrmsm_evaluationsdate <= ''''''+ convert(varchar(10),#monthReport,120) + '''''')
AND (eva.statuscode = 1) AND (sc.statuscode = 1) AND (st.statuscode = 1) AND
(se.statuscode = 1) AND (sen.statuscode = 1) AND
(ed.statuscode = 1)
GROUP BY st.xrmsm_studentsid, su.xrmsm_subjectscode) AS StudentProgress ON
StudentsRecords.xrmsm_studentsid = StudentProgress.xrmsm_studentsid''
EXEC (#SQL2)',N'#CRM_Filteredxrmsm_educationalstructure nvarchar(114),#CRM_Filteredxrmsm_sessions nvarchar(78),#CRM_Filteredxrmsm_subjects nvarchar(165),#termStartDate datetime,#monthReport datetime',#CRM_Filteredxrmsm_educationalstructure=N'select
[xrmsm_educationalstructure0].*
from
Filteredxrmsm_educationalstructure as "xrmsm_educationalstructure0"',#CRM_Filteredxrmsm_sessions=N'select
[xrmsm_sessions0].*
from
Filteredxrmsm_sessions as "xrmsm_sessions0"',#CRM_Filteredxrmsm_subjects=N'select
[xrmsm_subjects0].*
from
Filteredxrmsm_subjects as "xrmsm_subjects0"
where
("xrmsm_subjects0".xrmsm_subjectsid = N''0FE2990A-1847-E311-9F1E-00155D039706'')',#termStartDate='2014-01-01 00:00:00',#monthReport='2014-01-31 00:00:00'
When I run this statement directly on SQL I receive the following error:
Msg 4145, Level 15, State 1, Line 44
An expression of non-boolean type specified in a context where a condition is expected, near 'Student'.
I dont know what could be the problem.
Thanks for all the views. I already solved the problem. My solution was make the dataset with the subquery part then i make the calculations in the report.