Skip to content

Commit 28dde07

Browse files
committed
add orm samples
1 parent 62d8f6e commit 28dde07

File tree

7 files changed

+104
-0
lines changed

7 files changed

+104
-0
lines changed

hello/try_os.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import os
2+
3+
def test_dir():
4+
print("os seprater: " + os.sep)
5+
6+
#current dir
7+
cwd = os.getcwd()
8+
print("cwd: "+ cwd)
9+
10+
#change dir
11+
os.chdir("../")
12+
cwd = os.getcwd()
13+
print("cwd: "+ cwd)
14+
15+
#parent dir
16+
print("parent dir: " + os.path.dirname(cwd))
17+
# list dir , list ->str
18+
print("list dirs:" + ' '.join(os.listdir(cwd)))
19+
20+
print(cwd + "is a dir: " + str(os.path.isdir(cwd)))
21+
print(" .gitignore is a dir: " + str(os.path.isdir('.gitignore')))
22+
23+
os.chdir('hello')
24+
cwd = os.getcwd()
25+
print("-------------")
26+
for file in os.listdir(cwd):
27+
if file.endswith(".py"):
28+
print(file + " is a python file")
29+
print("-------------")
30+
31+
test_dir()
32+
33+

sqlalchemy/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
sqlite3 samples.db
2+
sqlite> .schema
3+
4+
sqlite> select * from users

sqlalchemy/database.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# _*_ coding: utf-8 _*_
2+
import os
3+
from sqlalchemy import create_engine
4+
from sqlalchemy.orm import scoped_session,sessionmaker
5+
from sqlalchemy.ext.declarative import declarative_base
6+
7+
# echo = True,will show generated sql.
8+
# The echo flag is a shortcut to setting up SQLAlchemy logging, which is accomplished via Python’s standard logging module
9+
#the SQLite dialect will interpret instructions to the Python built-in sqlite3 module.
10+
# lazy Connecting, will connect to db will first time it is asked to perform a task
11+
12+
#db engine (sqlite)
13+
database_file = os.path.join(os.path.abspath(os.path.dirname(__file__)),'samples.db')
14+
#engine = create_engine('sqlite:///:memory:' echo=True)
15+
engine = create_engine('sqlite:///' + database_file, echo=True, convert_unicode=True)
16+
17+
#create base class
18+
#Declarative, which allows us to create classes that include directives to describe the actual database table they will be mapped to.
19+
Base = declarative_base()
20+
21+
22+
#session
23+
db_session = scoped_session(sessionmaker(autocommit=False,autoflush=False,bind=engine))
24+
#query
25+
Base.query = db_session.query_property()
26+
27+
28+
def init_db():
29+
#because create a __init__.py in model forlder, we can import it!
30+
from model.user import User
31+
Base.metadata.create_all(bind=engine)

sqlalchemy/model/__init__.py

Whitespace-only changes.

sqlalchemy/model/user.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from sqlalchemy import create_engine,Column,Integer,String
2+
from database import Base
3+
4+
class User(Base):
5+
__tablename__ = 'users'
6+
7+
id = Column(Integer,primary_key=True)
8+
name = Column(String)
9+
fullname = Column(String)
10+
password = Column(String)
11+
12+
# __str__ __repr__ is the same
13+
# will be called when print(user)
14+
def __repr__(self):
15+
return "<user(name = '%s',fullname='%s',password='%s')>" % (name,fullname,password)
16+
17+
def __init__(self,name=None,fullname=None,password=None):
18+
self.name = name
19+
self.fullname = fullname
20+
self.password = password

sqlalchemy/samples.db

8 KB
Binary file not shown.

sqlalchemy/try_orm.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from database import init_db,db_session
2+
from model.user import User
3+
4+
5+
#init_db()
6+
7+
user1 = User("hoge1","1Hoge Jim","pw1")
8+
db_session.add(user1)
9+
db_session.commit()
10+
11+
user2 = User("hoge2","2Hoge Jim","pw2")
12+
user3 = User("hoge3","3Hoge Jim","pw3")
13+
14+
db_session.add(user2)
15+
db_session.add(user3)
16+
db_session.commit()

0 commit comments

Comments
 (0)