File tree Expand file tree Collapse file tree 4 files changed +80
-4
lines changed
Avancado/basic-sqlalchemy-mysql Expand file tree Collapse file tree 4 files changed +80
-4
lines changed Original file line number Diff line number Diff line change
1
+ # Author: Jonatas Freitas (https://github.com/jonatasfreitasv)
2
+ # Código do artigo
3
+ # https://jonatasfreiitas.wordpress.com/2015/02/01/python-3-x-sqlalchemy-orm-mysql/
4
+
5
+ from sqlalchemy import create_engine
6
+ from sqlalchemy .orm import sessionmaker
7
+
8
+
9
+ class Connection :
10
+
11
+ def session ():
12
+
13
+ connection_string = 'mysql+pymysql://%s:%s@%s:%s/%s' % (
14
+ "db_user" ,
15
+ "db_pass" ,
16
+ "db_host" ,
17
+ "db_port" ,
18
+ "db_name"
19
+ )
20
+
21
+ # echo = True, ativa debug
22
+ engine = create_engine (connection_string , echo = False )
23
+ Session = sessionmaker (bind = engine )
24
+
25
+ return Session ()
Original file line number Diff line number Diff line change
1
+ # Author: Jonatas Freitas (https://github.com/jonatasfreitasv)
2
+ # Código do artigo
3
+ # https://jonatasfreiitas.wordpress.com/2015/02/01/python-3-x-sqlalchemy-orm-mysql/
4
+
5
+ from connect import Connection
6
+ from users import Users
7
+
8
+ connection = Connection .session ()
9
+
10
+ # insert
11
+ new_user = Users ('David Gilmour' , 'pinkfloyd' , '77@77' )
12
+ connection .add (new_user )
13
+ connection .commit ()
14
+
15
+ # select
16
+ user = connection .query (Users ).filter_by (id = 1 ).first ()
17
+ print (user )
18
+
19
+ # update
20
+ user .name = "Richard Wright"
21
+ connection .commit ()
22
+ print (user )
23
+
24
+ # delete
25
+ connection .delete (user )
26
+ connection .commit ()
Original file line number Diff line number Diff line change
1
+ # Author: Jonatas Freitas (https://github.com/jonatasfreitasv)
2
+ # Código do artigo
3
+ # https://jonatasfreiitas.wordpress.com/2015/02/01/python-3-x-sqlalchemy-orm-mysql/
4
+
5
+ from sqlalchemy .ext .declarative import declarative_base
6
+ from sqlalchemy import Column , Integer , String
7
+
8
+ Base = declarative_base ()
9
+
10
+
11
+ class Users (Base ):
12
+ __tablename__ = 'users'
13
+
14
+ id = Column (Integer , primary_key = True )
15
+ name = Column (String (77 ))
16
+ login = Column (String (77 ))
17
+ password = Column (String (77 ))
18
+
19
+ def __init__ (self , name , login , password ):
20
+ self .name = name
21
+ self .login = login
22
+ self .password = password
23
+
24
+ def __repr__ (self ):
25
+ return "<users(%s, %s, %s)>" % (
26
+ self .name , self .login , self .password )
Original file line number Diff line number Diff line change @@ -6,9 +6,8 @@ Repositório para testes, estudos e tutoriais.
6
6
** Iniciantes** - Códigos simples para explicar programação para iniciantes
7
7
* config-file ` Tutorial Arquivo de Configuração com Python -> ` http://goo.gl/l8tDkC
8
8
9
- ** Intermediario** - Códigos simples para explicar programação para iniciantes
9
+ ** Intermediario** - Códigos de nível intermediário
10
10
* basic-unit-test ` Python + Unit Test (Testes Unitários) -> ` http://goo.gl/O3IaPb
11
11
12
- ** Pymage** - Conjunto de filtros e ferramentas para processamento de imagens
13
-
14
- ** Pyvanced** - Repositório para estudo de algoritmos avançados
12
+ ** Avançado** - Repositório para estudo avançado
13
+ * basic-sqlalchemy-mysql ` SqlAlchemy ORM + MySQL -> ` http://goo.gl/MkJx2a
You can’t perform that action at this time.
0 commit comments