Trying to handle SQLite3 with Python [Note]

at first

sqlite is a standard module included in Python. You can ʻimport` without installing.

connect

import sqlite3

db = "./exsample.db" #Path to database
con = sqlite3.connect(db) #connect

Make a table

cur = con.cursor()
table = "Friend" #table name
sql = f"""create table {table}(
    id integer primary key autoincrement,
    name text,
    age integer,
)"""
cur.execute(sql) #SQL execution
self.con.commit() #Save

ʻId is used as the primary key with primary key, and is automatically assigned with ʻautoincrement.

If you don't have a table, make one.

table = "Friend" #table name
sql = f"""create table if not exists {table}(
    id integer primary key autoincrement,
    name text,
    age integer,
)"""
cur.execute(sql) #SQL execution
self.con.commit() #Save

Enter ʻif not exists`.

Table editing

old_table = "Friend" #Old table name
new_table = "NewFriend" #New table name
sql = f"alter table {old_table} rename to {new_table}"
cur.execute(sql) #SQL execution
self.con.commit() #Save

Delete table

table = "NewFriend" #The table you want to delete
sql = f"drop table {table}"
cur.execute(sql) #SQL execution
self.con.commit() #Save

Type list

Model name information
NULL Null value
INTEGER Signed integer. 1, 2, 3, 4, 6,or 8 bytes
REAL Floating point number. Store in 8 bytes
TEXT text. UTF-8, UTF-16BE or UTF-16-Stored in one of LE
BLOB Store input data as it is

Record insertion

table = "Friend" #table name
sql = f"insert into {table} (name, age) values ('Jiro', 20)"
cur.execute(sql) #SQL execution
self.con.commit() #Save

Or

table = "Friend" #table name
sql = f"insert into {table} (name, age) values (?, ?)"
data = ("Jiro", 20)
cur.execute(sql, data) #SQL execution
self.con.commit() #Save

It can be inserted by setting it to ? And inserting a tuple in the second argument.

Record editing

Try converting Jiro to Taro

table = "Friend" #table name
id = 1 #ID of the record you want to edit
sql = f"update {table} set name='Taro' where id={id}"
cur.execute(sql) #SQL execution
self.con.commit() #Save

Record deletion

table = "Friend" #table name
id = 1 #ID of the record to delete
sql = f"delete from {table} where id={id}"
cur.execute(sql) #SQL execution
self.con.commit() #Save

Recommended Posts

Trying to handle SQLite3 with Python [Note]
Easily handle lists with python + sqlite3
Easily handle databases with Python (SQLite3)
[python] A note when trying to use numpy with Cython
Handle Excel with python
Handle rabbimq with python
Note to daemonize python
[Python] How to handle Japanese characters with openCV
How to handle datetime type in python sqlite3
Python script to get note information with REAPER
Connect to BigQuery with Python
Connect to Wikipedia with Python
Post to slack with Python 3
Reading Note: An Introduction to Data Analysis with Python
[Note] Operate MongoDB with Python
Connect to sqlite from python
Switch python to 2.7 with alternatives
Write to csv with Python
[Python] [SQLite3] Operate SQLite with Python (Basic)
Handle Base91 keys with python + redis.
Python: How to use async with
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Nice to meet you with python
Try to operate Facebook with Python
How to use SQLite in Python
Output to csv file with Python
(Note) Be careful with python argparse
Convert list to DataFrame with python
MP3 to WAV conversion with Python
To do tail recursion with Python2
How to get started with Python
[Note] Hello world output with python
What to do with PYTHON release?
Unable to install Python with pyenv
Handle Excel CSV files with Python
How to use FTP with Python
Foreign Key in Python SQLite [Note]
How to calculate date with python
Easily post to twitter with Python 3
I want to debug with Python
How to handle Japanese in Python
[Note] A story about trying to override a class method with two underscores in Python 3 series.
A note on what you did to use Flycheck with Python
How to change Django's SQLite3 uploaded to python anywhere with GUI only
How to import CSV and TSV files into SQLite with Python
Note: Python
Introduction to Bayesian Statistical Modeling with python ~ Trying Linear Regression with MCMC ~
Python note
Try to reproduce color film with Python
Try logging in to qiita with Python
Change Python 64bit environment to 32bit environment with Anaconda
English speech recognition with python [speech to text]
[Note] Get data from PostgreSQL with Python
Convert memo at once with Python 2to3
HTML email with image to send with python
Memo to ask for KPI with python
Python to remember only with hello, worlds
Output color characters to pretty with python
Note when creating an environment with python
Introduction to Python Image Inflating Image inflating with ImageDataGenerator