Creating a Database
This is the first stage of a project exploring my reading activity from Goodreads. For the initial part of the project, I obtained three raw data from Goodreads:
I utilized Python to read the data files and create a database in MySQL. You can find the Jupyter Notebook for the data upload here. Next steps are to clean and process the data for exploratory analysis and visualizing.
A snapshot of some of the syntax for creating a table in the database using Python.
# create the activities table
try:
= mysql.connect(host='', database = 'goodreads', user = '', password = '')
conn if conn.is_connected():
= conn.cursor()
cursor "select database();")
cursor.execute(= cursor.fetchone()
record print("You're connect to the database:", record)
'DROP TABLE IF EXISTS activities;')
cursor.execute(print('Creating table...')
# create the new table
'''
cursor.execute( CREATE TABLE activities(
activity_id int primary key not null,
activity_type varchar(255),
activity_desc varchar(255),
product varchar(255),
created_date varchar(255)
)''')
print('activities table is created')
# parse the json data
for i, item in enumerate(activities_list):
= i
activity_id = item.get('activity_type')
activity_type = item.get('activity')
activity_desc = item.get('product')
product = item.get('created_at')
created_date # pull the data into the database
cursor.execute('''
INSERT INTO goodreads.activities (
activity_id,
activity_type,
activity_desc,
product,
created_date
)
VALUES (%s,%s,%s,%s,%s)
''',
(
activity_id,
activity_type,
activity_desc,
product,
created_date,
)
)print("record inserted")
conn.commit()except Error as e:
print("Error while connecting to MySQL", e)