我很难弄清楚为什么会出现错误:nameerror:name“convert tobinarydata”没有定义,有什么想法吗?
我认为函数convertToBinaryData的声明是正确的,尽管我不确定如何修复这个问题
回溯(最近一次调用):文件“C:\users\user............\testdb.py”,第20行,db.insertRawEcg(1,2,“C:\users\user\desktop\......\ecgexample.jpg”,真,真,0.08,0.08,真,0.04,0.2,0.04,0.2,0.2,0.2,文件“C:\users\user\desktop......\dbconnect.py”,第146行,insertRawEcg scanPic=convertToBinaryData(EcgScan)名称错误:未定义名称“convertToBinaryData”
DBConnect.py
import mysql.connector
import datetime
import sys
class DBConnect:
config = {
'user': 'root',
'password': 'root',
'host': 'localhost',
'database': 'EcgDB',
'raise_on_warnings': True,
}
examOccurence = datetime.datetime.now()
currDate = examOccurence.strftime("%Y/%m/%d")
currTime = examOccurence.strftime("%H:%M:%S")
link = mysql.connector.connect(**config) #Establishing connection
mycourser = link.cursor(buffered=True) #Creating cursor object to allow execution of SQL statements
MeasurementsTable = """CREATE TABLE MeasurementsOfPatient(
ExamID int,
EcgID int,
ExamDate date,
ExamTime time,
PRIMARY KEY (ExamID, EcgID)
)"""
PatientsTable = """CREATE TABLE patients (
PatientID int NOT NULL AUTO_INCREMENT,
GroupID int,
ExamID int,
Age int,
Gender char(1) NOT NULL,
MedBackground VARCHAR(255),
PRIMARY KEY (PatientID),
FOREIGN KEY (ExamID)
REFERENCES MeasurementsOfPatient(ExamID)
ON UPDATE CASCADE,
FOREIGN KEY (GroupID)
REFERENCES users(GroupID)
ON UPDATE CASCADE
)"""
UsersTable = """CREATE TABLE users (
UserName VARCHAR(255),
PassWord VARCHAR(255),
GroupID int,
PRIMARY KEY (UserName, PassWord),
UNIQUE (GroupID)
)"""
EcgRawDataTable = """CREATE TABLE EcgRawData (
ExamID int,
EcgID int,
EcgScan MEDIUMBLOB,
Lead boolean,
Pwave boolean,
PXlength DECIMAL(3, 2),
PwaveAvg DECIMAL(3, 2),
Qwave boolean,
QXlength DECIMAL(3, 2),
QYlength DECIMAL(3, 2),
QwaveAvgX DECIMAL(3, 2),
QwaveAvgY DECIMAL(3, 2),
Swave boolean,
SXlength DECIMAL(3, 2),
SYlength DECIMAL(3, 2),
SwaveAvgX DECIMAL(3, 2),
SwaveAvgY DECIMAL(3, 2),
Twave boolean,
Rwave boolean,
RwaveAvg DECIMAL(3, 2),
PRinterval DECIMAL(3, 2),
QRSinterval DECIMAL(3, 2),
RRinterval DECIMAL(3, 2),
HeartRate int,
RespiratoryRate int,
HeartRythem VARCHAR(255),
FOREIGN KEY (ExamID, EcgID)
REFERENCES MeasurementsOfPatient(ExamID, EcgID)
ON UPDATE CASCADE
)"""
EcgParamsTable = """CREATE TABLE EcgParams(
ExamID int,
EcgID int,
EcgScan MEDIUMBLOB,
Lead boolean,
Pwave boolean,
PXlength DECIMAL(3, 2),
PwaveAvg DECIMAL(3, 2),
Qwave boolean,
QXlength DECIMAL(3, 2),
QYlength DECIMAL(3, 2),
QwaveAvgX DECIMAL(3, 2),
QwaveAvgY DECIMAL(3, 2),
Swave boolean,
SXlength DECIMAL(3, 2),
SYlength DECIMAL(3, 2),
SwaveAvgX DECIMAL(3, 2),
SwaveAvgY DECIMAL(3, 2),
Twave boolean,
Rwave boolean,
RwaveAvg DECIMAL(3, 2),
PRinterval DECIMAL(3, 2),
QRSinterval DECIMAL(3, 2),
RRinterval DECIMAL(3, 2),
HeartRate int,
RespiratoryRate int,
HeartRythem VARCHAR(255),
FOREIGN KEY (ExamID, EcgID)
REFERENCES MeasurementsOfPatient(ExamID, EcgID)
ON UPDATE CASCADE
)"""
def convertToBinaryData(self, filename):
#Convert digital data to binary format
with open(filename, 'rb') as file:
binaryData = file.read()
return binaryData
def write_file(self, data, filename):
#convert binary data to proper format and write it on hard disk
with open(filename, 'wb') as file:
file.write(data)
def insertRawEcg(self, ExamID, EcgID, EcgScan, Lead, Pwave, PXlength, PwaveAvg,
Qwave, QXlength, QYlength, QwaveAvgX, QwaveAvgY, Swave,
SXlength, SYlength, SwaveAvgX, SwaveAvgY, Twave, Rwave,
RwaveAvg, PRinterval, QRSinterval, RRinterval, HeartRate,
RespiratoryRate, HeartRythem):
insertScan = """INSERT INTO EcgRawData VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""
scanPic = convertToBinaryData(EcgScan)
dataTuple = (ExamID, EcgID, scanPic, Lead, Pwave, PXlength, PwaveAvg,
Qwave, QXlength, QYlength, QwaveAvgX, QwaveAvgY, Swave,
SXlength, SYlength, SwaveAvgX, SwaveAvgY, Twave, Rwave,
RwaveAvg, PRinterval, QRSinterval, RRinterval, HeartRate,
RespiratoryRate, HeartRythem)
cursor.execute(insertScan, dataTuple)
link.commit()
print("Raw data inserted successfully to DB")
testdb.py
from DBConnect import DBConnect
db = DBConnect()
db.insertRawEcg(1, 2, "C:\\Users\\USER\\Desktop\\...\\...\\...\\ecgexample.jpg", True, True, 0.08, 0.08, True, 0.04, 0.2, 0.04, 0.2, True, 1, 0.5, 1, 0.5, True, True,0.08, 1, 2, 4, 60, 13, "NSR")
使用scanpic=self.ConvertToBinaryData(EcgScan)
,因为ConvertToBinaryData
是一个类方法。
问题内容: 我在自学Python,只是在“探索”。Google说datetime是一个全局变量,但是当我尝试在终端中查找今天的日期时,我在问题标题中收到NameError吗? 问题答案: 您需要先导入模块: 之后,它可以工作:
问题内容: 我有以下代码,并在尝试运行它时收到以下错误消息: 我试图让Raspberry Pi在端口17上收到输入时运行HTML脚本: 问题答案: Python的布尔常量是大写的:与以大写字母和分别。 小写变体只是变量的有效免费名称,因此您可以将它们用于任何所需的变量,例如(不建议使用; P)。
我试图做一个基于链接列表和节点的N-Ary树。但是每当我试图给树添加一个新值时,我总是得到: NameError:未定义名称“self” 我使用模块,所以我必须从其他文件导入类。 我在树代码的def addTree(self,value,parent=self.root)中遇到了这个错误 树代码 节点代码 链接列表代码 也谢谢你的帮助,我对Python有点陌生,我不知道我做错了什么。
问题内容: 我使用cxfreeze从planrequest.py创建Windows可执行文件。看来工作正常,但是当我运行exe文件时,我得到了 在)要使用此修复程序的python状态中未定义名称出口。但是,我使用import sys。该代码可以作为python脚本正常运行(例如,在编译为可执行文件之前,我对命令行参数进行了广泛测试。) 问题答案: 导入sys不足以在全球范围内实现。 你要么要做 要
问题内容: 为什么这样的结构 给一个错误? 问题答案: 默认参数值在函数定义时评估,但仅在函数调用时可用。因此,参数列表中的参数不能相互引用。 将参数默认为默认值并在代码中为此添加测试是一种常见的模式:
问题内容: 我在Mac OS X上使用Python 2.6.1。 我有两个简单的Python文件(如下),但是当我运行时 我在终端上: 我尝试以不同的方式重命名文件和类,这就是为什么两端有x和z的原因。;) 文件sqlitedbx.py 文件update_url.py 问题答案: 您需要做: