📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 1

2022-08-16 •Youtube

Detailed Notes

Focus for this episode: Create simple Python script Connect to a database Retrieve and display information

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 i am into a
slight veering from the sql uh
tutorials and things like that
and want to get into something that's a
little more complicated but we'll still
be
dealing with databases and and how we
can use them and the programming and
things like that
we're gonna focus
uh for this next series of stuff and i
have no idea how long this is gonna take
because
i'm gonna build out an application this
is something that's been a an itch i've
wanted to scratch for a while
years
and it's really it's the idea of a
database synchronizer of some sort uh
you can always do backups and you know
restores and things like that but i want
to do something that is a little bit
more
um
more about the structure
much like some of the tools that are out
there like for example if we've looked
at our django stuff
they have got models and you've got a
way to do migrations and
basically track your database there
and there's some other things i've used
over the years that have similar tools
one that's uh an old library called
torque actually was an excellent one you
would define your database in xml
and then it would it could generate
classes around that
it's something that's a little more
uh targeted and the goal here is to
build a this can be a python application
and you'll see in the notes
i'm going to be using
jetbrains
uh their product called pycharm
but you can also use you can use pretty
much whatever you want if you want to
use like a visual studio code it's got
some pretty good python plugins and
there are definitely other ids out there
this just happens to be the one i'm
going to be using
and our goal is going to be to create
this application that essentially you
can point it at one database and it's
going to look at
differences between that and
a database on another server
so you know whether there are missing
tables missing fields
field types
indexes
you know we're trying to get to all of
it and we'll see how far we can go with
this
because i haven't done it before
i've played around with some of these
concepts and uh you know and this is not
like you would do in a you know in a
django or some of those other tools i
mentioned where you build it from
scratch using this
or some of them do have some sort of a
reverse engineer but you know
essentially where you
build your database you have some way
that you
model it outside of the database itself
for example you know through xml files
or something like that
and then you use those to generate or
update or migrate your database instead
this is going to be point this thing at
database a point it at database b
and let's see if those things exist in
both
and so this is going to be a programming
experiment experiment as well as some of
the other things that we're going to get
into and we're going to be getting into
um
for example we're going to get into
you know pulling describing tables and
generating basically dynamic sql and
some things along those lines
so let's start with uh in this first
little part there is a
new project thing that pycharm will do
and so i went ahead and did that and it
took a little bit because i you know
threw in some libraries and things like
that i do want to have a file that is
going to be
let's do
let's do show notes
and so i'm going to have a little bit of
this
and i'm not sure how i want to do these
but this is going to be uh let's see
it's going to be simple script
and today we're going to do is we're
just going to connect to our database
and i'm just going to connect to one
that's a local kind of database so we'll
start with their they give us this
main.py so we can work with that
uh what they give is just a print you
know it's a hello world it's basically
print hi
and then um
yeah they're gonna call that
we're not gonna worry about that we'll
leave that stuff there uh yeah we'll
leave that there for now so if i run it
uh if i go to
terminal and i do python 3 main
then it's just going to say you know
it's just a hello world okay so now
let's do
so we're going to connect to
the source database
because this is we'll need to
and in order to do this
we're going to need um
i need to import a couple things and so
we'll see what we need to import we're
going to start with import mysql because
we know that's going to be needed
and i'm probably going to import this
because i'm pretty sure that guy's going
to be needed
now the first thing i want to do is i'm
going to define
connect
to db
oh
i don't know why he went over here
and i don't know that i need anything
um
i don't think i do i don't need any
parameters
so
basically what i want to do is
um
let's do my
configuration and this is going to be
this is some python ace type stuff but
give you an idea of what we're doing so
we so for this we're going to have a
user
we're going to have
a password
and you can probably see some of this
stuff if you were to
google connect on via
python to a database
particularly my sql
so database is going to be
there
and we have this thing called
raise on warnings which we just need
that guy's true
uh database is going to be
oh that's a good question
i think i have this oops
i don't have my sequel set up quite
right on this one so let's just go look
over here
so uh
if i want to connect here so my database
is going to be
tutorial
and the host is going to be that guy
and let's see the password gosh i hope i
can remember the password of these so
the username was
and so then
i do
return
my sql
connector
dot connect
and i'm going to give it this structure
most of the stuff you don't need to know
uh because it's just sort of a hey
we have this there so i'm going to light
you know
but if you want to see how python works
there you go so we're going to connect
to it which means we're going to do
uh
dbc our db database connection equals
connect
to db
and then we are going to
grab a cursor
uh cursor equals
see
dot cursor
and our query
is going to be
um let's do something simple
let's look at our
um
did we go to the yeah we said we were
going to be on the tutorial database so
we're going to go here and let's just do
let's describe tables
tables
and then we're going do
cursor.execute the query
i'm gonna do a result oops
results equals
cursor dot fetch all most of this stuff
is not going to matter too much
and then but
um
this gives us a start so you can see so
for row and results
uh print row
let's see what happens here if we try to
run that
then oh so we've got two
okay
i stepped aside for a second i had a
couple of things to install
uh so i went in and had to install uh
the my sequel and in order to do that
whoop
me do this
uh
i do history
grab pip 3.
so i had to do a pip 3 install my sequel
uh actually
the key one was i had to do whoop
i did this uh throw this in the show
notes
so i had to do
my sequel i also had to do
that guy and i think
did i have
i think i had one other no
maybe not
[Music]
gonna do it here okay so i think i'm
good
so i had a pair of uh connectors i had
to install and so now what we're going
to do
is um we've got our pair of imports
i'm connecting as tutor because my
password was test123 connecting my
database i guess i can do this to just
clean that up
get some spaces out of the way
i did have a little typo earlier that i
fixed i've got raison warnings database
host all that goodness
and
uh
if i run it
then we're gonna see here that i come in
here
i come in i'm gonna connect to the
server
and
i go i'm just gonna do show tables
get the results back and for each row
i'm going to print out
the results so each one of these this is
a row so i could also do print
row
zero
i think will give me more of a collec
yeah so that gives me the actual string
so now
i can see tables
and let me go ahead
and
for each of these
well let's see how i want to do okay so
let's do it with that so let's see the
same thing we're going to now do um
three two one and take this basically
the same thing except for now this one
we're gonna do
describe
let's describe address
uh yeah because i'm in the right
database and so i'm going to come
through
and same thing
and now here
i'm gonna see well let's do this
just make it easier
address columns
and so now if we run it we see address
columns so here are the columns so we're
actually you know we're connecting and
we're doing our most basic thing which
is we've got a list of tables
and we've got some columns for the table
so what we're going to do
is we're going to end up adding a
second connection
and this will be connect to database 2
and
we're going to make him on a local host
and i'm not going to have that set up
yet so i'm going to go create that
database and that user but that's where
we're going to be going next
so for now uh this one we came in and
this is going to be there'll be links in
the show notes for this
also you can see you know pretty well
what we've got and actually i'm gonna go
ahead
as part of this and get rid of this
little helpful stuff they've got
uh
let's see
we'll get rid of that and we're going to
come back we're going to start building
some of these things out a little bit
more next time around
and first thing we're going to do is
we're going to do a
table compare
so we've just you know we played around
with it here but this will be our first
one
will be that we're gonna come in we're
gonna start looking at that
but i think that's a good time to wrap
this one up 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.279

well hello and welcome back i am into a

30.48

slight veering from the sql uh

35.36

tutorials and things like that

37.52

and want to get into something that's a

38.96

little more complicated but we'll still

41.76

be

43.12

dealing with databases and and how we

45.68

can use them and the programming and

47.76

things like that

49.52

we're gonna focus

50.96

uh for this next series of stuff and i

53.52

have no idea how long this is gonna take

55.28

because

56.239

i'm gonna build out an application this

58.559

is something that's been a an itch i've

60.559

wanted to scratch for a while

63.199

years

64.239

and it's really it's the idea of a

66.72

database synchronizer of some sort uh

70.4

you can always do backups and you know

72.24

restores and things like that but i want

74

to do something that is a little bit

75.759

more

77.28

um

78.32

more about the structure

80.96

much like some of the tools that are out

83.04

there like for example if we've looked

84.88

at our django stuff

86.4

they have got models and you've got a

88.24

way to do migrations and

90.799

basically track your database there

93.04

and there's some other things i've used

94.479

over the years that have similar tools

98.4

one that's uh an old library called

100.64

torque actually was an excellent one you

102.64

would define your database in xml

106.079

and then it would it could generate

108.479

classes around that

110.479

it's something that's a little more

112.72

uh targeted and the goal here is to

115.36

build a this can be a python application

118.24

and you'll see in the notes

120.399

i'm going to be using

121.68

jetbrains

123.04

uh their product called pycharm

125.52

but you can also use you can use pretty

126.96

much whatever you want if you want to

128.08

use like a visual studio code it's got

130.16

some pretty good python plugins and

132.16

there are definitely other ids out there

135.28

this just happens to be the one i'm

136.48

going to be using

138

and our goal is going to be to create

139.76

this application that essentially you

141.68

can point it at one database and it's

144.239

going to look at

145.68

differences between that and

148.319

a database on another server

150.56

so you know whether there are missing

152.239

tables missing fields

155.44

field types

157.12

indexes

158.4

you know we're trying to get to all of

159.92

it and we'll see how far we can go with

161.599

this

162.48

because i haven't done it before

165.28

i've played around with some of these

166.56

concepts and uh you know and this is not

171.04

like you would do in a you know in a

172.48

django or some of those other tools i

174.08

mentioned where you build it from

176.08

scratch using this

177.84

or some of them do have some sort of a

179.519

reverse engineer but you know

180.8

essentially where you

182.64

build your database you have some way

185.04

that you

186.4

model it outside of the database itself

189.12

for example you know through xml files

191.12

or something like that

193.44

and then you use those to generate or

196.319

update or migrate your database instead

199.2

this is going to be point this thing at

201.36

database a point it at database b

204.319

and let's see if those things exist in

206.4

both

207.36

and so this is going to be a programming

209.68

experiment experiment as well as some of

212.159

the other things that we're going to get

213.36

into and we're going to be getting into

214.879

um

216.239

for example we're going to get into

218.48

you know pulling describing tables and

220.64

generating basically dynamic sql and

222.959

some things along those lines

224.959

so let's start with uh in this first

227.68

little part there is a

231.2

new project thing that pycharm will do

234.4

and so i went ahead and did that and it

236

took a little bit because i you know

237.599

threw in some libraries and things like

239.04

that i do want to have a file that is

241.2

going to be

243.36

let's do

246.879

let's do show notes

251.92

and so i'm going to have a little bit of

253.76

this

255.04

and i'm not sure how i want to do these

256.88

but this is going to be uh let's see

260.639

it's going to be simple script

262.4

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

263.759

just going to connect to our database

271.36

and i'm just going to connect to one

272.8

that's a local kind of database so we'll

275.36

start with their they give us this

276.639

main.py so we can work with that

279.759

uh what they give is just a print you

281.52

know it's a hello world it's basically

282.96

print hi

284.16

and then um

286.16

yeah they're gonna call that

287.84

we're not gonna worry about that we'll

288.88

leave that stuff there uh yeah we'll

290.88

leave that there for now so if i run it

293.44

uh if i go to

294.84

terminal and i do python 3 main

299.6

then it's just going to say you know

300.96

it's just a hello world okay so now

302.8

let's do

306.8

so we're going to connect to

309.039

the source database

311.52

because this is we'll need to

314.4

and in order to do this

316.4

we're going to need um

318.8

i need to import a couple things and so

321.12

we'll see what we need to import we're

322.24

going to start with import mysql because

324.479

we know that's going to be needed

326.8

and i'm probably going to import this

328.4

because i'm pretty sure that guy's going

329.84

to be needed

331.44

now the first thing i want to do is i'm

332.479

going to define

333.759

connect

334.8

to db

339.52

oh

341.199

i don't know why he went over here

346

and i don't know that i need anything

350.72

um

352.88

i don't think i do i don't need any

354.4

parameters

355.52

so

356.319

basically what i want to do is

359.919

um

363.199

let's do my

364.84

configuration and this is going to be

368.24

this is some python ace type stuff but

371.039

give you an idea of what we're doing so

372.319

we so for this we're going to have a

375.28

user

377.52

we're going to have

378.8

a password

383.36

and you can probably see some of this

384.72

stuff if you were to

386.56

google connect on via

389.919

python to a database

392.479

particularly my sql

396.72

so database is going to be

398.8

there

404.639

and we have this thing called

408.16

raise on warnings which we just need

413.36

that guy's true

415.44

uh database is going to be

419.36

oh that's a good question

424.639

i think i have this oops

427.68

i don't have my sequel set up quite

430

right on this one so let's just go look

431.52

over here

432.639

so uh

434.16

if i want to connect here so my database

436.4

is going to be

438.72

tutorial

441.52

and the host is going to be that guy

447.039

and let's see the password gosh i hope i

450.16

can remember the password of these so

451.84

the username was

458.72

and so then

460.8

i do

461.84

return

463.68

my sql

465.599

connector

468.639

dot connect

473.44

and i'm going to give it this structure

475.919

most of the stuff you don't need to know

477.44

uh because it's just sort of a hey

480.72

we have this there so i'm going to light

483.68

you know

484.639

but if you want to see how python works

486.4

there you go so we're going to connect

487.44

to it which means we're going to do

489.44

uh

490.639

dbc our db database connection equals

493.599

connect

494.96

to db

503.68

and then we are going to

505.44

grab a cursor

508.96

uh cursor equals

511.599

see

516.8

dot cursor

524.159

and our query

526

is going to be

527.279

um let's do something simple

530.48

let's look at our

534.16

um

535.04

did we go to the yeah we said we were

536.32

going to be on the tutorial database so

537.76

we're going to go here and let's just do

539.36

let's describe tables

542.88

tables

546.48

and then we're going do

548.44

cursor.execute the query

554.88

i'm gonna do a result oops

561.76

results equals

565.6

cursor dot fetch all most of this stuff

567.36

is not going to matter too much

570.56

and then but

572.88

um

574.08

this gives us a start so you can see so

576

for row and results

578.08

uh print row

581.92

let's see what happens here if we try to

583.6

run that

586

then oh so we've got two

590.08

okay

591.279

i stepped aside for a second i had a

593.04

couple of things to install

594.8

uh so i went in and had to install uh

598.32

the my sequel and in order to do that

601.04

whoop

603.04

me do this

606.16

uh

606.959

i do history

610.24

grab pip 3.

613.6

so i had to do a pip 3 install my sequel

617.2

uh actually

619.279

the key one was i had to do whoop

624.72

i did this uh throw this in the show

626.72

notes

629.839

so i had to do

631.839

my sequel i also had to do

637.519

that guy and i think

640.16

did i have

648

i think i had one other no

650.56

maybe not

651.41

[Music]

655.279

gonna do it here okay so i think i'm

657.2

good

659.519

so i had a pair of uh connectors i had

661.519

to install and so now what we're going

663.04

to do

664.32

is um we've got our pair of imports

667.44

i'm connecting as tutor because my

669.519

password was test123 connecting my

671.2

database i guess i can do this to just

673.2

clean that up

674.88

get some spaces out of the way

676.959

i did have a little typo earlier that i

679.12

fixed i've got raison warnings database

681.36

host all that goodness

684.88

and

686.64

uh

687.44

if i run it

690

then we're gonna see here that i come in

691.6

here

692.88

i come in i'm gonna connect to the

694.16

server

695.44

and

696.88

i go i'm just gonna do show tables

700.24

get the results back and for each row

702

i'm going to print out

703.519

the results so each one of these this is

705.36

a row so i could also do print

708.48

row

709.279

zero

711.68

i think will give me more of a collec

713.44

yeah so that gives me the actual string

715.279

so now

716.399

i can see tables

718.8

and let me go ahead

721.44

and

722.48

for each of these

724.72

well let's see how i want to do okay so

726.24

let's do it with that so let's see the

727.68

same thing we're going to now do um

737.44

three two one and take this basically

740.72

the same thing except for now this one

741.839

we're gonna do

744.88

describe

746.32

let's describe address

750.079

uh yeah because i'm in the right

751.2

database and so i'm going to come

752.48

through

753.68

and same thing

757.36

and now here

759.68

i'm gonna see well let's do this

762.639

just make it easier

766.8

address columns

771.12

and so now if we run it we see address

774.24

columns so here are the columns so we're

776.24

actually you know we're connecting and

778.24

we're doing our most basic thing which

780.48

is we've got a list of tables

783.12

and we've got some columns for the table

785.92

so what we're going to do

787.519

is we're going to end up adding a

790.079

second connection

796.079

and this will be connect to database 2

801.12

and

803.12

we're going to make him on a local host

809.2

and i'm not going to have that set up

810.72

yet so i'm going to go create that

812.48

database and that user but that's where

814.8

we're going to be going next

817.36

so for now uh this one we came in and

820.079

this is going to be there'll be links in

822

the show notes for this

824.72

also you can see you know pretty well

826.399

what we've got and actually i'm gonna go

828.24

ahead

829.6

as part of this and get rid of this

831.44

little helpful stuff they've got

835.68

uh

836.72

let's see

839.199

we'll get rid of that and we're going to

841.04

come back we're going to start building

842.32

some of these things out a little bit

843.6

more next time around

845.279

and first thing we're going to do is

846.8

we're going to do a

848.399

table compare

850.48

so we've just you know we played around

851.76

with it here but this will be our first

853.279

one

854.24

will be that we're gonna come in we're

855.68

gonna start looking at that

857.44

but i think that's a good time to wrap

859.04

this one up so go out there and have

860.959

yourself a great day a great week and we

864.079

will talk to you

866.24

next time

883.839

you