어떻게 객체 지향 프로그래밍에서 파이썬으로 mysql로 데이터를 전달할 수 있습니까? 모든 반에서 연결해야합니까?객체 지향 프로그래밍에서 데이터를 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)
을 orinted 내가이 코딩을 사용하여 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()
업데이트 :
DataPoint에
class DataPoint:
def __init__(self, dataValueBytes):
self._dataValueBytes = dataValueBytes
당신은 당신의 연결을 재사용 할 수 있습니다 (그리고 당신이해야) 물론 : 파이썬
가장 인기 SQLAlchemy의입니다. 코드를 보여주십시오. – Raptor
@Raptor 이미 코드를 업데이트합니다. – Hazim
DataPoint의 정의는 무엇입니까? – olivecoder