I'm working on a Python project which use SQlite3 database.
I created database with only one table called "Message" with this kind of data:
connexion = sqlite3.connect(BDD)
c = connexion.cursor()
c.execute(f""" CREATE TABLE IF NOT EXISTS {arg}(
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Jour_Heure_Reception text,
Jour_Heure_Reponse text,
Theme text,
Motif text,
Risque_incident_client text,
Transfert_sans_action text,
Matricule text,
Origine text)""")
connexion.commit()
connexion.close();
My goal is to search between 2 dates in the Jour_Heure_Reponse column, and return the number of entries by matricule.
To do that, I use this SQlite query:
def nbr(arg):
"requette dans la bdd de statistiques mails retournant le nombre de messages par utilisateur sur la période arg"
#Création de la liste des utilisateurs ayant saisies des entrées dans la BDD sur la période
connexion = sqlite3.connect(BDD)
c = connexion.cursor()
date_selection = str((datetime.now() - timedelta(arg)).strftime('%d/%m/%Y'))
yesterday = str(datetime.now().strftime('%d/%m/%Y'))
c.execute(f"""
select Matricule from Message Where
Jour_Heure_Reception >= "{date_selection}"
and Jour_Heure_Reponse < "{yesterday}" """ )
agents = c.fetchall()
liste_agents = []
for i in agents:
if not i[0] in liste_agents:
liste_agents.append(i[0])
c.close()
# calcul du nombre d'entrées pour chaque matricules présent dans la liste crée précédemment
connexion = sqlite3.connect(BDD)
c = connexion.cursor()
liste_affichage = []
for i in liste_agents:
c.execute(f"""SELECT * FROM Message where
Matricule = "{i}" and
Jour_Heure_Reception >= "{date_selection}" and
Jour_Heure_Reponse < "{yesterday}" """)
test = c.fetchall()
print(i)
for i in test:
print(i[1])
data_list = [str(i),str(len(test))]
liste_affichage.append(data_list)
c.close()
The problème is that one:
When i call mi nbr func, it return nothing if mi arg is not 1, and even with 1 arg, the result is not logical.
for exemple, calling nrb(1), return this ( i only print dates ):
02/06/2020
02/06/2020
02/06/2020
02/06/2020
02/06/2020
02/06/2020
02/06/2020
03/06/2020
03/06/2020
03/06/2020
03/06/2020
02/07/2020
02/07/2020
02/07/2020
03/07/2020
03/08/2020
02/09/2020
03/09/2020
03/09/2020
09/08/2020
09/08/2020
09/08/2020
09/08/2020
09/08/2020
01/10/2020
02/10/2020
02/10/2020
As you can see, the timedelta is not respected.
As the datatype is stored as Text, I send dates as str after a time.strftime() conversion.
Where am I going wrong?
Sqlite does not have a dedicated date type. It supports storing date using a text field in ISO-8601 format. You will have to do date format conversion, so that you can perform your comparison. For the format details and documentation see this:
https://www.sqlite.org/lang_datefunc.html
You should use %Y-%m-%d as format string.
Related
I have a school project on analyzing logs & roughly, I need to be able to retrieve the day & time from a line in the format "MM DD HH:MM:SS" and it always shows as "['MM DD HH:MM:SS']"
def get_complete_date(line):
"""
Pre : line est une ligne de log bien formée (str)
Post : Retourne la date et l'heure sous forme de chaine de caractère sans changer le format.
"""
# splt = line.split(sep=" ", maxsplit=5)
# dt = splt[2]
complete_date = line.split(" ")
# print(complete_date)
return complete_date[0:3]
Very naive approach, but if the length of datetime part is the same (same pattern on each line), you can simply slice it from 0 to pattern length:
def get_complete_date(l):
tpl = "MM DD HH:MM:SS"
return l[0: len(tpl)]
line = "MM DD HH:MM:SS some text"
print(get_complete_date(line))
>>>'MM DD HH:MM:SS'
You almost there. It's an issue with slicing.
def get_complete_date(line):
"""
Pre : line est une ligne de log bien formée (str)
Post : Retourne la date et l'heure sous forme de chaine de caractère sans changer le format.
"""
# splt = line.split(sep=" ", maxsplit=5)
# dt = splt[2]
complete_date = line.split(" ")
# print(complete_date)
return complete_date[1:3:1]
line = "MM DD HH:MM:SS"
print(get_complete_date(line))
Gives #
['DD', 'HH:MM:SS']
Explanation: Your slicing should be
[1:3:1] = [start:stop:step]
The 0th element is month which you don't need to start from 1.
I'm using Python to grab a JSON response. I have a json string that has fields that contain a single quote inside of the data. I'm trying to replace a single escaped quote ' with two single quotes so that I can insert it into SQL server. The problem is, when I use:
json_str = json_string.replace("\'","''")
it is changing it to ''.
How can I massage this data so that I can get it into SQL server?
Code snippet:
import pyodbc
import json
import requests
import csv
import datetime as DT
from datetime import datetime
import pytz
from pytz import timezone
# Dates / Times
today = DT.date.today()
week_ago = today - DT.timedelta(days=2)
# Settings
auth = ''
view_tickets = []
# List tickets from a View
print(f'Getting tickets...')
url = f'https:... created>{week_ago}'
headers = {'Content-Type': 'application/json'}
while url:
response = requests.get(url, auth=auth, headers=headers)
page_data = response.json()
# convert json to string
json_string = json.dumps(page_data)
# remove backslashes that screw up SQL import
json_string = json_string.encode().decode('unicode_escape')
#json_str = json_string.replace("\'","''")
# SQL EXPRESS INSERT Process
conn=pyodbc.connect('Driver={SQL Server};Server=NY-.\SQLEXPRESS;Database=Metrics;Trusted_Connection=yes;')
conn.timeout = 60
conn.autocommit = True
# Call SQL and trap Error if raised
try:
cursor = conn.cursor()
# SQL string
sql_declare = "DECLARE #json NVARCHAR(MAX) = N'" + json_str + "'"
sql_stat = "INSERT INTO Metrics.dbo.Tickets SELECT * FROM OPENJSON(#json, '$.results') WITH (id INTEGER '$.id', subject VARCHAR(255) '$.subject')"
sql = sql_declare + " " + sql_stat
cursor.execute(sql)
print('Connected to database')
except pyodbc.Error as err:
print('Error !!!!! %s' % err)
except:
print('something else failed')
conn.close()
print('closed db connection')
I have tried string.replace but that is not working.
Sample String:
'{"results": [{"subject": "Golf International de Longwy & Golf de Thionville Rive Droite - Add course request", "raw_subject": "Golf International de Longwy & Golf de Thionville Rive Droite - Add course request", "description": "Name GILBERT RAVAGLI\nEmail xxxxx\nPhone 00352781695321 france\n\nMessage\n\nBonjour J'ai acheter une montre Les deux golf qui sont dans mon secteur n'apparaissent pas sur la montre Le golf internationnal de Longwy et le golf de Thionville rive droite France Pouvez vous les cartographier que la montre puisse me servir? Cordialement"}], "facets": null, "next_page": null, "previous_page": null, "count": 72}'
SQL Code:
DECLARE #json NVARCHAR(MAX)
SET #json = N'{"results": [{"subject": "Golf International de Longwy & Golf de Thionville Rive Droite - Add course request", "raw_subject": "Golf International de Longwy & Golf de Thionville Rive Droite - Add course request", "description": "Name GILBERT RAVAGLI\nEmail xxxxx\nPhone 00352781695321 france\n\nMessage\n\nBonjour J'ai acheter une montre IZZO SWAMI Les deux golf qui sont dans mon secteur n'apparaissent pas sur la montre Le golf internationnal de Longwy et le golf de Thionville rive droite France Pouvez vous les cartographier que la montre puisse me servir? Cordialement"}], "facets": null, "next_page": null, "previous_page": null, "count": 72}'
SELECT * FROM OPENJSON (#json)
WITH (
subject VARCHAR(250) '$.subject'
)
Shouldn't all of this:
sql_declare = "DECLARE #json NVARCHAR(MAX) = N'" + json_str + "'"
sql_stat = "INSERT ... FROM OPENJSON(#json, '$.results') ..."
sql = sql_declare + " " + sql_stat
cursor.execute(sql)
Just be:
sql = "INSERT ... FROM OPENJSON(?, '$.results') ...);"
cursor.execute(sql, json_string)
Now you're passing in a parameter and don't have to deal with local variables in T-SQL, string delimiters, single quotes, etc.
I'm trying to add to the Table Widget the information that I have on my SQLite table.
That's it my SQL table:
.
That my Table Widget:
The code of the method:
if self.ui.btn_jugadores.clicked.connect(lambda: self.ui.pila_de_paginas.setCurrentWidget(self.ui.pg_jugadores)): self.mostrar_jugadores()
def mostrar_jugadores(self):
try:
cursor = conectarse_bbdd_jugadores()
consulta = ''' SELECT * FROM jugadores'''
ejecucion = cursor.execute(consulta).fetchall()
if len(ejecucion) > 0:
fila = 0
for e in ejecucion:
columna = 0
for apartado in e:
celda = QTableWidgetItem(apartado)
self.ui.tabla_jgds.setItem(fila, columna, celda)
columna += 1
print(apartado)
fila += 1
else:
QMessageBox.setText('No hay registros en la tabla de jugadores')
QMessageBox.setIcon(QMessageBox.warning)
QMessageBox.exec_()
except Error as error:
QMessageBox.setText('No se ha podido ejecutar la sentencia')
QMessageBox.setIcon(QMessageBox.warning)
QMessageBox.exec_()
And this is the result:
when I executed it
I don't know why the numbers are not appearing in the table widget.
I am importing an excel worksheet that has the following columns name:
N° Pedido
1234
6424
4563
The column name ha a special character (°). Because of that, I can´t merge this with another Data Frame or rename the column. I don´t get any error message just the name stays the same. What should I do?
This is the code I am using and the result of the Dataframes:
import pandas as pd
import numpy as np
# Importando Planilhas
CRM = pd.ExcelFile(r'C:\Users\Michel\Desktop\Relatorio de
Vendas\relatorio_vendas_CRM.xlsx', encoding= 'utf-8')
protheus = pd.ExcelFile(r'C:\Users\Michel\Desktop\Relatorio de
Vendas\relatorio_vendas_protheus.xlsx', encoding= 'utf-8')
#transformando em Data Frame
df_crm = CRM.parse('190_pedido_export (33)')
df_protheus = protheus.parse('Relatorio de Pedido de Venda')]
# Transformando Campos em float o protheus
def turn_to_float(x):
return np.float(x)
df_protheus["TES"] = df_protheus["TES"].apply(turn_to_float)
df_protheus["Qtde"] = df_protheus["Qtde"].apply(turn_to_float)
df_protheus["Valor"] = df_protheus["Valor"].apply(turn_to_float)
#Tirando Tes de não venda do protheus
# tirando valores com código errado 6
df_protheus_1 = df_protheus[df_protheus.TES != 513.0]
df_protheus_2 = df_protheus_1[df_protheus_1.TES != 576.0]
**df_crm.columns = df_crm.columns.str.replace('N° Pedido', 'teste')
df_crm.columns**
Orçamento Origem N° Pedido Nº Pedido ERP Estabelecimento Tipo de
Pedido Classificação(Tipo) Aplicação Conta CNPJ/CPF Contato ...
Aprovação Parcial Antecipa Entrega Desconto da Tabela de Preço
Desconto do Cliente Desconto Informado Observações Observações NF Vl
Total Bruto Vl Total Completo
0 20619.0 23125 NaN Optitex 1 - Venda NaN Industrialização/Revenda
XAVIER E ARAUJO LTDA ME 7970626000170 NaN ... N N 0 0 0
Note that I used other codes for the bold part with the same result:
#renomeando tabela para dar Merge
#df_crm['proc'] = df_crm['N\xc2\xb0 Pedido']
#df_crm['N Pedido'] = df_crm['N° Pedido']
#df_crm.drop('N° Pedido',inplace=True,axis=1)
#df_crm
#df_crm['N Pedido'] = df_crm['N° Pedido']
#df.drop('N° Pedido',inplace=True,axis=1)
#df_crm
#df_crm_1 = df_crm.rename(columns={"N°Pedido": "teste"})
#df_crm_1
Thanks for posting the link to the Google Sheet. I downloaded it and loaded it via pandas:
df = pd.read_excel(r'~\relatorio_vendas_CRM.xlsx', encoding = 'utf-8')
df.columns = df.columns.str.replace('°', '')
df.columns = df.columns.str.replace('º', '')
Note that the two replace statements are replacing different characters, although they look very similar.
Help from: Why do I get a SyntaxError for a Unicode escape in my file path?
I was able to copy the values into another column. You could try that
df['N Pedido'] = df['N° Pedido']
df.drop('N° Pedido',inplace=True,axis=1)
Here's my situation:
I got a table on a database, as following:
nome_imagem estado type
57260-tracker-_tracker_face awake 0
57261-tracker-_tracker_face drowsiness 1
57268-tracker-_tracker_face noface 2
57289-tracker-_tracker_face distracted 3
57290-tracker-_tracker_face awake 1
57291-tracker-_tracker_face drowsiness 2
57293-tracker-_tracker_face noface 3
And I want to update de type column according to serveral if conditions, but I'm getting a error on line 29:
mysql.connector.errors.InternalError: Unread result found
I'm pretty sure its the way im making the query, and I already searched another questions about this before opening this one, but I couldnt solve it anyway.
EDIT 1: Changed the query and applied the solution gave by gzc to get rid of the error, but now it updates all the type column instead of just the cases where the if is true
import mysql.connector
from mysql.connector import errorcode
import os
cnx = mysql.connector.connect(user='root', database='empresa')
cursor = cnx.cursor()
fileDir = os.path.dirname(os.path.realpath(__file__))
textDir = os.path.join(fileDir, "test_img")
query = ("SELECT nome_imagem, estado, type FROM alertas ")
cursor.execute(query)
results = list(cursor)
for (nome_imagem, estado, type) in results:
print nome_imagem, estado
my_file_name = nome_imagem+'.txt'
my_file = open("test_img/"+my_file_name, 'r')
content = my_file.readline()
status = content.strip().split()[-1].split("=")[1]
face = content.strip().split()[0].split("=")[1]
print status, face #1 tem face, 0 nao tem
if (face == '1' and estado == status): #se tem cara e o estado que tem na bd for igual ao estado que o programa classificou = correto
print "correto"
cursor.execute("UPDATE alertas SET type='1' WHERE nome_imagem=nome_imagem")
if (face == '1' and estado == 'drowsiness' and status == 'awake') or (face == 1 and estado == 'awake' and status == 'drowsiness'): #verificar isto
print "trocado"
if (estado != '' and face == '0'): # se tiver estado mas nao tiver cara classifico logo como errado 3
print "errado"
if (estado == 'distracted' and face == '1'): # se tem cara mas for distracted deixo normal pois nao consigo classificar
print "normal"
cursor.close()
cnx.close()
What am I doing wrong?
Thanks
EDIT 2: gzc solver it again :)
You must read all results before doing another operation on the same cursor.
...
query = ("SELECT nome_imagem, estado, type FROM alertas ")
cursor.execute(query)
results = list(cursor)
for (nome_imagem, estado, type) in results:
...