[python]Python function passing more than one value

Reference: https://stackoverflow.com/questions/354883/how-do-you-return-multiple-values-in-python

I just realize the python function can pass multiple values through dictionary!

Here’s the code which I tried:

import sqlite3


def connect_db():
    conn = sqlite3.connect("lite.db")
    cur = conn.cursor()
    return {'conn': conn, 'cursor': cur}


def create_table(conn):
    conn['cursor'].execute("CREATE TABLE IF NOT EXISTS store (item TEXT, quantity INTEGER, price REAL)")
    conn['conn'].commit()
    conn['conn'].close()


def insert(conn, item, quantity, price):
    conn['cursor'].execute("INSERT INTO store VALUES(?,?,?)", (item, quantity, price))
    conn['conn'].commit()
    conn['conn'].close()


def view(conn):
    conn['cursor'].execute("SELECT * FROM store")
    rows = conn['cursor'].fetchall()
    conn['conn'].close()
    return rows


if __name__ == '__main__':
    print(view(connect_db()))
Advertisements
This entry was posted in Python, Scripting and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s