-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The algorithm allows us to transfer data from an table to an object or vice versa.
- Loading branch information
1 parent
acf7a44
commit c3e9ef2
Showing
6 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|