FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tutorials/Docker/Using_postgresql/sample_code.py at master · Data4Democracy/tutorials · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
Data4Democracy
/
tutorials
Public
Notifications
You must be signed in to change notification settings
Fork
27
Star
86
Code
Issues
7
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tutorials
/
Docker
/
Using_postgresql
/
sample_code.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
56 lines (44 loc) · 1.59 KB
Breadcrumbs
tutorials
/
Docker
/
Using_postgresql
/
sample_code.py
Copy path
File metadata and controls
56 lines (44 loc) · 1.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
json
import
sqlalchemy
from
sqlalchemy
.
sql
import
select
from
sqlalchemy
import
Column
,
Text
connection_string
=
'postgresql://user:pass@postgresql/mydatabase'
db
=
sqlalchemy
.
create_engine
(
connection_string
)
engine
=
db
.
connect
()
meta
=
sqlalchemy
.
MetaData
(
engine
)
meta
.
reflect
(
bind
=
engine
)
# list all tables
# there shouldn't be any... yet!
meta
.
tables
# create table
table
=
sqlalchemy
.
Table
(
"twitterusers"
,
meta
,
Column
(
'screen_name'
,
Text
,
primary_key
=
True
),
Column
(
'last_scraped'
,
Text
),
extend_existing
=
True
)
table
.
create
(
engine
)
# some sample data to be stored
entries
=
[
{
'screen_name'
:
'katie'
,
'last_scraped'
:
'today'
},
{
'screen_name'
:
'hunter'
,
'last_scraped'
:
'yesterday'
},
{
'screen_name'
:
'felix'
,
'last_scraped'
:
'last week'
},
{
'screen_name'
:
'audie'
,
'last_scraped'
:
'last year'
},
]
# Insert data
record
=
sqlalchemy
.
table
(
"twitterusers"
,
Column
(
'screen_name'
,
Text
),
Column
(
'last_scraped'
,
Text
))
for
entry
in
entries
:
statement
=
record
.
insert
().
values
(
screen_name
=
entry
[
'screen_name'
],
last_scraped
=
entry
[
'last_scraped'
],
)
engine
.
execute
(
statement
)
# Updating entries
t
=
table
.
update
().
values
(
last_scraped
=
'guajiro'
).
where
(
table
.
c
.
screen_name
==
'hunter'
)
engine
.
execute
(
t
)
#find_user = table.select().where(table.c.screen_name=='hunter')
# Look up data
table
=
meta
.
tables
[
'twitterusers'
]
res
=
engine
.
execute
(
select
([
table
.
c
.
screen_name
,
table
.
c
.
last_scraped
]))
rows
=
res
.
fetchall
()
Back
|
FazBrowse Home
|
New Git URL