INSERT Operation

4 minutes
Share the link to this page
Copied
  Completed
Learn how to insert data into a table.

Transcript

Hello, in this python programming video, we are going to look at inserting data into our table which we created inside of our database. And we correctly created the table in a tutorial previously, that was just all in lining code like I showed you, you could, you know, do it by the researcher, you know, PHP, my admin or whatever the GUI you're using is and you can insert data into like this table of ours as well using insert. Generally you wouldn't you do it in code, because you know, let's say you get some user information, you put that in via the code, and to actually create or insert data is really simple. So we need one of the standard stuff that we got from the form, create a SQL query or the triple quotations or you can do in our multi line for insert into and now we just specify the table name to test table but this is just a table here does does for the database name because we specified up here.

And now we need to specify which field we are going to be inserting it into. So the name, location, gender age, we're going to be using all of them. So just name, the page location, gender and age code bracket and now we need to specify the value so you know what we're actually going to be inserting into these fields. On to the name say my name for those that don't know. I'm going to put Okay, where I live in the United Kingdom. I'm going to put the M mail because what I am I'm sure I'll get some common joke and otherwise and and I trace it out I am recording this video so triple boy Just one day.

So now what we're going to do is we're actually going to use a try catch, block. and beauty of this is let's type it out and explain what we're using. So cursor dot execute. So this is nothing new, the SQL, executing the SQL query that we created a document like that, I won't explain what it is no indentation here, except DB dot. Finally, we just need to not worry, too. Okay.

So the reason we've got this try block is we try to execute our SQL query which inserting data in if something goes wrong, and during this, then what happens is, it rolls the database back so it helps prevent issues and I've seen Committed to EMS. So that would have actually caused an issue. So, yeah, so this is just that if something goes wrong, shouldn't do tested it. So it should be fine to say that. And to confirm that there's no data in there. Currently, if I were to run the module, let's see what we got, we should get no errors, which we don't good sign up for, click Browse.

The data is right here. And obviously, if I were to change this to, and then I'll change it to settings, which is nice name, and I'll leave everything else as it is. So now when the module again, we get, we should have a number Oh, so we've got two rows now, to insert data into a database. To make it more complex by having more fields, more value. Different types of value depends on the datatype of each field, maybe have some sort of conditional statement and get input from the user, but in its core, in essence, this is really all we need for a Insert query might have been more complex in your application, depending on certain scenario, but the insert part will just be this. That's it for this video.

Thanks for watching, and I look forward to seeing you in my next tutorial.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.