In [1]:
%reload_ext sql
In [2]:
%%sql postgresql://superuser:superuserpass@localhost/app
select * from api.todos;
Out[2]:
In [3]:
%sql select 1;
Out[3]:
In [16]:
import os
os.getcwd()
Out[16]:
In [4]:
import anosql
import psycopg2
import sqlite3
# PostgreSQL
conn = psycopg2.connect('postgresql://superuser:superuserpass@localhost/app')
queries = anosql.load_queries('postgres', 'sql/study01.sql')
queries.select1(conn)
Out[4]:
In [5]:
queries.select2(conn)
Out[5]:
In [1]:
!cat sql/study01.sql
In [3]:
!echo "hello"
In [44]:
%config SqlMagic.displaylimit = 5
%sql select * from pg_catalog.pg_tables pt where pt.schemaname = 'pg_catalog';
Out[44]:
In [45]:
%sql select * from pg_catalog.pg_tables pt where pt.schemaname = 'api';
Out[45]:
In [46]:
%sql select * from pg_catalog.pg_views pv where pv.schemaname = 'api';
Out[46]:
In [48]:
%sql select user;
Out[48]:
In [50]:
%sql select '$user';
Out[50]:
In [51]:
%sql select '{Alex,Sonia}'::text[] As name;
Out[51]:
In [52]:
%sql select unnest('{Alex,Sonia}'::text[]) As name;
Out[52]:
In [53]:
%sql SELECT unnest(string_to_array('split.this', '.')) As x;
Out[53]:
In [54]:
%sql SELECT unnest(regexp_matches('100 hats 200', '\d+', 'g'));
Out[54]:
In [106]:
%%sql
CREATE TABLE public.families_j (
doc_id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
profile jsonb
);
select * from public.families_j;
Out[106]:
In [107]:
%sql DROP TABLE public.families_j;
Out[107]:
In [108]:
%sql CREATE SCHEMA study;
In [110]:
%%sql
CREATE TABLE study.families_j (
doc_id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
profile jsonb
);
Out[110]:
In [111]:
%sql select * from study.families_j;
Out[111]:
In [122]:
%%sql
-- multiple rows per insert values
INSERT INTO study.families_j (profile) VALUES
('{"name":"Gomez", "members": [{"member":{"relation":"padre"}}]}')
,('{"name":"Sanchez", "members": [{"member":{"relation":"madre"}}]}')
;
SELECT * FROM study.families_j;
Out[122]:
In [120]:
%sql select profile->'members' from study.families_j;
Out[120]:
In [128]:
%sql select profile->>'members' as name from study.families_j;
Out[128]:
In [129]:
%sql select profile->'name' as name from study.families_j;
Out[129]:
In [130]:
%sql select profile->'members'->'member' from study.families_j;
Out[130]:
In [131]:
%sql select profile->'members'->>'member' from study.families_j;
Out[131]:
In [138]:
%sql select profile->'members'->'member'->>'relation' from study.families_j;
Out[138]:
In [139]:
%sql select profile->'members'->0#>>'{member}' from study.families_j;
Out[139]:
In [143]:
%sql select profile->'members'->0->'member'->'relation' from study.families_j;
Out[143]:
In [133]:
%sql CREATE TABLE study.books ( id integer, data json );
In [134]:
%%sql
INSERT INTO study.books VALUES (1,
'{ "name": "Book the First", "author": { "first_name": "Bob", "last_name": "White" } }');
INSERT INTO study.books VALUES (2,
'{ "name": "Book the Second", "author": { "first_name": "Charles", "last_name": "Xavier" } }');
INSERT INTO study.books VALUES (3,
'{ "name": "Book the Third", "author": { "first_name": "Jim", "last_name": "Brown" } }');
Out[134]:
In [136]:
%sql SELECT id, data->>'name' AS name FROM study.books;
Out[136]:
In [137]:
%sql SELECT id, data->'author'->>'first_name' as author_first_name FROM study.books;
Out[137]:
In [146]:
!head sql/library.sql
In [147]:
!datafiller --size=100 sql/library.sql > sql/library_test_data.sql
In [148]:
!head sql/library_test_data.sql
In [1]:
!ddlgenerator postgresql sql/sample_data01.csv > sql/sample_data01.sql
In [2]:
!head sql/sample_data01.csv
In [3]:
!head sql/sample_data01.sql
In [6]:
%load_ext pgcli.magic
In [8]:
%pgcli postgres://superuser:superuserpass@localhost:5432/library
In [89]:
%%sql
SELECT '2012-03-11 3:10 AM America/Los_Angeles'::timestamptz AS america
, '2012-03-11 3:10 AM'::timestamptz as istanbul1
, '2012-03-11 3:10 AM GMT-3'::timestamptz as istanbul2
, '2012-03-11 3:10'::timestamptz::date as date1
;
Out[89]:
In [86]:
works = %sql SELECT 1,2
In [90]:
%%sql
SELECT
a
, extract('year' from b)
FROM
( VALUES
( '2012-03-11 3:10 AM'::timestamptz
, '1012-03-11 3:10'::timestamptz::date)
) dates(a,b)
;
Out[90]:
In [91]:
%%sql
SELECT
*
FROM
( VALUES
( '2012-03-11 3:10 AM'::timestamptz
, '1012-03-11 3:10'::timestamptz::date)
) dates
;
Out[91]:
In [92]:
%%sql
SELECT
column1
FROM
( VALUES
( '2012-03-11 3:10 AM'::timestamptz
, '1012-03-11 3:10'::timestamptz::date)
) dates
;
Out[92]:
In [93]:
%%sql
SELECT
*
FROM
( VALUES
( '2012-03-11 3:10 AM'::timestamptz
, '1012-03-11 3:10'::timestamptz::date)
) AS dates
;
Out[93]:
In [94]:
%sql select '[2012-04-24, infinity]'::daterange;
Out[94]:
In [4]:
%sql select '1 hour'::interval;
Out[4]:
In [5]:
%sql select now() - interval '1 hour';
Out[5]:
In [6]:
%sql select '1 hour ago'::interval;
Out[6]:
In [7]:
%sql select '1 day ago'::interval;
Out[7]:
In [8]:
%sql select INTERVAL '1' YEAR;
Out[8]:
In [9]:
%sql select INTERVAL '1 year';
Out[9]:
In [17]:
%sql select INTERVAL '@ 1 year';
Out[17]:
In [21]:
@sql select INTERVAL '1-1';
In [23]:
%sql select INTERVAL '1 day 2:03:04' HOUR TO MINUTE;
# drops second field
Out[23]:
In [24]:
%sql select INTERVAL '2:03:04';
Out[24]:
In [25]:
%sql select INTERVAL '-2:03:04';
Out[25]:
In [26]:
%sql select INTERVAL '2:03:04 ago';
Out[26]:
In [27]:
%sql select INTERVAL 'P0Y0M0DT2H3M4S';
Out[27]:
In [ ]: