Python Intermediate Tutorial #8 - Database Programming

Поделиться
HTML-код
  • Опубликовано: 14 окт 2024

Комментарии • 42

  • @hovardlee
    @hovardlee 3 года назад +21

    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]

    • @dsinghr
      @dsinghr 5 месяцев назад

      in real world, you would be using something like Pandas or spark dataframes

  • @emonymph6911
    @emonymph6911 Год назад +21

    The tutorial was so good I will forgive you for using notepad as an IDE.

    • @bonsai0076
      @bonsai0076 5 месяцев назад +2

      That isn’t notepad

    • @malikkhan4823
      @malikkhan4823 5 месяцев назад +3

      ​@@bonsai0076yes you are right its python idle

    • @blocksnmusket
      @blocksnmusket Месяц назад

      a

  • @Radical9535
    @Radical9535 Год назад +11

    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!

    • @rmediting3349
      @rmediting3349 Год назад +2

      yeah i`m currently a beginner to coding and i confused by different syntaxes

  • @Machtyn
    @Machtyn Год назад +3

    Thank you for leaving the mistakes in there.

  • @fatemehkhalatbari536
    @fatemehkhalatbari536 Год назад +3

    Hi .l have question. I go to the third step, but it gives me an error(error=databbase is locked).pleas help me🙏🏻

  • @jrgomez
    @jrgomez Год назад +3

    Definitively I love these tutorial series!

  • @therealchicken7151
    @therealchicken7151 Год назад +5

    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 )

  • @VíctorAlonsoGarcía-x4h
    @VíctorAlonsoGarcía-x4h Год назад +1

    The sqlite3 script is very very nice. Could you tell me where to find it in the description. Your video scripts are cool

  • @alexw3003
    @alexw3003 3 года назад +2

    you should make a discordpy DB tutorial for like warnings and or levels and etc

  • @TechMarketer
    @TechMarketer 3 года назад +2

    Hi Bro, could you tell us more about DB and OOP relations?

  • @TobiasLange-n5c
    @TobiasLange-n5c 3 месяца назад

    Thank you for these tutorials

  • @rohitdeosthale3604
    @rohitdeosthale3604 Год назад +1

    why does cursor needs to be defined in class?

  • @juniorizcortes6370
    @juniorizcortes6370 2 года назад +1

    Do we have the same process in creating database tables in oracle and sql?

  • @adhilnovandri
    @adhilnovandri 3 года назад +2

    hi bro.
    if you open connection database in __init__ function, how to close the connection?

    • @hovardlee
      @hovardlee 3 года назад +2

      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.

  • @sebastianquesada3134
    @sebastianquesada3134 5 месяцев назад

    thanks so much my teacher failed to let me know that sql and pyhton have two different syntaxs i was very confused...

  • @kvelez
    @kvelez 11 месяцев назад +1

    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.

  • @therealchicken7151
    @therealchicken7151 Год назад +1

    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" ?

    • @nathanhammond523
      @nathanhammond523 Год назад

      were you able to do that? im working through this right now

    • @therealchicken7151
      @therealchicken7151 Год назад

      @@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

    • @therealchicken7151
      @therealchicken7151 Год назад

      TheRealChicken#5230

  • @vaishnavikhandare
    @vaishnavikhandare Год назад +1

    look at text cursor on 1:56
    did you notice something?

  • @annog6673
    @annog6673 2 года назад +1

    You too think in a functional way in an objective world; my friend.

  • @davidfalk99
    @davidfalk99 2 года назад +2

    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'

    • @davidfalk99
      @davidfalk99 2 года назад

      I needed up adding. self.connection = sqlite3.connect('mydata.db')

  • @jhonreylabanon5843
    @jhonreylabanon5843 7 месяцев назад

    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'

    • @jhonreylabanon5843
      @jhonreylabanon5843 7 месяцев назад

      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

  • @aadimanchekar1032
    @aadimanchekar1032 4 года назад +2

    Awesome bro💕

    • @NeuralNine
      @NeuralNine  4 года назад

      Thank you brother! Appreciate it! :)

  • @jinayjain7664
    @jinayjain7664 4 года назад +1

    Its a good video but
    bro sometimes your video becomes blurr

  • @karanjoshi8202
    @karanjoshi8202 4 года назад +1

    Nice Video

    • @NeuralNine
      @NeuralNine  4 года назад +1

      Thank you brother! :)

    • @karanjoshi8202
      @karanjoshi8202 4 года назад +1

      @@NeuralNineI appreciate Your Work

    • @NeuralNine
      @NeuralNine  4 года назад +1

      @@karanjoshi8202 that means a lot to me! :)

  • @hardani.ismunabil
    @hardani.ismunabil 3 года назад +1

    your outro music is damn loud