-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_creation.py
77 lines (65 loc) · 2.56 KB
/
database_creation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import pandas as pd
import csv
import mysql.connector as mysql
from mysql.connector import Error
moviedata = pd.read_csv('dataset.csv', index_col=False, delimiter=',', quotechar='"',quoting=csv.QUOTE_ALL)
# creates connection to the machine and creates there a Database called movie_rec
try:
mydb = mysql.connect(
host="localhost",
username="root",
password="",
)
if mydb.is_connected():
create_db_query = "CREATE DATABASE movie_rec"
with mydb.cursor() as mycursor:
mycursor.execute(create_db_query)
except Error as e:
print("Error while creating DB", e)
# connects to the movie_rec db created above and creates a new table
try:
mydb = mysql.connect(
host="localhost",
username="root",
password="",
database="movie_rec"
)
if mydb.is_connected():
mycursor = mydb.cursor()
mycursor.execute("select database();")
record = mycursor.fetchone()
print("You are connected to database: ", record)
mycursor.execute('DROP TABLE IF EXISTS movies;')
print('Creating table.....')
mycursor.execute("CREATE TABLE movies(id INTEGER PRIMARY KEY NOT NULL,"
"genres TEXT,"
"plot TEXT,"
"release_date YEAR NOT NULL,"
"ratings FLOAT NOT NULL,"
"title TEXT NOT NULL)")
print('Table is created....')
# loop through the data frame
for i, row in moviedata.fillna(-1).iterrows():
sql = "INSERT INTO movie_rec.movies VALUES (%s,%s,%s,%s,%s,%s)"
mycursor.execute(sql, tuple(row))
print("Record inserted")
# the connection is not auto committed by default, so we must commit to save the changes
mydb.commit()
select_movies_query = """
SELECT genres,plot,release_date,ratings,title
FROM movies
WHERE genres = "Action"
AND release_date= 2013
AND ratings > 7 AND ratings < 8
"""
mycursor.execute(select_movies_query)
print('selecting.....')
print('selection is made....')
for movie in mycursor.fetchall():
print(movie)
# the connection is not auto committed by default, so we must commit to save the changes
mydb.commit()
except Error as e:
print("Error while connecting to MySQL", e)
# closing the database connection
# mydb.close()