First, create a Person table.
db = "./exsample.db"
con = sqlite3.connect(db)
cur = con.cursor()
table = "Person" #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 sorted with ʻautoincrement
.
Create an external table.
table = "Memo" #table name
sql = f"""create table {table}(
id integer primary key autoincrement,
title text,
content text,
writer_id integer,
foreign key(writer_id) references Person(id)
)"""
cur.execute(sql) #SQL execution
self.con.commit() #Save
Note the foreign key (writer_id) references Person (id)
.
Here, once create an item of ʻintegrar with
writer_id integer, and under that
foreign key (
Recommended Posts