<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=Python%3A_sqlite</id>
	<title>Python: sqlite - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=Python%3A_sqlite"/>
	<link rel="alternate" type="text/html" href="https://onnocenter.or.id/wiki/index.php?title=Python:_sqlite&amp;action=history"/>
	<updated>2026-05-03T22:24:19Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.4</generator>
	<entry>
		<id>https://onnocenter.or.id/wiki/index.php?title=Python:_sqlite&amp;diff=68328&amp;oldid=prev</id>
		<title>Onnowpurbo at 00:57, 8 April 2023</title>
		<link rel="alternate" type="text/html" href="https://onnocenter.or.id/wiki/index.php?title=Python:_sqlite&amp;diff=68328&amp;oldid=prev"/>
		<updated>2023-04-08T00:57:46Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;https://onnocenter.or.id/wiki/index.php?title=Python:_sqlite&amp;amp;diff=68328&amp;amp;oldid=45211&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Onnowpurbo</name></author>
	</entry>
	<entry>
		<id>https://onnocenter.or.id/wiki/index.php?title=Python:_sqlite&amp;diff=45211&amp;oldid=prev</id>
		<title>Onnowpurbo at 02:16, 4 December 2015</title>
		<link rel="alternate" type="text/html" href="https://onnocenter.or.id/wiki/index.php?title=Python:_sqlite&amp;diff=45211&amp;oldid=prev"/>
		<updated>2015-12-04T02:16:58Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;https://onnocenter.or.id/wiki/index.php?title=Python:_sqlite&amp;amp;diff=45211&amp;amp;oldid=45205&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Onnowpurbo</name></author>
	</entry>
	<entry>
		<id>https://onnocenter.or.id/wiki/index.php?title=Python:_sqlite&amp;diff=45205&amp;oldid=prev</id>
		<title>Onnowpurbo: New page: Sumber: http://www.bogotobogo.com/python/python_sqlite_connect_create_drop_table.php   SQLite  &quot;SQLite is a software library that implements a self-contained, serverless, zero-configuratio...</title>
		<link rel="alternate" type="text/html" href="https://onnocenter.or.id/wiki/index.php?title=Python:_sqlite&amp;diff=45205&amp;oldid=prev"/>
		<updated>2015-12-03T02:35:07Z</updated>

		<summary type="html">&lt;p&gt;New page: Sumber: http://www.bogotobogo.com/python/python_sqlite_connect_create_drop_table.php   SQLite  &amp;quot;SQLite is a software library that implements a self-contained, serverless, zero-configuratio...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Sumber: http://www.bogotobogo.com/python/python_sqlite_connect_create_drop_table.php&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SQLite&lt;br /&gt;
&lt;br /&gt;
&amp;quot;SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world.&amp;quot;&lt;br /&gt;
- http://www.sqlite.org/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SQLite Module in Python&lt;br /&gt;
&lt;br /&gt;
To use the SQLite3 module we need to add an import statement to our python script:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import sqlite3&lt;br /&gt;
&lt;br /&gt;
We can check sqlite version:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sqlite3.version&lt;br /&gt;
'2.6.0'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sqlite3.sqlite_version&lt;br /&gt;
'3.7.17&lt;br /&gt;
&lt;br /&gt;
The sqlite.version is the version of the pysqlite (2.6.0), which is the binding of the Python language to the SQLite database. The sqlite3.sqlite_version gives us the version of the SQLite database library. In our case it is 3.7.17.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Connecting to the Database&lt;br /&gt;
&lt;br /&gt;
To connect to the database, we can use sqlite3.connect function by passing the name of a file to open or create it:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import sqlite3&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; db = sqlite3.connect('data/test.db')&lt;br /&gt;
&lt;br /&gt;
We can use the argument &amp;quot;:memory:&amp;quot; to create a temporary DB in the RAM:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import sqlite3&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; dbm = db = sqlite3.connect(':memory:')&lt;br /&gt;
&lt;br /&gt;
When we are done working with the DB we need to close the connection:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; db.close()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; dbm.close()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Creating DB with SQLite command line - Outside of python shell&lt;br /&gt;
&lt;br /&gt;
Another way of creating db is to use the sqlite3 command line tool:&lt;br /&gt;
&lt;br /&gt;
$ ls&lt;br /&gt;
$ sqlite3 test.db&lt;br /&gt;
SQLite version 3.7.17 2013-05-20 00:56:22&lt;br /&gt;
Enter &amp;quot;.help&amp;quot; for instructions&lt;br /&gt;
Enter SQL statements terminated with a &amp;quot;;&amp;quot;&lt;br /&gt;
sqlite&amp;gt; .tables&lt;br /&gt;
sqlite&amp;gt; .exit&lt;br /&gt;
$ ls&lt;br /&gt;
test.db&lt;br /&gt;
&lt;br /&gt;
The .tables command gives a list of tables in the test.db database. We don't have any tables now. The .exit command terminates the interactive session of the sqlite3 command line tool.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE&lt;br /&gt;
&lt;br /&gt;
To use the database, we need to get a cursor object and pass the SQL statements to the cursor object to execute them. Then, we should commit the changes.&lt;br /&gt;
&lt;br /&gt;
We are going to create a books table with title, author, price and year columns.&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; cursor = db.cursor()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; cursor.execute('''CREATE TABLE books(id INTEGER PRIMARY KEY, &lt;br /&gt;
...                   title TEXT, author TEXT, price TEXT, year TEXT)&lt;br /&gt;
... ''')&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; db.commit()&lt;br /&gt;
&lt;br /&gt;
Note that the commit function is invoked on the db object, not the cursor object.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
DROP TABLE&lt;br /&gt;
&lt;br /&gt;
To drop a table:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; cursor = db.cursor()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; cursor.execute('''DROP TABLE books''')&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; db.commit()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
INSERT - Inserting Data into the Database&lt;br /&gt;
&lt;br /&gt;
To insert data we use the cursor to execute the query. In this example we are going to insert two books in the database, their information will stored in python variables.&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; db.close()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import sqlite3&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; db = sqlite3.connect('data/test.db')&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; cursor = db.cursor()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; cursor.execute('''CREATE TABLE books(id INTEGER PRIMARY KEY,&lt;br /&gt;
...                    title TEXT, author TEXT, price TEXT, year TEXT)&lt;br /&gt;
...                ''')&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; db.commit()&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import sqlite3&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; db = sqlite3.connect('data/test.db')&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; cursor = db.cursor()&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; title1 = 'Learning Python'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; author1 = 'Mark Lutz'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; price1 = '$36.19'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; year1 ='Jul 6, 2013'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; title2 = 'Two Scoops of Django: Best Practices For Django 1.6'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; author2 = 'Daniel Greenfeld'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; price2 = '$34.68'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; year2 = 'Feb 1, 2014'&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; cursor.execute('''INSERT INTO books(title, author, price, year)&lt;br /&gt;
...                   VALUES(?,?,?,?)''', (title1, author1, price1, year1))&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; cursor.execute('''INSERT INTO books(title, author, price, year)&lt;br /&gt;
...                   VALUES(?,?,?,?)''', (title2, author2, price2, year2))&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; db.commit()&lt;br /&gt;
&lt;br /&gt;
Note: If we need values from Python variables it is recommended to use the &amp;quot;?&amp;quot; placeholder. Never use string operations or concatenation to make your queries because is very insecure.&lt;br /&gt;
&lt;br /&gt;
The values of the Python variables are passed inside a tuple.&lt;br /&gt;
&lt;br /&gt;
If we have more books to insert, we can continue. But this time, we'll do it another way: passing a dictionary using the &amp;quot;:keyname&amp;quot; placeholder:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; title3 = 'Python Cookbook'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; author3 = 'David Beazley'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; price3 = '$30.29'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; year3 = 'May 29, 2013'&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; cursor.execute('''INSERT INTO books(title, author, price, year)&lt;br /&gt;
...                   VALUES(:title, :author, :price, :year)''',&lt;br /&gt;
...                   {'title':title3, 'author':author3, 'price':price3, 'year':year3})&lt;br /&gt;
&amp;lt;sqlite3.Cursor object at 0x7f1d2717d650&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; db.commit()&lt;br /&gt;
&lt;br /&gt;
If we need to insert several users, we can use executemany and a list with the tuples:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; title4 = 'The Quick Python Book'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; author4 = 'Naomi R. Ceder'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; price4 = '$16.39'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; year4 = 'Jan 15, 2010'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; title5 ='Python Testing'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; author5 ='David Sale'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; price5 = '$38.20'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; year5 = 'Sep 2, 2014'&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; books = [(title4,author4, price4, year4),&lt;br /&gt;
...          (title5,author5, price5, year5)]&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; cursor.executemany('''INSERT INTO books(title, author, price, year) VALUES(?,?,?,?)''', books)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; db.commit()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
sqlite3 command line&lt;br /&gt;
&lt;br /&gt;
Let's see what we've done so far using sqlite command shell:&lt;br /&gt;
&lt;br /&gt;
$ sqlite3 test.db&lt;br /&gt;
SQLite version 3.7.17 2013-05-20 00:56:22&lt;br /&gt;
Enter &amp;quot;.help&amp;quot; for instructions&lt;br /&gt;
Enter SQL statements terminated with a &amp;quot;;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
sqlite&amp;gt; .tables&lt;br /&gt;
books&lt;br /&gt;
&lt;br /&gt;
sqlite&amp;gt; SELECT * FROM books;&lt;br /&gt;
1|Learning Python|Mark Lutz|$36.19|Jul 6, 2013&lt;br /&gt;
2|Two Scoops of Django: Best Practices For Django 1.6|Daniel Greenfeld|$34.68|Feb 1, 2014&lt;br /&gt;
3|Python Cookbook|David Beazley|$30.29|May 29, 2013&lt;br /&gt;
4|The Quick Python Book|Naomi R. Ceder|$16.39|Jan 15, 2010&lt;br /&gt;
5|Python Testing|David Sale|$38.20|Sep 2, 2014&lt;br /&gt;
&lt;br /&gt;
sqlite&amp;gt; .mode column&lt;br /&gt;
sqlite&amp;gt; .headers on&lt;br /&gt;
sqlite&amp;gt; SELECT * FROM books;&lt;br /&gt;
id          title            author      price       year       &lt;br /&gt;
----------  ---------------  ----------  ----------  -----------&lt;br /&gt;
1           Learning Python  Mark Lutz   $36.19      Jul 6, 2013&lt;br /&gt;
2           Two Scoops of D  Daniel Gre  $34.68      Feb 1, 2014&lt;br /&gt;
3           Python Cookbook  David Beaz  $30.29      May 29, 201&lt;br /&gt;
4           The Quick Pytho  Naomi R. C  $16.39      Jan 15, 201&lt;br /&gt;
5           Python Testing   David Sale  $38.20      Sep 2, 2014&lt;br /&gt;
sqlite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Note that we modified the way the data is displayed in the console. We used the column mode and turned on the headers.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Referensi==&lt;br /&gt;
&lt;br /&gt;
* http://www.bogotobogo.com/python/python_sqlite_connect_create_drop_table.php&lt;/div&gt;</summary>
		<author><name>Onnowpurbo</name></author>
	</entry>
</feed>