我如何将数据从面向对象的编程传递到python中的mysql?我需要在每个班级建立联系吗?

更新:

这是我的目标

class AttentionDataPoint(DataPoint):
    def __init__ (self, _dataValueBytes):
        DataPoint._init_(self, _dataValueBytes)
        self.attentionValue=self._dataValueBytes[0]

    def __str__(self):
        if(self.attentionValue):
            return "Attention Level: " + str(self.attentionValue)

class MeditationDataPoint(DataPoint):
    def __init__ (self, _dataValueBytes):
        DataPoint._init_(self, _dataValueBytes)
        self.meditationValue=self._dataValueBytes[0]

    def __str__(self):
        if(self.meditationValue):
            return "Meditation Level: " + str(self.meditationValue)


我尝试使用此编码将数据发送到mysql。

import time
import smtplib
import datetime
import MySQLdb

db = MySQLdb.connect("192.168.0.101", "fyp", "123456", "system")
cur = db.cursor()

while True:
    Meditation_Level = meditationValue()
    Attention_Level = attentionValue()
    current_time = datetime.datetime.now()

    sql = "INSERT INTO table (id, Meditation_Level, Attention_Level, current_time) VALUES ('test1', %s, %s, %s)"
    data = (Meditation_Level, Attention_Level, current_time)
    cur.execute(sql, data)
db.commit()

db.close()


更新:

数据点

class DataPoint:
def __init__(self, dataValueBytes):
self._dataValueBytes = dataValueBytes

最佳答案

我正在考虑类似一个具有连接功能的类,您只需将该类的实例传递给需要连接的每个类(在此为db实例),也许不是!
这只是我在其他语言中使用过的想法。

关于python - 如何从面向对象的编程获取数据到mySQL?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37582199/

10-12 23:12
查看更多