strftime(python)的问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了strftime(python)的问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2621字,纯文字阅读大概需要4分钟。
内容图文
![strftime(python)的问题](/upload/InfoBanner/zyjiaocheng/795/2369197a0de94553b3e0603071c5fcc4.jpg)
我尝试在Mosquitto Broker上发送数据并在SQLite上添加相同的数据
这是我的脚本:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sqlite3
import logging
class Photos:
def __init__(self):
#Connection a la basse de donnees sqlite3
self.db = sqlite3.connect('/home/pi/FlowerPower.db')
#Preparation d'un objet cursor qui va executer les futures requetes SQL sur la base de donnees
self.cursor = self.db.cursor()
logging.basicConfig(filename='subscribeFP.log',level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
def traitement(self, msg, numSonde):
try:
#Convertit le message poste par le publisher de byte a string
queryArgs = str(msg.payload)
#Variable nom de la sonde
nomSonde = str(numSonde[2])
#Prepare la requete SQL pour la table photo
queryText = "INSERT INTO photos(sonde, date, photos) VALUES (?, ?, ?)", (nomSonde, strftime('%%d-%%m-%%Y %%H:%%%M:%%S','now','localtime'), queryArgs)
#Execute la requete SQL sur la BDD
self.cursor.execute(str(queryText))
#Commit pour etre sur que la requete SQL est effectuee
self.db.commit()
#Code d'exception
except sqlite3.Error as e:
self.logging.error('Erreur Sqlite3: ' + str(e))
#Retour en arrière si il y a une erreur (proprietes ACID)
self.db.rollback()
当我用mosquitto_pub发送数据时,我有这个错误:
File "/usr/lib/python3/dist-packages/mosquitto.py", line 720, in loop
rc = self.loop_read(max_packets)
File "/usr/lib/python3/dist-packages/mosquitto.py", line 961, in loop_read
rc = self._packet_read()
File "/usr/lib/python3/dist-packages/mosquitto.py", line 1360, in _packet_read
rc = self._packet_handle()
File "/usr/lib/python3/dist-packages/mosquitto.py", line 1775, in _packet_handle
return self._handle_publish()
File "/usr/lib/python3/dist-packages/mosquitto.py", line 1888, in _handle_publish
self.on_message(self, self._userdata, message)
File "insertionBdd.py", line 37, in on_message
photos.traitement(msg, numSonde)
File "/home/pi/photosFP.py", line 22, in traitement
queryText = "INSERT INTO photos(sonde, date, photos) VALUES (?, ?, ?)", (nomSonde, strftime('%%d-%%m-%%Y %%H:%%%M:%%S','now','localtime'), queryArgs)
NameError: global name 'strftime' is not defined
我不明白为什么我有这个错误,我已经尝试添加:“from datetime importe datetime”
我的另一个脚本,运行strftime但没有这个问题…
有人可以帮帮我吗?
解决方法:
就像消息所说,你还没有定义strftime,你只定义了datetime.
strftime是datetime对象的一种方法.但是,您似乎将其称为独立函数,并将其传递给某些字符串:这根本不是它的工作原理.我不确定你要做什么,但看起来你想得到当前的时间和格式.所以,你需要实际调用相关的方法:
datetime.now().strftime('%d-%m-%Y %H:%M:%S')
我也不确定你的代码中“本地时间”是做什么的,或者为什么你将百分号加倍.
内容总结
以上是互联网集市为您收集整理的strftime(python)的问题全部内容,希望文章能够帮你解决strftime(python)的问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。