Introduction to SQLite ISYS 475. Download SQLite – Precompiled Binaries for Windows Create a folder “sqlite” under c:\xampp.

Slides:



Advertisements
Similar presentations
Legal Meetings: Extended Instructions on Movica and Screencast.
Advertisements

CC SQL Utilities.
Office Links - Sharing Data in Microsoft Office A Mixed Bag of Treasures Chester N. Barkan Registrar Long Island University, C.W.Post Campus.
Oracle 10g Express. Download Oracle 10g Express Oracle 10g Express Edition: – edition/overview/index.htmlhttp://
® IBM Software Group © IBM Corporation QUY Thai Duy – ITFac DLU Lesson 2: The DB2 Environment.
10 REASONS Why it makes a good option for your DB IN-MEMORY DATABASES Presenter #10: Robert Vitolo.
Lab6 – Debug Assembly Language Lab
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
1 Introduction to Programming Environment Using MetroWerks CodeWarrior and Palm Emulator.
Working with SQL and PL/SQL/ Session 1 / 1 of 27 SQL Server Architecture.
Integrating Databases into the Web with PHP/MySQL CS 4000.
SQLite and the.NET Framework This PPT:
Simple Web SQLite Manager/Form/Report
SQLite 1 CS440. What is SQLite?  Open Source Database embedded in Android  SQL syntax  Requires small memory at runtime (250 Kbytes)  Lightweight.
MBAC 611.  We have been using MS Access to query and modify our databases.  MS Access provides a GUI (Graphical User Interface) that hides much of the.
Databases Dan Otero Alex Loddengaard
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
Lesson 4 Computer Software
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Your Interactive Guide to the Digital World Discovering Computers 2012.
Computer Literacy BASICS: A Comprehensive Guide to IC 3, 5 th Edition Lesson 3 Windows File Management 1 Morrison / Wells / Ruffolo.
CSCI 6962: Server-side Design and Programming
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Overview of SQL Server Alka Arora.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
ASP.NET Programming with C# and SQL Server First Edition
Miscellaneous Excel Combining Excel and Access. – Importing, exporting and linking Parsing and manipulating data. 1.
Python MySQL Database Access
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
MySQL Stored Procedure and User-Defined Function ISYS 475.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
Chapter 2: SQL – The Basics Objectives: 1.The SQL execution environment 2.SELECT statement 3.SQL Developer & SQL*Plus.
Chapter 17 Creating a Database.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Inserting Data.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Course FAQ’s I do not have any knowledge on SQL concepts or Database Testing. Will this course helps me to get through all the concepts? What kind of.
Technology Vocabulary By: Rakeysha Patterson. Search Engine  A computer program that searches documents, especially on the World Wide Web, for a specified.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
Introduction.  Administration  Simple DBMS  CMPT 454 Topics John Edgar2.
What is MySQL? MySQL is a relational database management system (RDBMS) based on SQL (Structured Query Language). First released in January, Many.
Introducing Dreamweaver. Dreamweaver The web development application used to create web pages Part of the Adobe creative suite.
© 2012 LogiGear Corporation. All Rights Reserved FitNesseFitNesse Authors: Nghia Pham 1.
Computer Literacy BASICS: A Comprehensive Guide to IC 3, 5 th Edition Lesson 3 Windows File Management 1 Morrison / Wells / Ruffolo.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
1 Working with MS SQL Server Beginning ASP.NET in C# and VB Chapter 12.
Software-Projekt 2008 Seminarvortrag“Short tutorial of MySql“ Wei Chen Verena Honsel.
Database Processing with JSP ISYS 350. Database Applications Applications Database Server Queries/Updates Results.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Introduction to Database Programming with Python Gary Stewart
C Copyright © 2009, Oracle. All rights reserved. Using SQL Developer.
Computer Literacy BASICS: A Comprehensive Guide to IC 3, 5 th Edition Lesson 3 Windows File Management 1 Morrison / Wells / Ruffolo.
3 A Guide to MySQL.
Aga Private computer Institute Prepared by: Srwa Mohammad
Creating Database Objects
SQL Environment.
Topics Introduction Hardware and Software How Computers Store Data
CS422 Principles of Database Systems Course Overview
Introduction to Web programming
Introduction What is a Database?.
Database Performance Tuning and Query Optimization
Introduction of Week 6 Assignment Discussion
8 6 MySQL Special Topics A Guide to MySQL.
Accessing Databases with ADO.NET, Handling Exceptions, and Printing
Tutorial 6 PHP & MySQL Li Xu
Chapter 11 Database Performance Tuning and Query Optimization
Creating Database Objects
Tutorial 12 Managing and Securing a Database
Presentation transcript:

Introduction to SQLite ISYS 475

Download SQLite – Precompiled Binaries for Windows Create a folder “sqlite” under c:\xampp and copy the downloaded program to the folder.

Introduction SQLite is a database system that is: – self-contained: It requires very minimal support from external libraries; – Serverless: reads and writes directly from the database files on disk and requires no intermediary server process. SQLite itself is the server. – zero-configuration: It does not need to be "installed" before it is used. There is no "setup" procedure; – transactional: all changes and queries appear to be Atomic, Consistent, Isolated, and Durable (ACID) that can survives system failure. – It is small. A typical installation occupies only about 256K on your drive.

Where are good places to use it? In desktop applications. SQLite makes a great application file format. You can use SQL, don't have to design your own file format. For smaller websites. SQLite should be able to manage a site that gets under 100,000 hits a day easily. In other words, most websites would be fine with SQLite as the database. For embedded devices. The uses in the iPhone and Symbian are good examples of this.

Two Ways of Working with SQLite SQLite Command Line PHP/SQLite

Starting SQLite Command Line To start the sqlite3 program from the Windows command prompt, just type "sqlite3" followed by the name the file that holds the SQLite database. If the file does not exist, a new one is created automatically. Note: The standard extension for SQLite database is “.sqlite”

Helpful links Command line commands: – – mands_and_general_usage.html

For a listing of the available dot commands, you can enter ".help" at any time

.read FILENAME Execute SQL in FILENAME sqlite>.read c:\\teaching\\475\\MySQLOrder.txt sqlite>.tables customers odetails orders products salesreps tbl1 Note: Need the escape character, “\\”

.databases List names and files of attached databases sqlite>.database seq name file main c:\xampp\Sqlite\salesdb.sqlite sqlite>

.tables List names of tables sqlite>.tables customers odetails orders products salesreps

.schema Show the CREATE statements sqlite>.schema CREATE TABLE customers (cid char(3), cname varchar(20), rating char(1), city varchar(20)); CREATE TABLE odetails(oid char(3), pid char(3),qty decimal(5,1)); CREATE TABLE orders(oid char(3), cid char(3),sid char(3),odate date); CREATE TABLE products(pid char(7), pname varchar(15), price decimal(6,2), onhand decimal(5,1)); CREATE TABLE salesreps(sid char(3),sname varchar(20),phone char(4)); CREATE TABLE tbl1(one varchar(10), two smallint);

.backup sqlite>.backup c:\\xampp\\sqlite\\backup.txt

.mode MODE Set output mode where MODE is one of: csv Comma-separated values column Left-aligned columns. html HTML code insert SQL insert statements for TABLE line One value per line list Values delimited by.separator string tabs Tab-separated values tcl TCL list elements sqlite>.mode list sqlite> select * from tbl1;

Set output to html sqlite>.mode html sqlite> select * from customers; C1 MYERS A CHICAGO C2 GOODMAN A SAN FRANCISCO C3 LEE B CHICAGO C4 GRAUER C LOS ANGELES

Other Commands.output FILENAME Send output to FILENAME.output stdout Send output to the screen.quit

PHP and SQLite k.sqlite.php

PHP Links to SQLite This is an extension for the SQLite Embeddable SQL Database Engine. SQLite is a C library that implements an embeddable SQL database engine. Programs that link with the SQLite library can have SQL database access without running a separate RDBMS process. SQLite is the server. The SQLite library reads and writes directly to and from the database files on disk.

Run phpinfo();

Two Ways of Working with SQLite Using PDO commands Using PHP/SQLIte commands

Working with an existing Sqlite database with PDO DSN: sqlite:absolute path to database Example: sqlite:c:\\xampp\\Sqlite\\salesdb.sqlite

Define a database handler 2. Connecting to a database at a specified path: $db = new PDO("sqlite:c:\\xampp\\Sqlite\\salesdb.sqlite"); 1. Connecting to a database in the current project folder: $db = new PDO("sqlite:SalesDB.sqlite");

Run SQL SELECT command and fetch records <?php $db = new PDO("sqlite:c:\\xampp\\sqlite\\salesdb.sqlite"); $result=$db->query("SELECT * FROM customers"); $result->setFetchMode(PDO::FETCH_ASSOC); while($row = $result->fetch()) { echo $row['cid']. " ". $row['cname']. " ". $row['city']. " ". $row['rating']. " "; } ?>

Using foreach <?php $db = new PDO("sqlite:c:\\xampp\\sqlite\\salesdb.sqlite"); $result=$db->query("SELECT * FROM customers"); $result->setFetchMode(PDO::FETCH_ASSOC); foreach ($result as $row) { echo $row['cid']. " ". $row['cname']. " ". $row['city']. " ". $row['rating']. " "; } ?>

Use PDO exec to run Insert <?php $db = new PDO("sqlite:c:\\xampp\\sqlite\\salesdb.sqlite"); $db->exec("INSERT INTO customers VALUES ('c6','chen','sf', 'a')"); ?>

Use PDO exec to run Delete <?php $db = new PDO("sqlite:c:\\xampp\\sqlite\\salesdb.sqlite"); $count = $db->exec("DELETE FROM customers WHERE cid = 'c6'"); print("Deleted $count rows.\n"); ?>

Use PDO to create a new sqlite database DSN: sqlite:absolute path to database Example: sqlite:customer.sqlite sqlite:c:\\xampp\\sqlite\\customer.sqlite Note: If the database does not exist, SQLite will create it. But we need to execute SQL CREATE TABLE command to create tables.

Define a database handler 2. Creating a new database at a specified path: $db = new PDO("sqlite:c:\\xampp\\sqlite\\customer.sqlite"); 1. Creating a new database in the current project folder: $db = new PDO("sqlite:customer.sqlite");

Creating a New SQLite Table Use exec() command to run the CREATE TABLE command: $db->exec('CREATE TABLE customer (cid CHAR(5) PRIMARY KEY, cname CHAR(25), city CHAR(25), rating CHAR(1))');

Run SQL Insert commands to add records <?php $db = new PDO("sqlite:c:\\xampp\\sqlite\\customer.sqlite"); $db->exec('CREATE TABLE customer (cid CHAR(5) PRIMARY KEY, cname CHAR(25), city CHAR(25), rating CHAR(1))'); $db->exec("INSERT INTO customer VALUES ('c1','peter','sf', 'a')"); $db->exec("INSERT INTO customer VALUES ('c2','paul','la', 'b')"); $db->exec("INSERT INTO customer VALUES ('c3','mary','sf', 'a')"); ?>

Run SQL SELECT command and fetch records <?php $db = new PDO("sqlite:c:\\xampp\\sqlite\\customer.sqlite"); $result=$db->query("SELECT * FROM customer"); $result->setFetchMode(PDO::FETCH_ASSOC); foreach ($result as $row) { echo $row['cid']. " ". $row['cname']. " ". $row['city']. " ". $row['rating']. " "; } ?>

Using PHP/SQLIte3 class SQLite3::__construct — Instantiates an SQLite3 object and opens an SQLite 3 database SQLite3::query — Executes an SQL query and returns a SQLite3Result class SQLite3Result::fetchArray — Fetches a result row as an associative or numerically indexed array or both SQLite3::exec — Executes a result-less query against a given database SQLite3::changes — Returns the number of database rows that were changed (or inserted or deleted) SQLite3::close — Closes the database connection

Working with SQLite3 class <?php $db = new SQLite3("c:\\xampp\\sqlite\\customer.sqlite"); $result=$db->query("SELECT * FROM customer"); while($row = $result->fetchArray()) { echo $row['cid']. " ". $row['cname']. " ". $row['city']. " ". $row['rating']. " "; } ?>

Adding a record <?php $db = new SQLite3("c:\\xampp\\sqlite\\customer.sqlite"); $db->exec("INSERT INTO customer VALUES ('c6','chen','sf', 'a')"); echo $db->changes(). 'record added'; ?>

Deleting a record <?php $db = new SQLite3("c:\\xampp\\sqlite\\customer.sqlite"); $db->exec("DELETE FROM customer WHERE cid = 'c6'"); echo $db->changes(). 'record deleted'; ?>

In-memory database An in-memory database (IMDB; also main memory database system or MMDB) is a database management system that primarily relies on main memory for computer data storage. It is contrasted with database management systems which employ a disk storage mechanism. Main memory databases are faster than disk-optimized databases since the internal optimization algorithms are simpler and execute fewer CPU instructions. Accessing data in memory eliminates seek time when querying the data, which provides faster and more predictable performance than disk

Creating SQLite Database in Memory DSN: sqlite::memory: – Example: $db = new PDO("sqlite::memory:"); Note: there is a “:” after the word memory. The database ceases to exist as soon as the database connection is closed. opening two database connections each with the filename ":memory:" will create two independent in- memory databases.

In Memory Database Example <?php $db = new PDO("sqlite::memory:"); $db->exec('CREATE TABLE customer (cid CHAR(5) PRIMARY KEY, cname CHAR(25), city CHAR(25), rating CHAR(1))'); $db->exec("INSERT INTO customer VALUES ('c1','peter','sf', 'a')"); $db->exec("INSERT INTO customer VALUES ('c2','paul','la', 'b')"); $db->exec("INSERT INTO customer VALUES ('c3','mary','sf', 'a')"); $result=$db->query("SELECT * FROM customer"); $result->setFetchMode(PDO::FETCH_ASSOC); foreach ($result as $row) { echo $row['cid']. " ". $row['cname']. " ". $row['city']. " ". $row['rating']. " "; } ?>

ATTACH DATABASE The ATTACH DATABASE statement adds another database file to the current database connection. Example: – ATTACH DATABASE "FromDisk.sqlite" AS diskdb; Tables in an attached database can be referred to using the syntax database-name.table- name.

Copy Disk Database to Memory <?php $dbMemo = new PDO('sqlite::memory:'); $dbMemo->exec('ATTACH "c:\\xampp\\sqlite\\salesdb.sqlite" AS diskdb'); $dbMemo->exec('create table customer as select * from diskdb.customers'); $dbMemo->exec('DETACH diskdb'); $result=$dbMemo->query("SELECT * FROM customer"); $result->setFetchMode(PDO::FETCH_ASSOC); foreach ($result as $row) { echo $row['cid']. " ". $row['cname']. " ". $row['city']. " ". $row['rating']. " "; } ?>

Copy Memory Database to disk <?php $db = new PDO("sqlite::memory:"); $db->exec('CREATE TABLE customer (cid CHAR(5) PRIMARY KEY, cname CHAR(25), city CHAR(25), rating CHAR(1))'); $db->exec("INSERT INTO customer VALUES ('c12','peter','sf', 'a')"); $db->exec("INSERT INTO customer VALUES ('c22','paul','la', 'b')"); $db->exec("INSERT INTO customer VALUES ('c32','mary','sf', 'a')"); $result=$db->query("SELECT * FROM customer"); $result->setFetchMode(PDO::FETCH_ASSOC); foreach ($result as $row) { echo $row['cid']. " ". $row['cname']. " ". $row['city']. " ". $row['rating']. " "; } $db->exec('ATTACH "c:\\xampp\\sqlite\\customer.sqlite" AS diskdb'); $db->exec('insert into diskdb.customer select * from customer'); $db->exec('DETACH diskdb'); $dbSave = new PDO('sqlite:c:\\xampp\\sqlite\\customer.sqlite'); $result=$dbSave->query("SELECT * FROM customer"); $result->setFetchMode(PDO::FETCH_ASSOC); foreach ($result as $row) { echo $row['cid']. " ". $row['cname']. " ". $row['city']. " ". $row['rating']. " "; } ?>