📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 15

2022-10-04 •Youtube

Detailed Notes

Focus for this episode: This episode continues our look at stored procedures and adding those.

This tutorial walks through the creation of a database synchronization tool in Python. It continues our Python and SQL Tutorials with a useful application that provides a deeper look at SQL structures and commands.

Repository For Code: git clone [email protected]:rbroadhead/dbsync.git

Transcript Text
[Music]
well hello and welcome back we're
continuing our database and our sql and
python series of tutorials with these
combos
and doing our database synchronization
script
last time around we started looking at
stored procedures
and we got into some
uh moderately useful queries let's go
ahead and just like we'll walk back
through some of these
so what we did is one of the things
we're looking at is we were getting our
routine names based on
our database name so our database name
is tutorial and we're going to get our
procedures that exist in there and so we
see we've got a whole bunch and this was
one that we created back in the
sql tutorial so there's a whole bunch in
there
then you can see the
uh some of the details related to those
now one of the things we did is uh we
went in to look at the source code
so if we go look at
the one called while loop
then we get to see that it was a pretty
straightforward you know definition
there if we go back and look at the
names one that we know had some
parameters because that was the next
thing is one look at parameters and
actually the i guess we'll start with
that so if we do
and they live in the parameters table so
if i just do
in the information schema parameters
we'll just look at this first
it's just bringing back a little over 2
000 rows and you can see here we have a
catalog
and so we can actually which is the um
oh actually no that's not what i want
it's the we have a specific schema so we
can see here
uh let's see
we can see well i've got a lot of tables
in here let's nail down our schema and
we're going to do
where
specific
if i can spell it
specific schema equals so let's narrow
it down to our database
which is what we're going to need to do
anyways we get into this we're going to
pull everything so now we've got
everything our database
and we can see here specific name so
that's going to be our
each of these that's going to be our
store procedure
so and we're going to do an
specific
whoop
specific
specific
name equals uh in this case like let's
do while loop uh
well we do have one for wildlife but
let's go look like multipliers got three
so let's look at those
so if we look at that
we have a three of them we have three
variables now this has this ordinal
position so it's where does it exist in
the procedure so we definitely want to
do order by
and that's fully spelled out so ordinal
position
just to make sure that they come through
as one two three
and we see here this is actually got a
fair amount of information
so we have
um
we have an identifier basically you know
what is the
uh what is the type which i think is
what we're going to be able to work with
you have a data type but you can do
something here that's a little more
specific
so what i could do
is
uh if it's remember we're doing like our
create procedure
and we're going to call it
like here
the multiplier
i think it's as and then we give it the
parameters so the parameters are going
to be
uh these by parameter name
so it's going to be like uh x
y and z but remember we have to do
whether they're in or out so this is a
this is an n
this is an out
this is an in out
and then we have our data type we're
going to use our data identifier instead
and then we get our source which if we
do for
let's see let's do here if we get our
source
it's pretty simple
and so it looks something like that
let's see what happens if we try to do
that so if we come in here
and we do delimiter
there
and then come back here i want to do
that
and do delimiter
semicolon we'll get back to that let's
see how this works
we may have to tweak we have to jump
back because this may not be exactly
what i did so let's uh
uh where am i going i'm gonna go to this
database and i'm just gonna jump in and
do it this way
because i can
let me do a mysql i'm gonna do use
tutorial
uh actually let's see
let's see do i already have a temp
okay use temp
and now if i do this
i have an error in my sql syntax so it's
not
oh it's not as
so if i do go back to
um
let's go back to
wherever that's at
i think it's here
um
[Music]
maybe i don't have it here
oh so we got a cd sql demo
uh let's see let's do grip
what did i call that i called that
multiplier
let's go find that one there we go
so if i do here
uh let's see so if i do vid14
we're gonna see
that he looks basically like this
so let's get out of that
whoop
there we go
and then if we do my sql
use
temp so let's go look at what this looks
like actually let's go bring it over
here
so that's what we started with let's see
how close were we to what actually
should we do it so we should have create
we can do or replace
procedure
we want to give it we do want to give it
the database name because we won't
necessarily be there
oh we don't need an as
and then we do our
uh
got everything there and then we're good
so let's see if that works
it may not like the size
so we may have to
oops i don't want to make a tutorial i
want to make it oh good i did it wrong
anyways
spelled that wrong so if i come in here
there we go so now i have created that
so this is what we have to do and it's
not too terribly difficult so let's go
look at it
because this is the
this is what i want to execute
and so
let's jump back here
where we were creating it
oh
and let's see it's missing procedure oh
missing procedures
so what we want to create whoops
let's do this maybe he'll do it right
there we go so that's what we want to
create
so what we've gotten from here is we
have our
source results we've already picked
we've you know pitched those out so now
what we want to do is we want to grab
our
let's do print
and it's going to be variables
and what we need here
is it's going to be a little different
because this one's going to be
we'll call it the var query
and we're going to go pick up
let's see what we want from it
so we want
uh we really don't need the ordinal
position because that's going to come in
but we do need
parameter mode
we need
parameter name
and i think what we can do is we can get
away
what we want to do is we're going to
have to find some that are vars so let's
go look at that real quick um
let's do dtd identifier
ident
fire okay
so if we do those
there we go
and so now let's do this where
we just grab all of them because let's
see if we've got
yeah so we can see here so we've got
like you know whatever it is we need to
do so we'll be able to build them just
with these and obviously we need it
where there is not a
parameter name that is missing
so
um
let's see and
so we've got those things oh and
let's do parameter
name is not no
i'm not sure why we would have that
we'll probably have to look at that one
at some point
there we go okay
so we're going to take that
[Music]
and we're going to have a little bit of
so we're going to have to do some work
on this guy because
tutorial
is actually
source name
what
and the specific name is going to be the
proc name
let's see we'll get it to there
and parameter name is not no okay and
[Music]
doing this one somewhere let's see so we
come in we do that
that oh and then we have to close it
there we go
so that's our var query
uh and we'll do
this because we're going to need a
cursor
and so we're going to do var cursor from
the source database
and
then we're gonna execute with our
var query
and then we're gonna do uh four
palm row
in
oops we need results
and that's going to be invar cursor
fetch all
for that in that
and for right now let's just print that
row
and see how it looks
so uh
let's see let's get out of there
it's out of there out of there
python three
main
and so now we're seeing all this stuff
gets split out and we can see our
variables
and i think we'll wrap this one because
the next thing we're doing is going to
actually build that out so now what
we're seeing is we're able to go through
we're able to find
uh
missing uh store actually i don't even
know where we're finding our missing
store procedures we're grabbing the
source we're grabbing the variables so
now we're going to come back next time
and we're going to for each of these
we're going to create this make the call
and see what happens
that being said i think it's a good time
to wrap it up so we will so go out there
and have yourself a great day a great
week and we will talk to you
next time
you
Transcript Segments
0.43

[Music]

27.199

well hello and welcome back we're

28.56

continuing our database and our sql and

32.32

python series of tutorials with these

34.96

combos

36.079

and doing our database synchronization

38.16

script

39.76

last time around we started looking at

41.36

stored procedures

43.04

and we got into some

46.399

uh moderately useful queries let's go

49.28

ahead and just like we'll walk back

50.48

through some of these

52.16

so what we did is one of the things

53.28

we're looking at is we were getting our

55.039

routine names based on

60.879

our database name so our database name

62.719

is tutorial and we're going to get our

64.64

procedures that exist in there and so we

66.799

see we've got a whole bunch and this was

68.96

one that we created back in the

71.439

sql tutorial so there's a whole bunch in

73.04

there

75.04

then you can see the

76.72

uh some of the details related to those

79.84

now one of the things we did is uh we

83.04

went in to look at the source code

87.04

so if we go look at

88.88

the one called while loop

92.72

then we get to see that it was a pretty

94.96

straightforward you know definition

97.119

there if we go back and look at the

100.4

names one that we know had some

102.88

parameters because that was the next

104.4

thing is one look at parameters and

107.28

actually the i guess we'll start with

109.119

that so if we do

111.04

and they live in the parameters table so

113.68

if i just do

117.28

in the information schema parameters

122.479

we'll just look at this first

124.719

it's just bringing back a little over 2

127.04

000 rows and you can see here we have a

129.599

catalog

130.72

and so we can actually which is the um

135.52

oh actually no that's not what i want

137.36

it's the we have a specific schema so we

139.36

can see here

141.44

uh let's see

143.36

we can see well i've got a lot of tables

145.36

in here let's nail down our schema and

147.76

we're going to do

149.52

where

151.12

specific

153.36

if i can spell it

154.84

specific schema equals so let's narrow

157.599

it down to our database

160.8

which is what we're going to need to do

162.08

anyways we get into this we're going to

163.68

pull everything so now we've got

164.8

everything our database

166.879

and we can see here specific name so

168.959

that's going to be our

171.2

each of these that's going to be our

172.48

store procedure

174.4

so and we're going to do an

176.72

specific

178.239

whoop

181.12

specific

182.72

specific

185.519

name equals uh in this case like let's

188.159

do while loop uh

190.159

well we do have one for wildlife but

191.68

let's go look like multipliers got three

193.519

so let's look at those

199.04

so if we look at that

201.04

we have a three of them we have three

202.56

variables now this has this ordinal

204.319

position so it's where does it exist in

206.8

the procedure so we definitely want to

208.319

do order by

212.159

and that's fully spelled out so ordinal

215.12

position

216.799

just to make sure that they come through

218.239

as one two three

219.84

and we see here this is actually got a

221.76

fair amount of information

224.879

so we have

226.4

um

227.28

we have an identifier basically you know

229.599

what is the

231.76

uh what is the type which i think is

233.84

what we're going to be able to work with

235.04

you have a data type but you can do

236.48

something here that's a little more

237.84

specific

239.12

so what i could do

241.599

is

242.72

uh if it's remember we're doing like our

244.879

create procedure

247.76

and we're going to call it

249.599

like here

250.879

the multiplier

252.879

i think it's as and then we give it the

255.04

parameters so the parameters are going

256.479

to be

258.239

uh these by parameter name

261.44

so it's going to be like uh x

264.8

y and z but remember we have to do

267.28

whether they're in or out so this is a

269.84

this is an n

271.759

this is an out

273.68

this is an in out

278.16

and then we have our data type we're

280.16

going to use our data identifier instead

290.479

and then we get our source which if we

292.96

do for

294.96

let's see let's do here if we get our

296.8

source

302.8

it's pretty simple

307.84

and so it looks something like that

310.8

let's see what happens if we try to do

313.28

that so if we come in here

316.08

and we do delimiter

319.6

there

323.12

and then come back here i want to do

325.039

that

326.32

and do delimiter

328.4

semicolon we'll get back to that let's

330.24

see how this works

333.759

we may have to tweak we have to jump

335.84

back because this may not be exactly

337.28

what i did so let's uh

340.4

uh where am i going i'm gonna go to this

342.479

database and i'm just gonna jump in and

345.199

do it this way

346.88

because i can

348.4

let me do a mysql i'm gonna do use

351.12

tutorial

353.52

uh actually let's see

358.96

let's see do i already have a temp

363.52

okay use temp

365.68

and now if i do this

369.039

i have an error in my sql syntax so it's

371.919

not

372.8

oh it's not as

374.319

so if i do go back to

376.8

um

378.479

let's go back to

380.479

wherever that's at

382.72

i think it's here

384.8

um

385.13

[Music]

387.199

maybe i don't have it here

393.12

oh so we got a cd sql demo

398.56

uh let's see let's do grip

401.28

what did i call that i called that

403.6

multiplier

406.479

let's go find that one there we go

408.8

so if i do here

414.639

uh let's see so if i do vid14

419.599

we're gonna see

421.44

that he looks basically like this

425.599

so let's get out of that

427.919

whoop

429.28

there we go

430.479

and then if we do my sql

433.599

use

434.84

temp so let's go look at what this looks

437.44

like actually let's go bring it over

439.039

here

440.479

so that's what we started with let's see

441.919

how close were we to what actually

443.68

should we do it so we should have create

446.08

we can do or replace

449.199

procedure

450.639

we want to give it we do want to give it

452.24

the database name because we won't

453.84

necessarily be there

457.12

oh we don't need an as

459.039

and then we do our

461.68

uh

462.639

got everything there and then we're good

464.88

so let's see if that works

468.88

it may not like the size

472.16

so we may have to

477.36

oops i don't want to make a tutorial i

479.199

want to make it oh good i did it wrong

481.28

anyways

484.56

spelled that wrong so if i come in here

489.199

there we go so now i have created that

491.84

so this is what we have to do and it's

493.759

not too terribly difficult so let's go

495.759

look at it

496.879

because this is the

498.639

this is what i want to execute

502.639

and so

504.96

let's jump back here

506.4

where we were creating it

509.68

oh

511.919

and let's see it's missing procedure oh

514.24

missing procedures

517.44

so what we want to create whoops

520.8

let's do this maybe he'll do it right

522.719

there we go so that's what we want to

524.48

create

526.399

so what we've gotten from here is we

528.32

have our

531.12

source results we've already picked

532.72

we've you know pitched those out so now

535.44

what we want to do is we want to grab

536.72

our

540.24

let's do print

543.36

and it's going to be variables

549.12

and what we need here

551.68

is it's going to be a little different

552.72

because this one's going to be

555.36

we'll call it the var query

559.2

and we're going to go pick up

564.399

let's see what we want from it

568.48

so we want

570

uh we really don't need the ordinal

571.2

position because that's going to come in

572.399

but we do need

573.92

parameter mode

579.04

we need

582.56

parameter name

588

and i think what we can do is we can get

589.68

away

590.64

what we want to do is we're going to

591.519

have to find some that are vars so let's

593.12

go look at that real quick um

596.24

let's do dtd identifier

601.12

ident

603.36

fire okay

605.04

so if we do those

606.959

there we go

608.24

and so now let's do this where

612.08

we just grab all of them because let's

613.519

see if we've got

614.8

yeah so we can see here so we've got

616.48

like you know whatever it is we need to

618.72

do so we'll be able to build them just

620.88

with these and obviously we need it

623.12

where there is not a

625.68

parameter name that is missing

628.8

so

629.839

um

632.32

let's see and

634.56

so we've got those things oh and

638.16

let's do parameter

642.32

name is not no

646.24

i'm not sure why we would have that

647.44

we'll probably have to look at that one

648.64

at some point

649.92

there we go okay

653.519

so we're going to take that

654.92

[Music]

657.44

and we're going to have a little bit of

658.8

so we're going to have to do some work

660.88

on this guy because

663.12

tutorial

664.399

is actually

667.12

source name

670.8

what

677.36

and the specific name is going to be the

679.68

proc name

686.64

let's see we'll get it to there

693.68

and parameter name is not no okay and

696.78

[Music]

698.88

doing this one somewhere let's see so we

700.8

come in we do that

704.88

that oh and then we have to close it

708

there we go

710.24

so that's our var query

712.56

uh and we'll do

714.839

this because we're going to need a

717.519

cursor

723.68

and so we're going to do var cursor from

725.92

the source database

728

and

728.839

then we're gonna execute with our

733.2

var query

736.639

and then we're gonna do uh four

740.72

palm row

742.32

in

745.36

oops we need results

756.079

and that's going to be invar cursor

758.48

fetch all

762.399

for that in that

765.76

and for right now let's just print that

767.839

row

771.12

and see how it looks

773.92

so uh

775.36

let's see let's get out of there

777.44

it's out of there out of there

779.92

python three

781.839

main

784.88

and so now we're seeing all this stuff

786.639

gets split out and we can see our

788.24

variables

789.68

and i think we'll wrap this one because

791.76

the next thing we're doing is going to

792.8

actually build that out so now what

794.079

we're seeing is we're able to go through

796.16

we're able to find

797.6

uh

798.839

missing uh store actually i don't even

800.959

know where we're finding our missing

802.639

store procedures we're grabbing the

804.639

source we're grabbing the variables so

807.279

now we're going to come back next time

809.44

and we're going to for each of these

810.8

we're going to create this make the call

813.519

and see what happens

815.279

that being said i think it's a good time

816.72

to wrap it up so we will so go out there

818.72

and have yourself a great day a great

820.56

week and we will talk to you

823.2

next time

840.639

you