-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
41 lines (31 loc) · 1.23 KB
/
models.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
"""
This module is for creating data tables in MySQL database.
"""
from peewee import *
import pymysql
import settings as s
# create mysql connection to check if schema exist on MySQL Server with pymysql
conn = pymysql.connect(host=s.HOST, user=s.USER, password=s.PASSWORD)
# check if database exist
exist = conn.cursor().execute("SHOW DATABASES LIKE '{}';".format(s.SCHEMA))
if not exist:
conn.cursor().execute("CREATE DATABASE {}".format(s.SCHEMA))
conn.close()
# Create a MySQL database connector object for Peewee ORM
mysql_db = MySQLDatabase(database=s.SCHEMA, user=s.USER, password=s.PASSWORD, host=s.HOST, port=s.PORT)
class Car(Model):
"""Define Car model for MySQL Database"""
brand = CharField(max_length=255, null=False)
model = CharField(max_length=255, null=False)
year = SmallIntegerField(null=False)
ext_color = CharField(max_length=125, null=False)
int_color = CharField(max_length=125, null=False)
transmission = CharField(max_length=125, null=False)
price = DecimalField(null=False, decimal_places=3)
phone = CharField(max_length=20, null=False)
class Meta:
database = mysql_db
# create table in the database
mysql_db.connect()
mysql_db.create_tables([Car])
mysql_db.close()