-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathORMdb.py
31 lines (24 loc) · 1007 Bytes
/
ORMdb.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
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
Base = declarative_base()
class User(Base):
#same operation as CREATE TABLE in SQL but ORM
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
username = Column(String)
fullname = Column(String)
email = Column(String)
password = Column(String)
def __init__(self, username, fullname, email, password):
self.username = username
self.fullname = fullname
self.email = email
self.password = password
def __repr__(self):
return "<user(username='%s', fullname='%s' , email='%s' password='%s' )>" % (
self.username, self.fullname, self.email, self.password)
#we specify the path and create engine
path_to_db = "mydatabase.db"
engine = create_engine('sqlite:///' + path_to_db)
Base.metadata.create_all(engine)