I think the best option to use database is using within context manager. It solves a lot of issues like closing connection: def count_table(): with sqlite3.connect(DB_NAME) as db: num_of_rows = db.execute(SQL_COUNT).fetchall() return num_of_rows[0]
Great use of combining sql with python however if i was a beginner to coding i may be confused by the different syntaxes maybe explain that a bit better we all ought to learn otherwise awesome video!
Bro I love you I am learning java in school and python myself private cause I prefer it by far and here I learn something for both nice ( I focus on python )
The best option is to use context manager like you can use with file: def create_table(): with sqlite3.connect(DB_NAME) as db: db.execute(SQL_CREATE_TABLE) You do not have to remember to close connection.
19:50 couldnt we just pass one tuple instead of 4 placeholders? also cant we instead of person make an class above which takes a name and puts it in the corresponding "person" ?
I get this error. Can you help? line 15, in load_person self.cursor.execute("SELECT * FROM persons WHERE id = {}".format(id_number)) AttributeError: 'Person' object has no attribute 'cursor'
i kept getting this type of error, can anybody help me with this? line 25, in p1.load_person(1) AttributeError: 'Person' object has no attribute 'load_person'
I think the best option to use database is using within context manager. It solves a lot of issues like closing connection:
def count_table():
with sqlite3.connect(DB_NAME) as db:
num_of_rows = db.execute(SQL_COUNT).fetchall()
return num_of_rows[0]
in real world, you would be using something like Pandas or spark dataframes
The tutorial was so good I will forgive you for using notepad as an IDE.
That isn’t notepad
@@bonsai0076yes you are right its python idle
a
Great use of combining sql with python however if i was a beginner to coding i may be confused by the different syntaxes maybe explain that a bit better we all ought to learn otherwise awesome video!
yeah i`m currently a beginner to coding and i confused by different syntaxes
Thank you for leaving the mistakes in there.
Hi .l have question. I go to the third step, but it gives me an error(error=databbase is locked).pleas help me🙏🏻
Definitively I love these tutorial series!
Bro I love you I am learning java in school and python myself private cause I prefer it by far and here I learn something for both nice ( I focus on python )
The sqlite3 script is very very nice. Could you tell me where to find it in the description. Your video scripts are cool
you should make a discordpy DB tutorial for like warnings and or levels and etc
Hi Bro, could you tell us more about DB and OOP relations?
Thank you for these tutorials
why does cursor needs to be defined in class?
Do we have the same process in creating database tables in oracle and sql?
hi bro.
if you open connection database in __init__ function, how to close the connection?
The best option is to use context manager like you can use with file:
def create_table():
with sqlite3.connect(DB_NAME) as db:
db.execute(SQL_CREATE_TABLE)
You do not have to remember to close connection.
thanks so much my teacher failed to let me know that sql and pyhton have two different syntaxs i was very confused...
import sqlite3
class Person:
def __init__(self, usr_id, name, lastname, age) -> None:
self.usr_id = usr_id
self.name = name
self.lastname = lastname
self.age = age
self.connection = sqlite3.connect('python_sql.db') # generates db file in dir.
self.cursor = self.connection.cursor() # rm db and execute again if many vals appear.
def load_person(self, usr_id):
self.cursor.execute(f"""
SELECT * FROM persons
WHERE id = {usr_id}
""")
results = self.cursor.fetchone()
self.usr_id = usr_id
self.name = results[1]
self.lastname = results[2]
self.age = results[3]
connection = sqlite3.connect('mydata.db') # generates db file in dir.
cursor = connection.cursor() # rm db and execute again if many vals appear.
#SQL commands / code
cursor.execute("""
CREATE TABLE IF NOT EXISTS persons (
id INTEGER PRIMARY_KEY,
first_name TEXT,
last_name TEXT,
age INTEGER
)
""")
#SQL input values
cursor.execute("""
INSERT INTO persons VALUES
(1, 'Paul', 'Smith', 24),
(2, 'Mark', 'Murdocc', 42),
(3, 'Ana', 'Smith', 34)
""")
#SQL data fetching
cursor.execute("""
SELECT * FROM persons
WHERE last_name = 'Smith'
""")
#SQL data display
rows = cursor.fetchall()
print(rows)
connection.commit() # apply changes.
connection.close() # close the connection.
19:50 couldnt we just pass one tuple instead of 4 placeholders? also cant we instead of person make an class above which takes a name and puts it in the corresponding "person" ?
were you able to do that? im working through this right now
@@nathanhammond523 I didn't check that tbh I was outside rn and had to pause mid, mind adding me on discord? I am trying it rn
TheRealChicken#5230
look at text cursor on 1:56
did you notice something?
no
@@gusic4529 it looks bo in shinchan🤧🤧🤧
You too think in a functional way in an objective world; my friend.
I get this error. Can you help? line 15, in load_person
self.cursor.execute("SELECT * FROM persons WHERE id = {}".format(id_number))
AttributeError: 'Person' object has no attribute 'cursor'
I needed up adding. self.connection = sqlite3.connect('mydata.db')
i kept getting this type of error, can anybody help me with this?
line 25, in
p1.load_person(1)
AttributeError: 'Person' object has no attribute 'load_person'
I already have the save "mydatebase.db" that supposed to get the attribute of person 1 who is paul, yet i still getting that type of error
Awesome bro💕
Thank you brother! Appreciate it! :)
Its a good video but
bro sometimes your video becomes blurr
Nice Video
Thank you brother! :)
@@NeuralNineI appreciate Your Work
@@karanjoshi8202 that means a lot to me! :)
your outro music is damn loud