본문 바로가기
카테고리 없음

python sql

by Pooh0216 2016. 2. 11.
import MySQLdb as mdb
import sys
import json
import MySQLdb
import sys


host = "localhost"
db_id = "root"
db_pw = "1q2w3e"
db_name = "fitbit_db"

db = MySQLdb.connect( host, db_id, db_pw, db_name )
cursor = db.cursor()
#cursor.execute( "select version()" )
#data = cursor.fetchone()
#cursor.execute( "show tables")
#data = cursor.fetchall()
#print data

# sleep start
with open('001_sleep_start.txt') as f:
	data = f.read()
	jsondata  = json.loads(data)

for row in jsondata['sleep-startTime']:
	a = row['value']
	b = row['dateTime']
	cursor.execute("INSERT INTO Sleep_start VALUES(%s , %s)",(a,b))   
	data = cursor.fetchall()

# sleep time in Bed

with open('002_sleep_timeInBed.txt') as a:
	timeinbed = a.read()
	timeinbed_1 = json.loads(timeinbed)
	
for row1 in timeinbed_1['sleep-timeInBed']:
	a = row1['value']
	b = row1['dateTime']
	cursor.execute("INSERT INTO Sleep_timeinbed VALUES(%s , %s)",(a,b))   
	data = cursor.fetchall()


db.close()