Connect to sqlite from python

Create a database in memory

This is convenient when you want to execute SQL many times and try it because you do not need to delete the table.

import sqlite3

#conn = sqlite3.connect('test_sqlite.db')
conn = sqlite3.connect(':memory')

curs = conn.cursor()

curs.execute('CREATE TABLE persons(id INTEGER PRIMARY KEY AUTOINCREMENT,name STRING)')

curs.execute('INSERT INTO persons(name) values("Mike")')
curs.execute('INSERT INTO persons(name) values("Nancy")')
curs.execute('INSERT INTO persons(name) values("Jun")')
conn.commit()

curs.execute('SELECT * FROM persons')
print(curs.fetchall())

curs.close()
conn.close()

Execution result:

[(1, 'Mike'), (2, 'Nancy'), (3, 'Jun')]

Recommended Posts

Connect to sqlite from python
Connect to utf8mb4 database from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Connect python to mysql
Connect to coincheck's Websocket API from Python
Use Tor to connect from urllib2 [Python] [Mac]
Connect to BigQuery with Python
Post from Python to Slack
Cheating from PHP to Python
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Switch from python2.7 to python3.6 (centos7)
Call Matlab from Python to optimize
Create folders from '01' to '12' with python
Post from python to facebook timeline
How to use SQLite in Python
[Lambda] [Python] Post to Twitter from Lambda!
Python (from first time to execution)
Post images from Python to Tumblr
Ssh connect to GCP from Windows
How to access wikipedia from python
Python to switch from another language
Did not change from Python 2 to 3
Update Python on Mac from 2 to 3
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
Connect to Packetix VPN from CentOS 7 minimal.
[Python] Fluid simulation: From linear to non-linear
Updated to Python 2.7.9
Connect to Docker's MySQL container from Flask
From Python to using MeCab (and CaboCha)
How to update Google Sheets from Python
Send a message from Python to Slack
Private Python handbook (updated from time to time)
I want to use jar from python
Connect to MySQL with Python within Docker
Convert from katakana to vowel kana [python]
How to avoid duplication of data when inputting from Python to SQLite.
Push notification from Python server to Android
Sum from 1 to 10
sql from python
Connecting from python to MySQL on CentOS 6.4
How to connect to Cloud Firestore from Google Cloud Functions with python code
Porting and modifying doublet-solver from python2 to python3.
Trying to handle SQLite3 with Python [Note]
How to access RDS from Lambda (python)
MeCab from Python
Sqlite in python
Python> Output numbers from 1 to 100, 501 to 600> For csv
Connect to s3 with AWS Lambda Python
[Python / Tkinter] Connect keyboard shortcuts to Menu
Compress python data and write to sqlite
Connect to pepper with PEPPER Mac's python interpreter
Convert from Markdown to HTML in Python
[Amazon Linux] Switching from Python 2 series to Python 3 series
API explanation to touch mastodon from python
"Backport" to python 2
Connect to postgreSQL from Python and use stored procedures in a loop.
Send a message from Slack to a Python server
Edit Excel from Python to create a PivotTable
I want to connect to PostgreSQL from various languages
Study from Python Hour7: How to use classes