Contents

Lec36-Databases

Databases

method of tables

create and drop tables

/lec36-databases/image.png

🤓

/lec36-databases/image-1.png

1
2
create table numbers (n, note);
create table numbers (n UNIQUE, note DEFAULT 'unknown');

/lec36-databases/image-2.png

1
drop table if exists t;

insert data into tables

/lec36-databases/image-3.png

1
2
insert into t values (1, 'one');
insert into t(col1) values (2);

update

1
update t set col1 = 3 where col2 = 'two';

delete

1
delete from t where col1 = 1;

Python and SQL

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import sqlite3

# connect to the database
conn = sqlite3.connect('mydatabase.db')

conn.execute('''CREATE TABLE IF NOT EXISTS mytable
               (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)''')

conn.execute("INSERT INTO mytable (name, age) VALUES ('Alice', 25)")
# a cursor object is used
conn.commit() # save changes into db file

database connection

一个db可以被多个程序连接操作 😋