import sqlite3 from init import init_db db_name = "myLibrary.db" init_db(db_name) with sqlite3.connect(db_name) as conn: cur = conn.cursor() cur.execute('SELECT * FROM author') print("Authors:") rows = cur.fetchall() print(rows) cur.execute('SELECT * FROM book') print("Books:") for row in cur: print(row) cur.execute('INSERT INTO author(name) VALUES("David Rise")') conn.commit() cur.execute('SELECT * FROM author') print("Authors2:") rows = cur.fetchall() print(rows) print("Select:") cur.execute('SELECT * FROM author WHERE id=%i' % 2) print(cur.rowcount) print(cur.fetchone())
#!/usr/bin/env python # import webapp2 import init from models import Task # code to init init.init_db() class MainHandler(webapp2.RequestHandler): def get(self): tasks = Task.query().order(Task.date_created).fetch(20) self.response.headers['Content-Type'] = 'text/plain' for t in tasks: self.response.write("------------------------------------------\n") self.response.write("Task Description: " + t.description + "\n") self.response.write("Date Created: " + str(t.date_created) + "\n") self.response.write("------------------------------------------\n") app = webapp2.WSGIApplication([ ('/', MainHandler) ], debug=True)
import sqlite3 from init import init_db db_name = "myLibrary.db" init_db(db_name) conn = sqlite3.connect(db_name) cur = conn.cursor() cur.execute('SELECT * FROM author') print("Authors:") rows = cur.fetchall() print(rows) cur.execute('SELECT * FROM book') print("Books:") for row in cur: print(row) cur.execute('INSERT INTO author(name) VALUES("Kirk Hammett")') conn.commit() cur.execute('SELECT * FROM author') print("Authors2:") rows = cur.fetchall() print(rows) print("Select1:") cur.execute('SELECT * FROM author WHERE id=%i' % 2) print(cur.fetchone())
from init import init_db init_db()
def main(): """ Main. Init database, create command bus, receive commands and pass to handlers """ # Init commandbus bus = CommandBus() # Init handlers for commands root_handler = RootHandler() new_handler = NewHandler() parent_handler = ParentHandler() child_handler = ChildHandler() read_handler = ReadHandler() update_handler = UpdateHandler() ancestor_handler = AncestorHandler() ancestors_handler = AncestorsHandler() descendants_handler = DescendantsHandler() remove_handler = RemoveHandler() open_handler = OpenHandler() # Route commands to coresponding handlers bus.subscribe(RootCommand, root_handler) bus.subscribe(NewCommand, new_handler) bus.subscribe(ParentCommand, parent_handler) bus.subscribe(ChildCommand, child_handler) bus.subscribe(ReadCommand, read_handler) bus.subscribe(UpdateCommand, update_handler) bus.subscribe(AncestorCommand, ancestor_handler) bus.subscribe(AncestorsCommand, ancestors_handler) bus.subscribe(DescendantsCommand, descendants_handler) bus.subscribe(RemoveCommand, remove_handler) bus.subscribe(OpenCommand, open_handler) # Get transaction mode mode = 'normal' try: mode = sys.argv[1][2:] except IndexError: pass # Read json-object-like commands from the input file commands = read_data(input()) open_command = commands.pop(0) # Check connection with the specified database bus.publish(OpenCommand(open_command)) # init database init_db(open_command, 'init', 'init.sql') # invoke each command for command in commands: with psycopg2.connect(host='localhost', dbname=open_command['open']['database'], user=open_command['open']['user'], password=open_command['open']['password']) \ as db_conn: try: command_name = globals()[ list(command.keys())[0].title() + 'Command'] command_instance = command_name(db_conn, list(command.values())[0], mode) # Handle command bus.publish(command_instance) except KeyError: # Command key should be valid print(json.JSONEncoder().encode({'status': 'ERROR'}))
#!/usr/bin/env python # import webapp2 import init from models import Task # code to init init.init_db() class MainHandler(webapp2.RequestHandler): def get(self): tasks = Task.query().order(Task.date_created).fetch(20) self.response.headers['Content-Type'] = 'text/plain' for t in tasks: self.response.write("------------------------------------------\n") self.response.write("Task Description: " + t.description + "\n") self.response.write("Date Created: " + str(t.date_created) + "\n") self.response.write("------------------------------------------\n") app = webapp2.WSGIApplication([('/', MainHandler)], debug=True)