''' ================================ D A T A B A S E C O N N E C T I O N ======================================'''
cnx = mysql.connector.connect(user='root', password='root',
host='localhost', port='8889',
database='Kiva')
cursor = cnx.cursor()
# loading team data
with open('team.json') as data_file:
teamData = json.load(data_file)
data_file.close()
Team_TABLE = '''
CREATE TABLE TEAM
( category VARCHAR (50),
description VARCHAR(1000),
id INTEGER(20),
loan_because VARCHAR(8000),
loan_count INTEGER(100),
loaned_amount INTEGER(100),
member_count INTEGER(100),
membership_type VARCHAR(24),
name VARCHAR(150),
shortname VARCHAR(100),
team_since DATETIME,
website_url VARCHAR(250),
whereabouts VARCHAR(200) ,
CONSTRAINT PK
PRIMARY KEY(id)
);'''
cursor.execute(Team_TABLE)
ref = len(teamData)
# insert team to table
for team in teamData:
# team = teamData['2']
insertTeam = []
insertTeam.append(teamData[team][0]['category'])
try:
insertTeam.append(teamData[team][0]['description'])
except:
insertTeam.append(None)
insertTeam.append(teamData[team][0]['id'])
try:
insertTeam.append(teamData[team][0]['loan_because'])
except:
insertTeam.append(None)
insertTeam.append(teamData[team][0]['loan_count'])
insertTeam.append(teamData[team][0]['loaned_amount'])
insertTeam.append(teamData[team][0]['member_count'])
insertTeam.append(teamData[team][0]['membership_type'])
insertTeam.append(teamData[team][0]['name'])
insertTeam.append(teamData[team][0]['shortname'])
insertTeam.append(teamData[team][0]['team_since'][:10])
try:
insertTeam.append(teamData[team][0]['website_url'])
except:
insertTeam.append(None)
try:
insertTeam.append(teamData[team][0]['whereabouts'])
except:
insertTeam.append(None)
try:
cursor.execute('''INSERT INTO `Kiva`.`TEAM`
(`category`,`description`,`id`,`loan_because`,`loan_count`,`loaned_amount`,`member_count`,`membership_type`,
`name`,`shortname`,`team_since`,`website_url`,`whereabouts`)
VALUES
(%s ,%s, %s,%s ,%s ,%s ,%s ,%s ,%s ,%s,%s,%s,%s);''' ,(insertTeam))
except:
continue
cnx.commit()
cnx.close()