Introduction to pysqlite

Slides:



Advertisements
Similar presentations
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Advertisements

PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
Python Crash Course Databases 3 rd year Bachelors V1.0 dd Hour 3.
Stored Procedure Language Stored Procedure Overview Stored Procedure is a function in a shared library accessible to the database server can also write.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
SQLLite and Java CS-328 Dick Steflik. SQLLite Embedded RDBMS ACID Compliant Size – about 257 Kbytes Not a client/server architecture –Accessed via function.
SQLite 1 CS440. What is SQLite?  Open Source Database embedded in Android  SQL syntax  Requires small memory at runtime (250 Kbytes)  Lightweight.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.
What is MySQL? MySQL is a database. The data in MySQL is stored in database objects called tables. A table is a collections of related data entries and.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 35 – Python Outline 35.1 Introduction First Python Program Python Keywords 35.2 Basic.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Web Services Week 8 Aims: –Using web services as front ends to databases Objectives: –Review of relational databases –Connecting to and querying databases.
Stored Procedures, Transactions, and Error-Handling
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
Advanced SQL: Cursors & Stored Procedures
Introduction to Computing Using Python Data Storage and Processing  How many of you have taken IT 240?  Databases and Structured Query Language  Python.
DAT602 Database Application Development Lecture 2 Review of Relational Database.
Introduction to Computing Using Python Data Storage and Processing  Databases and SQL  Python Database Programming  List comprehension and MapReduce.
Text Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1.
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
1 Section 10 - Embedded SQL u Many computer languages allow you to embed SQL statements within the code (e.g. COBOL, PowerBuilder, C++, PL/SQL, etc.) u.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Introduction to Database Programming with Python Gary Stewart
Instructor: Jinze Liu Fall /8/2016Jinze University of Kentucky 2 Database Project Database Architecture Database programming.
CompSci 280 S Introduction to Software Development
COMP 430 Intro. to Database Systems
Introduction to SQL Programming Techniques
SQL – Python and Databases
Adapters and Converters
Pl/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-3
CIS 136 Building Mobile Apps
Interacting with the Oracle Server
Applied CyberInfrastructure Concepts Fall 2017
Case Statements and Functions
Views, Stored Procedures, Functions, and Triggers
Tips and tricks for bridging platforms
Database Management Systems 2
Error Handling Summary of the next few pages: Error Handling Cursors.
Introduction to Python
SQL – Parameterized Queries
CS122B: Projects in Databases and Web Applications Spring 2017
CS122B: Projects in Databases and Web Applications Winter 2017
SQL Standard Query Language Good for manipulating a Database
SQL – Python and Databases (Continued)
Introduction to Python
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
CIS 136 Building Mobile Apps
CREATE, INSERT, SELECT.
Web DB Programming: PHP
SQL Application Persistence Design Patterns
Chapter 8 Advanced SQL.
Information Management
Android Developer Fundamentals V2
MySQL Web Application Connecting to a MySQL database
COP 2700 – Data Structures (SQL)
SQLLite and Android.
Topics Introduction to File Input and Output
SQL – Application Persistence Design Patterns
Stored Procedure Language
JDBC II IS
Presentation transcript:

Introduction to pysqlite A crash course to accessing SQLite from within your Python programs. Based on pysqlite 2.0.

SQLite basics SQLite is embedded, there is no server Each SQLite database is stored in one file SQLite supports in-memory databases, too

SQLite supports the following types TEXT INTEGER FLOAT BLOB NULL SQLite basics (2) SQLite supports the following types TEXT INTEGER FLOAT BLOB NULL

SQLite basics (3) Type conversions from SQLite to Python TEXT unicode INTEGER int FLOAT float BLOB buffer NULL None

from pysqlite2 import dbapi2 as sqlite Import the module from pysqlite2 import dbapi2 as sqlite

con = sqlite.connect(“mydb.db”) con = sqlite.connect(“:memory:”) Open a connection con = sqlite.connect(“mydb.db”) If the file does not exist, an empy database is created. con = sqlite.connect(“:memory:”) In-memory databases are always empty when created.

Create a cursor cur = con.cursor()

Execute a query ... and fetch one row Returned rows are tuples! cur.execute(“select firstname, lastname from person”) row = cur.fetchone() if row is None: # Error, no result else: firstname, lastname = row[0], row[1]

Execute a query ... and process all rows The cursor is iterable, just loop over the cursor! cur.execute(“select firstname, lastname from person”) for row in cur: print “firstname: %s, lastname: %s” % (row[0], row[1])

Queries with parameters (1) cur.execute(SQL, parameters) SQL: Python string, must be encoded in UTF-8 if it contains non-ASCII characters. Or: Unicode string. Parameters: Sequence (list, tuple, ...) or mapping (dict).

Queries with parameters (2) Use ? as placeholders and use a sequence for the parameters: cur.execute(“”” insert into person(firstname, lastname) values (?, ?)”, (“Gerhard”, “Haering”) )

Queries with parameters (3) Use :name as placeholders and use a mapping for the parameters: item = {“firstname”: “Gerhard”, “lastname”: “Haering”} cur.execute(“”” insert into person(firstname, lastname) values (:firstname, :lastname)”, item )

Queries with parameters (4) Neat hack - use the fact that locals() is a mapping, too: firstname = “Gerhard” lastname = “Haering” cur.execute(“”” insert into person(firstname, lastname) values (:firstname, :lastname)”, locals() )

To make changes permanent you must commit the changes! Oops? Where's my data??? pysqlite uses transactions so that your database is always in a consistent state. To make changes permanent you must commit the changes!

Committing changes cur = con.cursor() cur.execute(“insert into table1 ...”) cur.execute(“insert into table2 ...”) con.commit() After database modifications that belong together logically, commit your changes so that this consistent state is stored permanently.

Roll back changes cur = con.cursor() try: cur.execute(“delete from ...”) con.commit() except sqlite.DatabaseError: con.rollback() Roll back changes when an error occured, in order to keep your database consistent!

Be nice and clean up cur.close() con.close() You should close cursors and connections that you no longer use. Only close the connection when you've closed all cursors you created from it!

Conclusion That's it – I hope you've learnt something about using SQLite from Python using pysqlite 2.0!

Resources The Wiki on http://pysqlite.org/ The pysqlite mailing list! For how SQLite works and the SQL it supports: http://sqlite.org/