diff --git a/Database_Programming.py/__pycache__/sqlite3.cpython-39.pyc b/Database_Programming.py/__pycache__/sqlite3.cpython-39.pyc new file mode 100644 index 0000000..fc0ef7e Binary files /dev/null and b/Database_Programming.py/__pycache__/sqlite3.cpython-39.pyc differ diff --git a/Database_Programming.py/sqlite.py b/Database_Programming.py/sqlite.py new file mode 100644 index 0000000..eba248a --- /dev/null +++ b/Database_Programming.py/sqlite.py @@ -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() + diff --git a/Database_Programming.py/sqlite1.py b/Database_Programming.py/sqlite1.py new file mode 100644 index 0000000..e9ebebf --- /dev/null +++ b/Database_Programming.py/sqlite1.py @@ -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() diff --git a/Database_Programming.py/sqlite2.py b/Database_Programming.py/sqlite2.py new file mode 100644 index 0000000..1286bdc --- /dev/null +++ b/Database_Programming.py/sqlite2.py @@ -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() + + diff --git a/Database_Programming.py/sqlite3.py b/Database_Programming.py/sqlite3.py new file mode 100644 index 0000000..a2eb026 --- /dev/null +++ b/Database_Programming.py/sqlite3.py @@ -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() + diff --git a/mydata.db b/mydata.db new file mode 100644 index 0000000..9da5d4b Binary files /dev/null and b/mydata.db differ