Skip to content

Commit

Permalink
Classes and Tables
Browse files Browse the repository at this point in the history
The algorithm allows us to transfer data from an table to an object or vice versa.
  • Loading branch information
artuguen28 committed Oct 29, 2021
1 parent acf7a44 commit c3e9ef2
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 0 deletions.
Binary file not shown.
23 changes: 23 additions & 0 deletions Database_Programming.py/sqlite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Connecting to SQLite

import sqlite3

# This command creates a new database on your disk.
conn = sqlite3.connect('mydata.db')

# To execute statements:
c = conn.cursor()

# Now we can create our first table:
c.execute("""CREATE TABLE persons (
first_name TEXT,
last_name TEXT,
age INTEGER
)""")

# Committing to our connection:
conn.commit()

# Closing the connection:
conn.close()

16 changes: 16 additions & 0 deletions Database_Programming.py/sqlite1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Inserting values

import sqlite3

conn = sqlite3.connect('mydata.db')
c = conn.cursor()

# These are the 3 values that are being inserting on our table:
c.execute("""INSERT INTO persons VALUES
('John', 'Smith', 25),
('Anna', 'Smith', 30),
('Mike', 'Johnson', 40)""")

conn.commit()

conn.close()
17 changes: 17 additions & 0 deletions Database_Programming.py/sqlite2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Selecting values
import sqlite3

conn = sqlite3.connect('mydata.db')
c = conn.cursor()

c.execute("""SELECT * FROM persons;""")

# We use fetchall() method in order to get our results:
persons = c.fetchall()
for d in range(0, len(persons)):
print(persons[d])

conn.commit()
conn.close()


41 changes: 41 additions & 0 deletions Database_Programming.py/sqlite3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import sqlite3

# Classes and tables

class Person():
def __init__(self, first=None,
last=None, age=None):
self.first = first
self.last = last
self.age = age

def clone_person(self, result):
self.first = result[0]
self.last = result[1]
self.age = result[2]


# From table to object

conn = sqlite3.connect('mydata.db')
c = conn.cursor()

c.execute("""SELECT * FROM persons
WHERE last_name = 'Smith'""")

person1 = Person()
person1.clone_person(c.fetchone())

print(person1.first)
print(person1.last)
print(person1.age)

# From object to table

person2 = Person("Bob", "Davis", 23)
c.execute(f"""INSERT INTO persons VALUES
('{person2.first}', '{person2.last}', '{person2.age}')""")

conn.commit()
conn.close()

Binary file added mydata.db
Binary file not shown.

0 comments on commit c3e9ef2

Please sign in to comment.