fetch

Python and MySQL - Selecting and Getting Data



Sharing buttons:

hey guys over here and welcome back to a

Python and my sequel series in this

video we're gonna be learning how to

select data from our database and

display it to any user you choose so to

do that guys we're gonna be using the

select method that we used back in our

my sequel workbench this one right over

here except we running this command in

Python and if you guessed how you are

right we're using the execute command

that we use for actually pushing data to

our database so let's go start first

thing first guys we're going to go ahead

and say my cursor executes and again and

it's this execute command we're gonna be

running the exact same command we did

over here select start and star

basically means all if you haven't

realized by now select star from

students and all this is gonna do guys

is this it'll give us the entire list of

student data that we have and then to

actually get that data we're gonna say

my result is equal to my cursor not

fetch all and what the fetch all

function does guys it's gonna fetch all

of the rows from the last executed

statement so the last executed statement

would be this right over here my cursor

to fetch all is gonna go ahead and get

all of the rows that were fetched in

this execute statement so now we have my

result is equal to my cursor now fetch

all and our my resolve Ariel will now

have all of our row data so we're just

gonna say for row in my result we're

gonna print out row alright so go ahead

and save that guys again very very

straightforward we're running the select

star from students command getting all

the data and printing it out row by row

so let's go ahead and run this and we

immediately get Rachel Bob Amanda Jacob

AVI Michelle Mike and then Mike again

fantastic so that is how you run a

Select command using Python and get all

the data now let's say you wanted a

specific column when you work with my

sequel databases you're gonna have tons

and tons of different columns so the

question is if you want to do a specific

one it's all dependent on your execute

command so just like a querying database

querying something from a my sequel

database the exact same thing you're

gonna run in your execute come in so

over here we're gonna go ahead and

select age not just all the columns just

select age from students and now what

that's gonna do guys is it's only gonna

return us that one specific row so go

ahead and save that and run this and now

we see twenty two twelve thirty twenty

one eighteen seventy seven seventeen

that basically means that it only got us

the age column so again if you want more

specific data guys make sure you specify

the column you want otherwise the star

will get you all of your data last but

not least guys right now we're using the

fetch all method sometimes when you're

running on Python scripts when you're

getting data from your database you may

just want to see one entry you want to

see hey eyes gonna check one thing make

sure everything looks good to fetch one

entry the way you can do that guys is by

just saying my cursor dot fetch one all

right the fetch one function will only

get you one entry from your execute

command that way you will say memory

space you'll make your code run much

faster if you have a database with

thousands and thousands of rows fetch

one is very beneficial go ahead and save

that guys and run it and you're gonna

see 2222 again is the first row that is

the what the fetch one function does

it'll return the first row of the result

all right fantastic so in this video

guys we understood how to get data from

our database using the Select method

again select star select a specific

column from your table and then either

say fetch all to get all the rows or

fetch one to get a single row thanks so

much for listening guys don't wear the

LifeCam subscribe and I'll see you in

the next video

[Music]