Doing My Advanced Python Database Operations Module Because I Want All Courses To Be Over
Doing My Advanced Python Database Operations Module Because I Want All Courses To Be Over
I would be happy to never look at another course again in my life after these last two modules. But I am loving them! They are awesome.
I am learning how to execute SQL commands using a cursor object. I slept in the day so I am awake now anyway. I still have to finish making my current batch of Christmas cards. I have about 12 more to go. Then I will get some card and make a couple more when my new stickers arrive later this week. I LOVE making cards for people.
I am practising using Python commands to inject data into tables
Here is some code that I have written:
from start import helper
helper()
import sqlite3
con = sqlite3.connect("titanic.db")
curs = con.cursor()
# Create table named new_table
curs.execute("""CREATE TABLE new_table (
name TEXT,
age INTEGER,
username TEXT,
pay_rate REAL
)""")
# Insert row of values into new_table
curs.execute("""INSERT INTO new_table VALUES ('Bob Peterson', 34, 'bob1234', 40.00)""")
It actually feels really good to be doing this.
I am really strong. I am really powerful. I am taking back control over my life.
Thank you...
This is so easy. It is just combining all of my existing SQL knowledge with Python please. Thanks!
Inserting Multiple Rows
EZ mode.
Using .executemany()
Here is some code:
from start import helper
helper()
import sqlite3
con = sqlite3.connect("titanic.db")
curs = con.cursor()
# Here is the new_rows object
new_rows = [('Anne Smith', 33, 'AS896', 25.00),
('Billy Roberts', 29, 'bill5Rob', 30.00),
('Jason Johnson', 48, 'JasonJ77', 40.00),
('Tim Trunk', 51, 'Timtrunk4', 40.00),
('Sarah Fall',19, 'SFall232', 25.00)
]
# Insert new_rows into the new_table table
curs.executemany("""INSERT INTO new_table VALUES (?, ?, ?, ?)""", new_rows)
Well that was fun, wasn't it...
Retrieving Data
10:33 pm, I think I'm too tired. Well done to me for respecting my limits!
Comments
Post a Comment