SQL &/vs Python

Why ? 🤷

How ? 🤔

DB Connection

import sqlite3
connection = sqlite3.connect('example.db')

cursor = conn.cursor()

# Create table
cursor.execute(
    '''
    CREATE TABLE stocks
         (date text, trans text, symbol text, qty real, price real);
    '''
)

# Insert a row of data
cursor.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14);")

# Save (commit) the changes
connection.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
connection.close()
import sqlite3
connection = sqlite3.connect('example.db')

cursor = conn.cursor()

# Create table
cursor.execute(
    '''
    CREATE TABLE stocks
         (date text, trans text, symbol text, qty real, price real);
    '''
)

# Insert a row of data
cursor.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14);")

# Save (commit) the changes
connection.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
connection.close()
import sqlite3
connection = sqlite3.connect('example.db')

cursor = conn.cursor()
import sqlite3
connection = sqlite3.connect('example.db')

cursor = conn.cursor()

# Create table
cursor.execute(
    '''
    CREATE TABLE stocks
         (date text, trans text, symbol text, qty real, price real);
    '''
)

# Insert a row of data
cursor.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14);")

# Save (commit) the changes
connection.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
connection.close()
import sqlite3
connection = sqlite3.connect('example.db')

cursor = conn.cursor()
import sqlite3
connection = sqlite3.connect('example.db')

cursor = conn.cursor()

# Create table
cursor.execute(
    '''
    CREATE TABLE stocks
         (date text, trans text, symbol text, qty real, price real);
    '''
)

# Insert a row of data
cursor.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14);")

# Save (commit) the changes
connection.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
connection.close()
import sqlite3
connection = sqlite3.connect('example.db')

cursor = conn.cursor()
import sqlite3
connection = sqlite3.connect('example.db')

cursor = conn.cursor()

# Create table
cursor.execute(
    '''
    CREATE TABLE stocks
         (date text, trans text, symbol text, qty real, price real);
    '''
)

# Insert a row of data
cursor.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14);")

# Save (commit) the changes
connection.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
connection.close()
import sqlite3
connection = sqlite3.connect('example.db')

cursor = conn.cursor()
import sqlite3
connection = sqlite3.connect('example.db')

cursor = conn.cursor()

# Create table
cursor.execute(
    '''
    CREATE TABLE stocks
         (date text, trans text, symbol text, qty real, price real);
    '''
)

# Insert a row of data
cursor.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14);")

# Save (commit) the changes
connection.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
connection.close()
import sqlite3
connection = sqlite3.connect('example.db')

cursor = conn.cursor()
import sqlite3
connection = sqlite3.connect('example.db')

cursor = conn.cursor()

# Create table
cursor.execute(
    '''
    CREATE TABLE stocks
         (date text, trans text, symbol text, qty real, price real);
    '''
)

# Insert a row of data
cursor.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14);")

# Save (commit) the changes
connection.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
connection.close()
import sqlite3
connection = sqlite3.connect('example.db')

cursor = conn.cursor()

Fetch Data

import sqlite3
connection = sqlite3.connect('example.db')

cursor = conn.cursor()

cursor.execute(
    """
    CREATE TABLE users
        (id integer primary key autoincrement, full_name varchar(100))
    """
)

cursor.execute(
    """
    INSERT INTO users VALUES (1, 'ivo');
    """
)
cursor.commit()  # save changes

cursor.execute("SELECT id, full_name FROM users;")
users_data = cursor.fetchall()


# Save (commit) the changes
connection.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
connection.close()

DEMO

SQL <3 PYTHON

By Hack Bulgaria

SQL <3 PYTHON

  • 1,092