📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 7

2022-09-06 •Youtube

Detailed Notes

Focus for this episode: We start looking at bringing across data.

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 are
continuing our
uh working with
the data sync
as far as a tutorial for both mysql and
really for python since this is a python
script but we're going to be we're doing
a lot of playing around with the sql
now
since last time one of the things that i
wanted to mention
is and you'll see these in the update in
github
i was not closing out cursors
before and that can cause some issues
you can and so you always want to
everywhere you have a
uh an open
like you do here
where you're grabbing a cursor you want
to be able to close that back as well
just to make sure that you're you know
reclaiming your resources
so everywhere that i had that
create missing tables create missing
columns
i would just come in after the fact
and clean up those resources
now this episode i want to get into
uh
i'm going to do it this way i'm going to
get into the data side of this
what we had before
was we're able now to go from source to
destination and we can create tables and
then we can add in any missing columns
and now
we get to deal with
data
this one becomes
a little tricky
because
there's a lot of different things being
run into
and the biggest thing being probably
size of data this is something you're
probably not going to want to run if
you're dealing with millions of records
because it's just going to be
time consuming but
let's sort of dive into this first
and see what we can do so let's start
with
so we're going to call now this sync
rose
and we're going to send it a list of the
tables in the source database
because now they should all be synced up
and then
the two connectors and so
where do we go back here uh
up here sync rows
so we'll go ahead and we'll just open up
the cursors for both of them
really don't need that missing columns
and our query is going to be
select star
from and it's whatever the table is
and so that's going to be and this is
going to be our first part here so let's
just
do it we're just going to go from
scratch sort of on this method function
uh so we're gonna come in here we're
going to do our column query actually
we'll call this our source query now
uh let's call it our data query
and for now uh let's see
and then we wanted to do the uh fetch
all so let's see source rows
equals
uh what do we call that cursor
source
dot
if i spell it right fetch all there we
go
and let's just do this four row
in
source rows
we're just going to print row
print
row and this thing is going to blow us
up because we get a bunch of data
but let's go ahead and run this real
quick uh let's see
that should work
so we're seeing a huge amount of data
come by here
because it's flipping through
in this case it's one of those
tables that has a huge amount of data
here we've got one and we can see
in each one it's sort of flying by
but you can see here where you're
getting some
uh here we go
you're getting each of the columns and
then you're getting some sort of a
converted
value
so
the nice thing is
what i can do
is i can for each row
i can basically do i'm going to do a uh
insert
actually
insert query
and it's not really query we'll do
instead statement
equals
insert into
a table
and
so there's our table
then we're gonna have to give it the
columns
and so our columns are going to be
we're gonna have to come from a describe
which we've done before so we gotta
describe query
we'll just steal that here
there we go
uh from that describe results okay
and actually let's do this
uh insert statement equals that
and then we're going to do four
item
in
uh
what do we call it um
[Music]
description results or describe results
insert
statement
equals insert statement
oops statement
plus
uh so each one we're going to do a let's
do our little tick mark
and
plus the name which is
item
which i believe is zero gives us the
column name
and then
uh tick mark
and then a comma
oh
how do we want to do this
okay i'm gonna do this a little
differently so
uh
for whoops
first equals true
if
first
then i just do that
and
first equals false
else i'm going to do the same thing so
for this i'm going to start it with a
comma
uh let me do a comma
and then let's see so i get all the way
through it and then i'm going to come up
here and i'm going to say
insert statement equals and start
statement
plus i got to close that out
and then i'm gonna do values
whoops
i'll use
oop if i can type right
and i'm gonna give my values
and then i'm going to end up ending it
and i'll end it with a close it with a
semicolon
so then what i need to do
is
um am i in the oh i want to do this
within a row okay so that gives me
the start
so now what i'm going to do is i'm going
to come in here
and i'm going to do present print
oh
uh for each row okay
and
this is going to be a
print
search statement
and what i want to do is not build that
over and over again so this is going to
be uh insert base
because i need this to be
um what i'm going to do is this is going
to be what i'm going to start one for
with for each one and we'll see this in
a second
there we go just insert space
okay so insert base insert base center
surface insert base
and i get that so here insert statement
uh for each one equals
insert
base
plus
uh equals insert bay uh let's do it this
way so for each row
uh let's do this because i know i'm
gonna have to close that out
so
equals that and then he equals insert
statement
to close it out and then for within the
row
i'm going to want to go through
each
uh let's do
four
value in
row
and that's going to look like the oh
let's do this
so here first equals true
i do basically the same thing
except for here
it's going to be
insert statement
insert statement
and this should be value
i'm going to see how this looks
except for i don't need
those i need regular tick marks
because these are going to be values
and not uh names or anything like that
so let's see insert statement i need
that here
and then we're going to see in a second
what do these look like
oh i'm sorry it's not value it's called
row
oh no that's right it is value okay so
there's our value
okay so let's take a look what does
it blew up
uh oh
that needs to be a string
i'm not sure because we're adding this
into our little sql string that we're
building
and so now we're going to see
a ton of insert statements coming in
i'll take a second here
while it's generating all of these
and hopefully it'll finish here in a
second
i probably could get rid of those two
tables and that would have helped it
immensely
okay here we go
so now we can look into like stats
insert into stats he's got
stat id code value value 2 updated and
then the values are going to be
right there
so let's just take those because we
should be able to take those
so let's do
uh
some values and stat id we're going to
take those we're just going to pop over
here
and
oh i did not want to do that crap okay
so if i take those oh no data basics
selected so it needs to be tutorial
and then they all just ran so now
i won't see those show up now let me say
uh owners what were the ones that had a
lot of table uh owners and horses i
think
uh let's do
because i had a couple of these let's
get rid of breeders
horses and
owners
so what i'm going to do here is sync
rows
let's go back to my tables list here
real quick
[Music]
so okay if
row
not equal to
horses
let's do this
horses owners and breeders so let's do
this
and
and
raiders
whoops
that's the owners
so now this should fly through a lot
faster
uh
oops
if we come through here
so we're not going to see at the end
we're not going to see stats or some
values
oops
oh that's why
let's do this it's row zero my mistake
let's do that so it'll be a little more
readable
okay
oh but we do see
some values and stats
oh because we're not checking for
differences we're just pulling all of
the rows over
so that's our first thing is we need to
see does that exist or
do we want to just you know empty the
table
so the dumb thing to do
you know the brute force thing
would be to empty the table and do our
inserts so let's do
first
let's go to
the most simple way to do that
which would be so if we go into here
so before we do them the first thing
we're going to do is we're going to do
this
and we're going to go to the db
destination
and we're going to empty it so we're
going to do
truncate
which i think we can do
i'll make sure i've got that right okay
stats
okay
so i can truncate the table
uh this is going to be the trunk cable
query
and
trunk query
except we don't want to do it on the
source we want to do it on the
destination
so now we're going to empty the table
and then we're going to do is we're
going to come in here
and we're going to execute
here
except for it's going to be our insert
statement
and let's see how this works for us
so we now should see a whole bunch of
the not a whole bunch we should see
these tables start to fill up
so let's see what happens
uh let's see incorrect integer
four
okay so let's see what our statement
looks like
uh print
insert statement
and so we may have an issue that we've
got to look into so here
uh
oh it's a none so we need to change that
so what we do need to do is say uh let's
see
let's do this um
[Music]
let's do curve value equals string value
and
this is going to be curve value
except
let's do it this way
we're going to actually play with this
one a little bit so we're going to say
that plus that
plus that
and this it becomes core value
or
value
so it's going to have that but what
we're going to do is we're going to say
if
curve value equals
none
value equals no
so now
we should see that run through
and
we're still going to incorrect integer
none in here
so we have to look into that but we are
going to be able to see here if we look
at like address
we're now seeing that our numbers have
come across
so the next episode we're going to come
in we're going to chase this little
thing down
because we're having an issue
with that
oh i know what it is
it's going to be we have to do it like
that well
quick fix
um
yeah
so now
we should see this works
and
oh no it's not defined it doesn't like
the null
so oh
so it can't be no it's going to be
we're going to keep it blank
and now let's see what happens
and we're still getting some errors so
we're going to dig into this next time
around because i
don't want to go too long on this one
so we will come back
as always 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 are

29.199

continuing our

31.84

uh working with

33.76

the data sync

35.36

as far as a tutorial for both mysql and

39.28

really for python since this is a python

41.84

script but we're going to be we're doing

44.079

a lot of playing around with the sql

46.879

now

47.68

since last time one of the things that i

49.28

wanted to mention

51.52

is and you'll see these in the update in

54.239

github

56.239

i was not closing out cursors

59.84

before and that can cause some issues

62

you can and so you always want to

64.08

everywhere you have a

66.32

uh an open

67.92

like you do here

69.76

where you're grabbing a cursor you want

72.24

to be able to close that back as well

74.32

just to make sure that you're you know

75.68

reclaiming your resources

78.08

so everywhere that i had that

80

create missing tables create missing

81.759

columns

82.88

i would just come in after the fact

85.6

and clean up those resources

88.4

now this episode i want to get into

91.84

uh

92.64

i'm going to do it this way i'm going to

94

get into the data side of this

96.799

what we had before

98.799

was we're able now to go from source to

101.28

destination and we can create tables and

103.84

then we can add in any missing columns

106.479

and now

107.84

we get to deal with

110.64

data

111.92

this one becomes

114.32

a little tricky

116.159

because

117.119

there's a lot of different things being

118.32

run into

119.759

and the biggest thing being probably

122.24

size of data this is something you're

123.92

probably not going to want to run if

125.04

you're dealing with millions of records

128.08

because it's just going to be

129.84

time consuming but

132.16

let's sort of dive into this first

135.84

and see what we can do so let's start

137.68

with

138.72

so we're going to call now this sync

140.16

rose

141.28

and we're going to send it a list of the

143.04

tables in the source database

145.2

because now they should all be synced up

147.36

and then

148.4

the two connectors and so

151.28

where do we go back here uh

153.36

up here sync rows

156.48

so we'll go ahead and we'll just open up

158.08

the cursors for both of them

160.64

really don't need that missing columns

162.4

and our query is going to be

166.56

select star

168.959

from and it's whatever the table is

172.4

and so that's going to be and this is

174.319

going to be our first part here so let's

176.319

just

179.2

do it we're just going to go from

180.56

scratch sort of on this method function

184.239

uh so we're gonna come in here we're

185.84

going to do our column query actually

187.36

we'll call this our source query now

189.68

uh let's call it our data query

196.159

and for now uh let's see

198.879

and then we wanted to do the uh fetch

201.12

all so let's see source rows

205.76

equals

207.76

uh what do we call that cursor

211.28

source

213.28

dot

216.239

if i spell it right fetch all there we

218.64

go

220.319

and let's just do this four row

223.519

in

225.76

source rows

228.319

we're just going to print row

230.879

print

232.4

row and this thing is going to blow us

234.159

up because we get a bunch of data

236.08

but let's go ahead and run this real

237.2

quick uh let's see

243.2

that should work

245.76

so we're seeing a huge amount of data

247.519

come by here

248.799

because it's flipping through

250.48

in this case it's one of those

252.64

tables that has a huge amount of data

255.439

here we've got one and we can see

258.4

in each one it's sort of flying by

261.84

but you can see here where you're

263.44

getting some

265.44

uh here we go

268.32

you're getting each of the columns and

270.08

then you're getting some sort of a

271.759

converted

273.199

value

274.479

so

275.919

the nice thing is

277.44

what i can do

279.199

is i can for each row

282.08

i can basically do i'm going to do a uh

285.12

insert

286.32

actually

289.52

insert query

292.96

and it's not really query we'll do

294.32

instead statement

296.72

equals

298.639

insert into

305.68

a table

311.12

and

314.08

so there's our table

316.24

then we're gonna have to give it the

317.199

columns

321.36

and so our columns are going to be

324.479

we're gonna have to come from a describe

328.08

which we've done before so we gotta

330.24

describe query

334.4

we'll just steal that here

337.68

there we go

339.52

uh from that describe results okay

344.4

and actually let's do this

347.28

uh insert statement equals that

350.96

and then we're going to do four

352.639

item

354.32

in

355.759

uh

356.88

what do we call it um

358.07

[Music]

360.16

description results or describe results

367.199

insert

368.479

statement

370.24

equals insert statement

373.199

oops statement

374.96

plus

380.639

uh so each one we're going to do a let's

383.52

do our little tick mark

389.039

and

390.96

plus the name which is

393.28

item

395.36

which i believe is zero gives us the

398

column name

401.199

and then

402.56

uh tick mark

406.96

and then a comma

412.16

oh

413.28

how do we want to do this

415.44

okay i'm gonna do this a little

416.479

differently so

419.84

uh

420.88

for whoops

422.72

first equals true

426.96

if

428.319

first

431.199

then i just do that

433.52

and

435.44

first equals false

438.56

else i'm going to do the same thing so

440.4

for this i'm going to start it with a

442.319

comma

444.56

uh let me do a comma

448.84

and then let's see so i get all the way

451.12

through it and then i'm going to come up

452.639

here and i'm going to say

456.319

insert statement equals and start

458.56

statement

461.28

plus i got to close that out

464.72

and then i'm gonna do values

466.72

whoops

468.16

i'll use

472

oop if i can type right

474.56

and i'm gonna give my values

478.4

and then i'm going to end up ending it

480.319

and i'll end it with a close it with a

482.319

semicolon

483.52

so then what i need to do

487.039

is

490.84

um am i in the oh i want to do this

494

within a row okay so that gives me

498.08

the start

499.52

so now what i'm going to do is i'm going

500.8

to come in here

506

and i'm going to do present print

509.44

oh

512.159

uh for each row okay

518.56

and

519.599

this is going to be a

521.68

print

524.32

search statement

527.44

and what i want to do is not build that

529.04

over and over again so this is going to

530.959

be uh insert base

544.16

because i need this to be

547.36

um what i'm going to do is this is going

548.88

to be what i'm going to start one for

550.48

with for each one and we'll see this in

552

a second

553.92

there we go just insert space

556.8

okay so insert base insert base center

558.56

surface insert base

560.32

and i get that so here insert statement

565.12

uh for each one equals

567.279

insert

569.04

base

570.56

plus

574.24

uh equals insert bay uh let's do it this

576.48

way so for each row

584.48

uh let's do this because i know i'm

586.08

gonna have to close that out

589.68

so

590.399

equals that and then he equals insert

592.56

statement

594.88

to close it out and then for within the

597.279

row

598.8

i'm going to want to go through

601.36

each

602.839

uh let's do

605.44

four

608

value in

610.48

row

614.32

and that's going to look like the oh

615.6

let's do this

618.24

so here first equals true

622.079

i do basically the same thing

625.36

except for here

627.68

it's going to be

629.12

insert statement

634.56

insert statement

636.88

and this should be value

639.92

i'm going to see how this looks

644.72

except for i don't need

646.959

those i need regular tick marks

651.519

because these are going to be values

655.04

and not uh names or anything like that

657.2

so let's see insert statement i need

659.36

that here

662.72

and then we're going to see in a second

664.959

what do these look like

672.48

oh i'm sorry it's not value it's called

674.24

row

679.519

oh no that's right it is value okay so

681.839

there's our value

683.839

okay so let's take a look what does

687.6

it blew up

689.76

uh oh

691.36

that needs to be a string

699.44

i'm not sure because we're adding this

701.76

into our little sql string that we're

704.32

building

707.76

and so now we're going to see

714.88

a ton of insert statements coming in

721.68

i'll take a second here

723.76

while it's generating all of these

731.2

and hopefully it'll finish here in a

732.639

second

740.24

i probably could get rid of those two

741.68

tables and that would have helped it

743.12

immensely

744.959

okay here we go

746.24

so now we can look into like stats

750.16

insert into stats he's got

752.8

stat id code value value 2 updated and

755.839

then the values are going to be

757.6

right there

758.72

so let's just take those because we

760.079

should be able to take those

762.56

so let's do

763.839

uh

764.639

some values and stat id we're going to

766.56

take those we're just going to pop over

768.56

here

770.399

and

775.92

oh i did not want to do that crap okay

784.079

so if i take those oh no data basics

786.399

selected so it needs to be tutorial

791.519

and then they all just ran so now

795.44

i won't see those show up now let me say

797.6

uh owners what were the ones that had a

799.36

lot of table uh owners and horses i

802.079

think

804.959

uh let's do

806.48

because i had a couple of these let's

807.839

get rid of breeders

810.56

horses and

812.839

owners

814.72

so what i'm going to do here is sync

816.639

rows

824.48

let's go back to my tables list here

826.24

real quick

828.05

[Music]

832.88

so okay if

835.92

row

837.92

not equal to

839.839

horses

845.279

let's do this

849.44

horses owners and breeders so let's do

851.76

this

852.72

and

854.72

and

858.639

raiders

861.36

whoops

864.639

that's the owners

870.399

so now this should fly through a lot

871.839

faster

872.88

uh

875.12

oops

878.16

if we come through here

880.079

so we're not going to see at the end

881.36

we're not going to see stats or some

882.8

values

887.04

oops

893.36

oh that's why

896.32

let's do this it's row zero my mistake

906.399

let's do that so it'll be a little more

908.639

readable

911.04

okay

912.639

oh but we do see

914.56

some values and stats

918.16

oh because we're not checking for

919.6

differences we're just pulling all of

921.519

the rows over

923.44

so that's our first thing is we need to

925.12

see does that exist or

927.76

do we want to just you know empty the

930.16

table

931.04

so the dumb thing to do

933.04

you know the brute force thing

935.6

would be to empty the table and do our

938.079

inserts so let's do

940.639

first

943.04

let's go to

946.24

the most simple way to do that

948.959

which would be so if we go into here

953.36

so before we do them the first thing

954.639

we're going to do is we're going to do

956.399

this

958.639

and we're going to go to the db

960.48

destination

962.72

and we're going to empty it so we're

963.839

going to do

966.839

truncate

968.56

which i think we can do

971.44

i'll make sure i've got that right okay

974.56

stats

978.079

okay

980.8

so i can truncate the table

983.279

uh this is going to be the trunk cable

985.36

query

988.24

and

989.92

trunk query

994.32

except we don't want to do it on the

995.44

source we want to do it on the

996.56

destination

999.12

so now we're going to empty the table

1000.48

and then we're going to do is we're

1001.279

going to come in here

1003.839

and we're going to execute

1011.68

here

1017.279

except for it's going to be our insert

1018.88

statement

1025.039

and let's see how this works for us

1027.439

so we now should see a whole bunch of

1029.28

the not a whole bunch we should see

1030.64

these tables start to fill up

1033.199

so let's see what happens

1036.079

uh let's see incorrect integer

1041.439

four

1042.799

okay so let's see what our statement

1044.72

looks like

1048.079

uh print

1049.76

insert statement

1053.6

and so we may have an issue that we've

1055.039

got to look into so here

1063.679

uh

1067.039

oh it's a none so we need to change that

1072.08

so what we do need to do is say uh let's

1075.12

see

1080.08

let's do this um

1081.56

[Music]

1085.44

let's do curve value equals string value

1095.36

and

1100.08

this is going to be curve value

1112.08

except

1114.48

let's do it this way

1119.039

we're going to actually play with this

1120.16

one a little bit so we're going to say

1122.24

that plus that

1124.48

plus that

1128.32

and this it becomes core value

1133.919

or

1137.6

value

1140.96

so it's going to have that but what

1142.24

we're going to do is we're going to say

1143.36

if

1145.28

curve value equals

1150.32

none

1154.32

value equals no

1162.24

so now

1165.44

we should see that run through

1180.16

and

1181.12

we're still going to incorrect integer

1183.12

none in here

1189.28

so we have to look into that but we are

1190.88

going to be able to see here if we look

1192.799

at like address

1195.919

we're now seeing that our numbers have

1197.36

come across

1199.44

so the next episode we're going to come

1200.96

in we're going to chase this little

1202.48

thing down

1204.159

because we're having an issue

1206.48

with that

1209.36

oh i know what it is

1213.679

it's going to be we have to do it like

1215.36

that well

1218.4

quick fix

1221.36

um

1222.32

yeah

1224.159

so now

1225.44

we should see this works

1230.159

and

1231.12

oh no it's not defined it doesn't like

1233.039

the null

1244.08

so oh

1246.72

so it can't be no it's going to be

1252.159

we're going to keep it blank

1253.679

and now let's see what happens

1259.52

and we're still getting some errors so

1260.96

we're going to dig into this next time

1262.159

around because i

1263.6

don't want to go too long on this one

1266.159

so we will come back

1268.24

as always go out there and have yourself

1269.919

a great day a great week and we will

1272.32

talk to you

1273.919

next time

1290.88

you