[Python] [SQLite3] Operate SQLite with Python (Basic)

Notes on working with SQLite in Python It is a memorandum.

Source code and description

sample.py


import sqlite3
from contextlib import closing

dbname = "sample.db"

with closing(sqlite3.connect(dbname)) as conn:
    c = conn.cursor()
    query = '''drop table if exists User'''
    c.execute(query)
    conn.commit()

with closing(sqlite3.connect(dbname)) as conn:
    c = conn.cursor()
    query = '''create table if not exists
                User(id integer primary key, name varchar(32))'''
    c.execute(query)
    conn.commit()

with closing(sqlite3.connect(dbname)) as conn:
    c = conn.cursor()
    query = ''' insert into User (id, name) values (?,?)'''
    user = (1, "Yamashita")
    c.execute(query, user)
    conn.commit()

with closing(sqlite3.connect(dbname)) as conn:
    c = conn.cursor()
    query = ''' insert into User (id, name) values (?,?)'''
    user = [
        (2, "Kinoshita")
        ,(3, "Hasegawa")
    ]
    c.executemany(query, user)
    conn.commit()

with closing(sqlite3.connect(dbname)) as conn:
    c = conn.cursor()
    query = "select * from User"
    for row in c.execute(query):
        print(row)

with closing(sqlite3.connect(dbname)) as conn: When operating sqlite, commit (), close () are required after executing the query By using closing of contextlib, you can prevent omission of close () description of connection.

c.executemany(query, user) You can use executemany () to write multiple inserts at once, passing a list of tuples as arguments.

Recommended Posts

[Python] [SQLite3] Operate SQLite with Python (Basic)
Operate Kinesis with Python
Operate Blender with Python
Operate Excel with Python (1)
Operate Excel with Python (2)
Operate Excel with Python openpyxl
Operate TwitterBot with Lambda, Python
BASIC authentication with Python bottle
[Python] Using OpenCV with Python (Basic)
[Note] Operate MongoDB with Python
Operate a receipt printer with python
Scraping with Selenium in Python (Basic)
Try to operate Facebook with Python
Easily handle lists with python + sqlite3
Operate ECHONET Lite appliances with Python
Easily handle databases with Python (SQLite3)
Basic study of OpenCV with Python
FizzBuzz with Python3
Scraping with Python
Getting Started with python3 # 1 Learn Basic Knowledge
RF Python Basic_01
Statistics with python
Scraping with Python
Python with Go
Operate smartlife power supply with python (de-IFTTT)
Put protocol buffers into sqlite with python
Twilio with Python
Play with 2016-Python
Tested with Python
Basic Python writing
[GCP] Operate Google Cloud Storage with Python
Learn Python! Comparison with Java (basic function)
Trying to handle SQLite3 with Python [Note]
with syntax (Python)
Sqlite in python
Python3 basic grammar
Bingo with python
[Python] Automatically operate the browser with Selenium
Operate home appliances with Python and IRKit
RF Python Basic_02
Excel with Python
Microcomputer with Python
Cast with python
I tried hundreds of millions of SQLite with python
Achieve Basic Authentication with CloudFront Lambda @ Edge with Python 3.8
Send HTTP with Basic authentication header in Python
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Python I'm also basic
Python basic grammar / algorithm
Try scraping with Python.
Python Basic Course (7 Dictionary)
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python