Skip to content

Commit 06b7bd0

Browse files
committed
first commit
0 parents  commit 06b7bd0

7 files changed

+5733
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
env/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 9,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from sqlalchemy import create_engine\n",
10+
"\n",
11+
"# Connecting to MySQL server at localhost using PyMySQL DBAPI \n",
12+
"# engine = create_engine(\"mysql+pymysql://root:pass@localhost/mydb\")\n",
13+
"\n",
14+
"# Connecting to MySQL server at 23.92.23.113 using mysql-python DBAPI \n",
15+
"# engine = create_engine(\"mysql+mysqldb://root:pass@23.92.23.113/mydb\")\n",
16+
"\n",
17+
"# Connecting to PostgreSQL server at localhost using psycopg2 DBAPI \n",
18+
"engine = create_engine(\"postgresql+psycopg2://root:pass@localhost/mydb\")\n",
19+
"\n",
20+
"# Connecting to Oracle server at localhost using cx-Oracle DBAPI\n",
21+
"# engine = create_engine(\"oracle+cx_oracle://root:pass@localhost/mydb\")\n",
22+
"\n",
23+
"# Connecting to MSSQL server at localhost using PyODBC DBAPI\n",
24+
"# engine = create_engine(\"oracle+pyodbc://root:pass@localhost/mydb\")"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": 10,
30+
"metadata": {},
31+
"outputs": [],
32+
"source": [
33+
"# from sqlalchemy import create_engine\n",
34+
"\n",
35+
"# engine = create_engine('sqlite:///sqlite3.db') # using relative path\n",
36+
"# engine = create_engine('sqlite:////path/to/sqlite3.db') # using absolute path"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {},
43+
"outputs": [],
44+
"source": []
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 11,
49+
"metadata": {},
50+
"outputs": [
51+
{
52+
"name": "stdout",
53+
"output_type": "stream",
54+
"text": [
55+
"Engine(postgres+psycopg2://postgres:***@localhost/sqlalchemy_tuts)\n"
56+
]
57+
}
58+
],
59+
"source": [
60+
"from sqlalchemy import create_engine\n",
61+
"\n",
62+
"engine = create_engine(\"postgres+psycopg2://postgres:pass@localhost/sqlalchemy_tuts\")\n",
63+
"engine.connect()\n",
64+
"\n",
65+
"print(engine)"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 12,
71+
"metadata": {},
72+
"outputs": [
73+
{
74+
"name": "stdout",
75+
"output_type": "stream",
76+
"text": [
77+
"2018-07-07 18:39:14,402 INFO sqlalchemy.engine.base.Engine select version()\n",
78+
"2018-07-07 18:39:14,403 INFO sqlalchemy.engine.base.Engine {}\n",
79+
"2018-07-07 18:39:14,408 INFO sqlalchemy.engine.base.Engine select current_schema()\n",
80+
"2018-07-07 18:39:14,410 INFO sqlalchemy.engine.base.Engine {}\n",
81+
"2018-07-07 18:39:14,413 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1\n",
82+
"2018-07-07 18:39:14,416 INFO sqlalchemy.engine.base.Engine {}\n",
83+
"2018-07-07 18:39:14,419 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1\n",
84+
"2018-07-07 18:39:14,421 INFO sqlalchemy.engine.base.Engine {}\n",
85+
"2018-07-07 18:39:14,423 INFO sqlalchemy.engine.base.Engine show standard_conforming_strings\n",
86+
"2018-07-07 18:39:14,424 INFO sqlalchemy.engine.base.Engine {}\n",
87+
"Engine(postgres+psycopg2://postgres:***@localhost/sqlalchemy_tuts)\n"
88+
]
89+
}
90+
],
91+
"source": [
92+
"from sqlalchemy import create_engine\n",
93+
"\n",
94+
"engine = create_engine(\n",
95+
" \"postgres+psycopg2://postgres:pass@localhost/sqlalchemy_tuts\", \n",
96+
" echo=True, pool_size=6, max_overflow=10, encoding='latin1'\n",
97+
")\n",
98+
"\n",
99+
"engine.connect()\n",
100+
"\n",
101+
"print(engine)"
102+
]
103+
}
104+
],
105+
"metadata": {
106+
"kernelspec": {
107+
"display_name": "Python 3",
108+
"language": "python",
109+
"name": "python3"
110+
},
111+
"language_info": {
112+
"codemirror_mode": {
113+
"name": "ipython",
114+
"version": 3
115+
},
116+
"file_extension": ".py",
117+
"mimetype": "text/x-python",
118+
"name": "python",
119+
"nbconvert_exporter": "python",
120+
"pygments_lexer": "ipython3",
121+
"version": "3.5.2"
122+
}
123+
},
124+
"nbformat": 4,
125+
"nbformat_minor": 2
126+
}
+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"posts\n",
13+
"users\n",
14+
"-------------\n",
15+
"users\n",
16+
"posts\n"
17+
]
18+
}
19+
],
20+
"source": [
21+
"from sqlalchemy import create_engine, MetaData, Table, Integer, String, Column, Text, DateTime, Boolean, ForeignKey\n",
22+
"\n",
23+
"metadata = MetaData()\n",
24+
"\n",
25+
"user = Table('users', metadata,\n",
26+
" Column('id', Integer(), primary_key=True),\n",
27+
" Column('user', String(200), nullable=False),\n",
28+
")\n",
29+
"\n",
30+
"posts = Table('posts', metadata,\n",
31+
" Column('id', Integer(), primary_key=True),\n",
32+
" Column('post_title', String(200), nullable=False),\n",
33+
" Column('post_slug', String(200), nullable=False),\n",
34+
" Column('content', Text(), nullable=False),\n",
35+
" Column('user_id', Integer(), ForeignKey(\"users.id\")),\n",
36+
")\n",
37+
"\n",
38+
"for t in metadata.tables:\n",
39+
" print(metadata.tables[t])\n",
40+
"\n",
41+
"print('-------------') \n",
42+
"\n",
43+
"for t in metadata.sorted_tables:\n",
44+
" print(t.name) # print table name"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 2,
50+
"metadata": {},
51+
"outputs": [
52+
{
53+
"name": "stdout",
54+
"output_type": "stream",
55+
"text": [
56+
"['posts.id', 'posts.post_title', 'posts.post_slug', 'posts.content', 'posts.user_id']\n",
57+
"['posts.id', 'posts.post_title', 'posts.post_slug', 'posts.content', 'posts.user_id']\n",
58+
"{ForeignKey('users.id')}\n",
59+
"PrimaryKeyConstraint(Column('id', Integer(), table=<posts>, primary_key=True, nullable=False))\n",
60+
"MetaData(bind=None)\n",
61+
"post_title\n",
62+
"VARCHAR(200)\n"
63+
]
64+
}
65+
],
66+
"source": [
67+
"print(posts.columns) # return a list of columns\n",
68+
"print(posts.c) # same as posts.columns\n",
69+
"print(posts.foreign_keys) # returns a set containing foreign keys on the table\n",
70+
"print(posts.primary_key) # returns the primary key of the column\n",
71+
"print(posts.metadata) # get the MetaData object from the table\n",
72+
"print(posts.columns.post_title.name) # returns the name of the column\n",
73+
"print(posts.columns.post_title.type) # returns the type of the column"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": 3,
79+
"metadata": {},
80+
"outputs": [
81+
{
82+
"name": "stderr",
83+
"output_type": "stream",
84+
"text": [
85+
"/home/pp/project/sqlalchemy/env/lib/python3.5/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use \"pip install psycopg2-binary\" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.\n",
86+
" \"\"\")\n"
87+
]
88+
}
89+
],
90+
"source": [
91+
"from sqlalchemy import create_engine, MetaData, Table, Integer, String, \\\n",
92+
" Column, DateTime, ForeignKey, Numeric, CheckConstraint\n",
93+
"\n",
94+
"from datetime import datetime\n",
95+
"\n",
96+
"metadata = MetaData()\n",
97+
"\n",
98+
"engine = create_engine(\"postgres+psycopg2://postgres:pass@localhost/sqlalchemy_tuts\")\n",
99+
"\n",
100+
"customers = Table('customers', metadata,\n",
101+
" Column('id', Integer(), primary_key=True),\n",
102+
" Column('first_name', String(100), nullable=False),\n",
103+
" Column('last_name', String(100), nullable=False),\n",
104+
" Column('username', String(50), nullable=False),\n",
105+
" Column('email', String(200), nullable=False),\n",
106+
" Column('address', String(200), nullable=False),\n",
107+
" Column('town', String(50), nullable=False),\n",
108+
" Column('created_on', DateTime(), default=datetime.now),\n",
109+
" Column('updated_on', DateTime(), default=datetime.now, onupdate=datetime.now)\n",
110+
")\n",
111+
"\n",
112+
"\n",
113+
"items = Table('items', metadata,\n",
114+
" Column('id', Integer(), primary_key=True),\n",
115+
" Column('name', String(200), nullable=False),\n",
116+
" Column('cost_price', Numeric(10, 2), nullable=False),\n",
117+
" Column('selling_price', Numeric(10, 2), nullable=False),\n",
118+
" Column('quantity', Integer(), nullable=False),\n",
119+
" CheckConstraint('quantity > 0', name='quantity_check')\n",
120+
")\n",
121+
"\n",
122+
"\n",
123+
"orders = Table('orders', metadata,\n",
124+
" Column('id', Integer(), primary_key=True),\n",
125+
" Column('customer_id', ForeignKey('customers.id')),\n",
126+
" Column('date_placed', DateTime(), default=datetime.now),\n",
127+
" Column('date_shipped', DateTime())\n",
128+
")\n",
129+
"\n",
130+
"\n",
131+
"order_lines = Table('order_lines', metadata,\n",
132+
" Column('id', Integer(), primary_key=True),\n",
133+
" Column('order_id', ForeignKey('orders.id')),\n",
134+
" Column('item_id', ForeignKey('items.id')),\n",
135+
" Column('quantity', Integer())\n",
136+
")\n",
137+
"\n",
138+
"\n",
139+
"metadata.create_all(engine)"
140+
]
141+
}
142+
],
143+
"metadata": {
144+
"kernelspec": {
145+
"display_name": "Python 3",
146+
"language": "python",
147+
"name": "python3"
148+
},
149+
"language_info": {
150+
"codemirror_mode": {
151+
"name": "ipython",
152+
"version": 3
153+
},
154+
"file_extension": ".py",
155+
"mimetype": "text/x-python",
156+
"name": "python",
157+
"nbconvert_exporter": "python",
158+
"pygments_lexer": "ipython3",
159+
"version": "3.5.2"
160+
}
161+
},
162+
"nbformat": 4,
163+
"nbformat_minor": 2
164+
}

0 commit comments

Comments
 (0)