This question already has answers here:
How can I parse a time string containing milliseconds in it with python?
(7 answers)
Closed 14 days ago.
i am reading a entrance time from sqlite database to calaulate the duration of the car stay.
the datetime is currenly able to insert and retrive but when formated to do calualtion it keeps give an error when using the datetime.strptime funtion.EnterTime is stored as a text in the sqlite database.
import sqlite3
import smtplib
from datetime import datetime, timedelta
# Connect to the database
conn = sqlite3.connect("py.db")
# Get current time
current_time = datetime.now()
# Define the carplate number
carplate = "SJJ4649G"
# Check if the carplate already exists in the database
cursor = conn.cursor()
query = "SELECT * FROM entrance WHERE carplate = ?"
cursor.execute(query, (carplate,))
result = cursor.fetchall()
# If the carplate already exists, send an email
if len(result) > 0:
# Get the email address from the gov_info table
query = "SELECT email FROM gov_info WHERE carplate = ?"
cursor.execute(query, (carplate,))
email_result = cursor.fetchall()
# Get the entrance time from the entrance table
query = "SELECT EnterTime FROM entrance WHERE carplate = ?"
cursor.execute(query, (carplate,))
entrance_time_str = cursor.fetchone()[0]
print (entrance_time_str)
entrance_time = datetime.strptime(entrance_time_str, "%Y-%m-%d %H:%M:%S")
# Calculate the cost
delta = current_time - entrance_time
cost = delta.total_seconds() / 3600 * 10 # 10 is the hourly rate
# Email details
email = "testcsad69#gmail.com"
password = "ufwdiqcfepqlepsn"
send_to = email_result[0][0]
subject = "Parking Fees"
message = f"The cost for parking the car with plate number {carplate} is ${cost:.2f}. The entrance time was {entrance_time} and the current time is {current_time}."
# Send the email
smtp = smtplib.SMTP('smtp.gmail.com', 587)
smtp.ehlo()
smtp.starttls()
smtp.login(email, password)
smtp.sendmail(email, send_to, f"Subject: {subject}\n\n{message}")
smtp.quit()
# If the carplate does not exist, insert it into the database
else:
query = "INSERT INTO entrance (carplate, EnterTime) VALUES (?, ?)"
cursor.execute(query, (carplate, current_time))
conn.commit()
# Close the connection
cursor.close()
conn.close()
i have printed the entrance time out and it matches the data shown in the database.
i have also tried to remove the fromating that would not let me do the calulations.
this is the error i get
2023-02-06 16:07:46.640395
Traceback (most recent call last):
File "/media/pi/6134-E775/payment.py", line 32, in <module>
entrance_time = datetime.strptime(entrance_time_str, "%Y-%m-%d %H:%M:%S")
File "/usr/lib/python3.7/_strptime.py", line 577, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "/usr/lib/python3.7/_strptime.py", line 362, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: .640395
You need to append .%f to take microseconds:
entrance_time_str = '2023-02-06 16:07:46.640395' # HERE --v
entrance_time = datetime.strptime(entrance_time_str, "%Y-%m-%d %H:%M:%S.%f")
>>> entrance_time_str
'2023-02-06 16:07:46.640395'
>>> entrance_time
datetime.datetime(2023, 2, 6, 16, 7, 46, 640395)
Related
import sqlite3
import traceback
from time import sleep
import mysql.connector
def check_user(user_id):
conn = mysql.connector.connect(host='localhost', database='online', user='root1', password='rootRRR111_')
cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS online(id INT, last_online_date TEXT)')
conn.commit()
select = "SELECT * FROM online WHERE id = %s LIMIT 0, 1"
result = cur.execute(select, (user_id,))
if result is None:
insert = ('INSERT INTO online (id, last_online_date) VALUES (%s, %s)')
cur.reset()
cur.execute(insert, (user_id, online_time))
conn.commit()
def update_online_status(user_id, online_time):
conn = mysql.connector.connect(host='localhost', database='online', user='root1', password='rootRRR111_')
cursor = conn.cursor()
select = 'SELECT last_online_date FROM online WHERE id = %s'
result = cursor.execute(select, (user_id,))
old_online = result
online_time = f'{old_online},{online_time}'
cursor.reset()
cursor.execute('UPDATE online SET last_online_date = %s WHERE id = %s', (online_time, user_id))
conn.commit()
app = Client("my_account")
app.start()
while True:
try:
with open('ids.ini', 'r') as file:
users = file.read().splitlines()
for user in users:
result = app.get_users(user)
user_id = result['id']
if result['status'] == 'offline':
unix_timestamp = float(result['last_online_date'])
local_timezone = tzlocal.get_localzone()
local_time = datetime.fromtimestamp(unix_timestamp, local_timezone)
online_time = local_time.strftime("%Y/%m/%d %H:%M:%S")
elif result['status'] == 'online':
now = datetime.now()
online_time = now.strftime("%Y/%m/%d %H:%M:%S")
check_user(user_id)
update_online_status(user_id, online_time)
# sleep(300)
except Exception:
traceback.print_exc()
continue
app.stop()
I am writing a program that would read the online status of a user in telegram.
Instead of writing online to an existing user, a huge number of identical rows appear in the database.
Example:
Table with repetitions
When I try to fix something, there are a lot of errors.
mysql.connector.errors.programmingerror: not all parameters were used in the sql statement
mysql.connector.errors.internalerror: unread result found
and other...
Pls help!!
import mysql.connector as mc
USER = ''
PASSWORD = ''
HOST = ''
DATABASE = ''
mydb = mc.connect(user=USER, password=PASSWORD, host=HOST, database=DATABASE)
print(mydb.get_server_info())
mycursor = mydb.cursor()
### Let's just say "len(list_example_01) = len(list_example_02)"
### and "len(rows)" (SELECT column01_name FROM table_name WHERE DATE > DATE_FORMAT(CURDATE(), '%Y-%m-%d %H:%m:%s') also same.
data_example = []
for idz, i in enumerate(list_example_01):
data_example.append((i, list_example_02[idz]))
sql = '''
UPDATE table_name SET column01_name = %s WHERE column02_name = %s AND DATE > DATE_FORMAT(CURDATE(), '%Y-%m-%d %H:%m:%s')
'''
mycursor.executemany(sql, data_example)
mydb.commit()
I am a beginner to mysql and python. I try to update value01 into column01 WHERE column02_name = list_example02[idz].
Following just show what i try to make...... e.i:
CPUNAME
IMG_PATH
DATE
I5-11400
http://bcd/~~
2021-04-30 15:00:00
I5-10900
http://abcd/~~
2021-04-30 15:00:00
more...
I have list.
list_example_02 = ["I5-11400", "I5-10900", "I5-10400", ... ]
I want to find the row (which has same "cpu name" in list).
And I want to update "IMG_PATH" column's value.
01_Run
UPDATE CPU_TABLE SET IMG_PATH = 'http://bcd/~~' WHERE CPUNAME = 'I5-11400' AND DATE > DATE_FORMAT(CURDATE(), '%Y-%m-%d %H:%m:%s')
02_Run
UPDATE CPU_TABLE SET IMG_PATH = 'http://abcd/~~' WHERE CPUNAME = 'I5-10900' AND DATE > DATE_FORMAT(CURDATE(), '%Y-%m-%d %H:%m:%s')
i try to make loop (01_RUN, 02_RUN, ...) with 2var (1 var is for searching, and choosing specific row, and other Var is for Value.)
But...... when i run script above, i met "error message"
Traceback (most recent call last):
File "\venv\lib\site-packages\mysql\connector\cursor.py", line 75, in call
return bytes(self.params[index])
IndexError: tuple index out of range
As above written, Tuple has 2 element and there are 2 placeholder (%s) in preparedstatement. But I got error - Index out of range -.
This is the question which i have.
how to fix this?
"Thank you in advance"
It makes no sense to Format curdate() as it has already that format you want
Do that instead, and avouid the problem altogether with the %s and dateformat
UPDATE CPU_TABLE SET IMG_PATH = 'https://bcd/~~' WHERE CPUNAME = 'I5-11400' AND DATE > CURDATE();
I'm trying to extract data from my db and display it in a well formatted form.
When db gives me the date i'm trying to display it in the form of String 'Day/Mon/Year'. I have used the following code to do so , but it is throwing me the error.
How do i convert a list to date.time object ?
import psycopg2
from datetime import datetime
db = psycopg2.connect("dbname=news")
if db:
print("DB CONNECTED")
c = db.cursor()
c.execute("select day from (select date(time) as day, count(id) as errors from log where status != '200 OK' group by day order by errors desc limit 1) as seq")
t = c.fetchall()
res = datetime.strftime(t,'%b %d, %Y')
print(res)
db.close()
TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'list'
From my experience you need to iterate over the list. maybe this wil work, havent tested it, but thats the way i do it.
import psycopg2
from datetime import datetime
db = psycopg2.connect("dbname=news")
if db:
print("DB CONNECTED")
c = db.cursor()
c.execute("select day from (select date(time) as day, count(id) as errors from log where status != '200 OK' group by day order by errors desc limit 1) as seq")
t = c.fetchall()
c.close()
db.close()
for dates in t:
print(datetime.strftime(dates, '%b %d, %Y'))
I'm setting up a program that extracts the date, sender, subject and body of emails in my outlook. However, when I run the code it gives me the below error:
Traceback (most recent call last):
File "C:\Users\zaballgl\Documents\Adhoc\2019\April\ETL_MetricsEmailOutlook.py", line 83, in <module>
cursor.execute("INSERT INTO dbo.BSO_metricsEmailReports([Start_Date],[Name],[Subject],[Body])values(?,?,?,?)",row['Start_Date'],row['Name'],row['Subject'],row['Body'])
pyodbc.ProgrammingError: ('Invalid parameter type. param-index=1 param-type=CDispatch', 'HY105')
This is my code for extracting data from my outlook:
import win32com.client
import pandas as pd
import datetime
import numpy as np
import pyodbc
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.Folders('email#outlook.com').Folders('Inbox')
messages = inbox.Items
message = messages.GetFirst()
rec_time = message.CreationTime
body_content = message.body
subj_line = message.subject
sender = message.Sender
year=[]
month=[]
day=[]
hour=[]
minute=[]
subject=[]
sender=[]
body = []
while message:
###This iterates every format of the message.CreationTime and append them to the list above
year.append(message.CreationTime.year)
month.append(message.CreationTime.month)
day.append(message.CreationTime.day)
hour.append(message.CreationTime.hour)
minute.append(message.CreationTime.minute)
## Iterates every subject and append them to the subject variable list
subject.append(message.subject)
## Iterates every sender name and append them to the sender variable list
sender.append(message.Sender)
## Iterates every sender name and append them to the sender variable list
body.append(message.body)
## Goes to the next email
message = messages.GetNext()
## This saves all the information to a context manager
#------COLUMNS FOR THE TABLE---------------#
#StartDate
date = pd.DataFrame({'year':year,'month':month,'day':day,'hour':hour,'minute':minute}) # Had to do this to bypass this error: ValueError: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True
startDate = pd.to_datetime(date) # Using the above variable this converts this to dtype: datetime64[ns]
#Subject
subject = pd.Series(subject) # just a series of subject data
#Sender
sender = pd.Series(sender) # just a series of sender data
#Body
body = pd.Series(body) # just a series of sender data
df2 = pd.DataFrame({'Start_Date':startDate,'Name':sender, 'Subject': subject, 'Body':body})
And this is my code to transfer them to my MS SQL:
connStr = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server}; Server=someservername;DATABASE=somedatabase;UID=someID;PWD=somepassword#')
cursor = connStr.cursor()
deleteTable = "DELETE FROM dbo.BSO_metricsEmailReports"
cursor.execute(deleteTable)
for index,row in df2.iterrows():
cursor.execute("INSERT INTO dbo.BSO_metricsEmailReports([Start_Date], [Name],[Subject], [Body])values(?,?,?,?)",row['Start_Date'],row['Name'],row['Subject'],row['Body'] )
connStr.commit()
cursor.close()
connStr.close()
They would be sent to a table in my MS SQL 2014 with the below design:
**Column Name** | **Data Type**
Start_Date | datetime
Name | nchar(300)
Subject | nchar(300)
Body | nchar(300)
I'd check the execute line...
I use the ? for each parameter in the query and set it up:
sql_rollup =
'''
SELECT ID,FIRSTNAME, LASTNAME, .USERNAME, numSessions
FROM SESSION INNER JOIN
PERSONNEL ON SESSION.ID = PERSONNEL.ID
WHERE (SessionStartDT between ? AND ?) AND (SiteID = ?)
'''
Then I execute the above like this:
con = pyodbc.connect(
Trusted_connection='Yes',
Driver='{SQL Server}',
Server=myConfig["database"]["hostname"] + ',' + myConfig["database"]["port"],
Database=myConfig["database"]["database"]
)
con.autocommit = True
cur=con.cursor()
parms = (str(dateFirst), str(dateLast), siteID)
cur.execute(sql_rollup,parms)
Note the params is set up as a list and also (and I suspect this is where the problem lies), I convert the datatime values for dateFirst and dateLast to strings.
pyodbc doesn't understand Python objects. The database on the otherhand can automatically interpret strings and convert them into date/time values.
Does this help?
I am a bit of a newbie and I'm stuck with this postgres insert step.
My challenge is I am pulling a Dict from a json that is stored in a list and I am trying to pull the values from the dict and save it to a postgres DB.
any help on how to write this up correctly would be appreciated
Here is the connection string for the DB under the page break line is the code used for the db insert.
import psycopg2
'''DATABASE CONNECTION SETTINGS'''
def dbconnect():
"""Function returns settings for db connection."""
dbauth = psycopg2.connect("dbname='databse' user='username' \
host='dbhost' password='password'")
return dbauth
def weatherupdate(dbauth, list):
connection = dbauth
try:
connection
except:
print "I am unable to connect to the database"
conn = connection
cursor = conn.cursor()
l01 = list[0]['state_time_zone']
l02 = list[0]['time_zone']
l03 = list[0]['product_name']
l04 = list[0]['state']
l05 = list[0]['refresh_message']
l06 = list[0]['name']
l11 = list[1]['swell_period']
l12 = list[1]['lat']
l13 = list[1]['lon']
l14 = list[1]['cloud_oktas']
l15 = list[1]['gust_kt']
l16 = list[1]['history_product']
l17 = list[1]['local_date_time']
l18 = list[1]['cloud']
l19 = list[1]['cloud_type']
l110 = list[1]['swell_height']
l111 = list[1]['wmo']
l112 = list[1]['wind_dir']
l113 = list[1]['weather']
l114 = list[1]['wind_spd_kt']
l115 = list[1]['rain_trace']
l116 = list[1]['aifstime_utc']
l117 = list[1]['press_tend']
l118 = list[1]['press']
l119 = list[1]['vis_km']
l120 = list[1]['sea_state']
l121 = list[1]['air_temp']
l122 = list[1]['cloud_base_m']
l123 = list[1]['cloud_type_id']
l124 = list[1]['swell_dir_worded']
l125 = list[1]['sort_order']
query = "INSERT INTO weather (state_time_zone, time_zone, product_name, state, refresh_message, name, swell_period, lat, lon, cloud_oktas, gust_kt, history_product, local_date_time, cloud, cloud_type, swell_height, wmo, wind_dir, weather, wind_spd_kt, rain_trace, aifstime_utc, press_tend, press, vis_km, sea_state, air_temp, cloud_base_m, cloud_type_id, swell_dir_worded, sort_order ) VALUES (l01, l02, l03, l04, l05, l06, l11, l12, l13, l14, l15, l16, l17, l18, l19, l110, l111, l112, l113, l114, l115, l116, l117, l118, l119, l120, l121, l122, l123, l124, l125);"
cursor.execute(query)
conn.commit()
weatherupdate(dbconnect(), getweather())
When i run the code it throws this error:
Traceback (most recent call last):
File "weatherDb.py", line 57, in <module>
weatherupdate(dbconnect(), getweather())
File "weatherDb.py", line 53, in weatherupdate
cursor.execute(query)
psycopg2.ProgrammingError: column "l01" does not exist
LINE 1: ...d_type_id, swell_dir_worded, sort_order ) VALUES (l01, l02, ...
Im sure this is incorrect so any help and direction would be great.
Thanks in advance.
query = """INSERT INTO weather (state_time_zone, time_zone, product_name, [SNIP])
VALUES (%s, %s, %s, [SNIP] ) """
cursor.execute(query, (l01, l02, l03 [SNIP])