python - How to get data from Object Oriented Programming to mySQL? -
how can pass data object oriented programming mysql in python? need make connection in every class?
update:
this object orinted
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)
and try data mysql using coding.
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 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()
update:
datapoint
class datapoint: def __init__(self, datavaluebytes): self._datavaluebytes = datavaluebytes
i thinking of class has function connection , pass instance of class every class need connection (here db instance), or may not! it's idea used in other languages.