0% found this document useful (0 votes)
25 views2 pages

Ex01-Quick Start

Uploaded by

AB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Ex01-Quick Start

Uploaded by

AB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

7/25/24, 4:30 PM ex01-Quick Start.

ipynb - Colab

keyboard_arrow_down Quick Start


This quick start is a complete example to show how to

create a SQLite database;


creat a data table;
insert data into the table;
query data from the table.

%load_ext sql

keyboard_arrow_down Create a SQLite Database


For practice purposes, it's advisable to create your own database, so that you are free to perform any operations on it. If using the SQLite shell,
we can apply the open command to both create a SQLite database or open it in case it already exists just like:

sqlite> .open testdb

Similaryly, we can use ipython-sql to the same thing:

%sql sqlite:///data/writers.db3

u'Connected: @data/writers.db3'

keyboard_arrow_down Create a table


%%sql let you use multiple SQL statements inside a single cell.

It is now time to create one using a standard SQL command – CREATE TABLE. If the table already existed in the database, an error will pop up.
In addition, we set PRIMARY KEY on USERID to prevent from inserting duplicate writers into the table.

%%sql sqlite://
CREATE TABLE writer(
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
USERID int NOT NULL UNIQUE,
PRIMARY KEY (USERID)
);

Done.
[]

keyboard_arrow_down Add data to the table


The table we have just created is empty. Now we insert some sample data inside it. To populate this data in the form of rows, we use the
command INSERT.

%%sql sqlite://
INSERT INTO writer VALUES ('William', 'Shakespeare', 1616);
INSERT INTO writer VALUES ('Lin', 'Han', 1996);
INSERT INTO writer VALUES ('Peter', 'Brecht', 1978);

1 rows affected.
1 rows affected.
1 rows affected.
[]

keyboard_arrow_down Write the First Query


Let us now turn our attention to writing a simple query to check the results of our previous operations in which we created a table and inserted
three rows of data into it. For this, we would use the command called SELECT.

we can put the query result into a variable such as the following sqlres.

https://colab.research.google.com/drive/1KvBxczp18llk_wTkEii4PseMSRNqAJya#printMode=true 1/2
7/25/24, 4:30 PM ex01-Quick Start.ipynb - Colab

sqlres = %sql SELECT * from writer


sqlres

* sqlite:///data/writers.db3
Done.
FirstName LastName USERID
William Shakespeare 1616
Lin Han 1996
Peter Brecht 1978

You also can select the specific colummns using their names just like:

sqlres = %sql SELECT FirstName, LastName from writer


sqlres

* sqlite:///data/writers.db3
Done.
FirstName LastName
William Shakespeare
Lin Han
Peter Brecht

Start coding or generate with AI.

https://colab.research.google.com/drive/1KvBxczp18llk_wTkEii4PseMSRNqAJya#printMode=true 2/2

You might also like