|
|
|
@ -103,52 +103,52 @@ SQLite functionality comes as part of the default python installation (no packag
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
```python
|
|
|
|
import sqlite3
|
|
|
|
import sqlite3
|
|
|
|
con = sqlite3.connect("mydb.db")
|
|
|
|
connection = sqlite3.connect("mydb.db")
|
|
|
|
|
|
|
|
|
|
|
|
# close connection
|
|
|
|
# close connection
|
|
|
|
con.close()
|
|
|
|
connection.close()
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
Create a table (this could also be done in `psycopg2-binary` as well). Note there is no need to close the cursor
|
|
|
|
Create a table (this could also be done in `psycopg2-binary` as well). Note there is no need to close the cursor
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
```python
|
|
|
|
cur = con.cursor()
|
|
|
|
cursor = connection.cursor()
|
|
|
|
cur.execute("CREATE TABLE people (name, age)")
|
|
|
|
cursor.execute("CREATE TABLE people (name, age)")
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
Very similar, except that `cur.execute` returns a result set that has the `fetchone`/`fetchall` functionality
|
|
|
|
Very similar, except that `cur.execute` returns a result set that has the `fetchone`/`fetchall` functionality
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
```python
|
|
|
|
res = cur.execute("SELECT * FROM people")
|
|
|
|
result = cursor.execute("SELECT * FROM people")
|
|
|
|
print(res.fetchall())
|
|
|
|
print(result.fetchall())
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
Selecting a single row is similar to `psycopg2-binary`, but you use `?` instead of `%s`
|
|
|
|
Selecting a single row is similar to `psycopg2-binary`, but you use `?` instead of `%s`
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
```python
|
|
|
|
res = cur.execute("SELECT * FROM people WHERE name = ?", ['Matt'])
|
|
|
|
result = cursor.execute("SELECT * FROM people WHERE name = ?", ['Matt'])
|
|
|
|
print(res.fetchone())
|
|
|
|
print(result.fetchone())
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
`INSERT` works as expected
|
|
|
|
`INSERT` works as expected
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
```python
|
|
|
|
cur.execute("INSERT INTO people (name, age) VALUES (?, ?)", ['Zagthorp', 543])
|
|
|
|
cursor.execute("INSERT INTO people (name, age) VALUES (?, ?)", ['Zagthorp', 543])
|
|
|
|
con.commit()
|
|
|
|
connection.commit()
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
So does `DELETE`
|
|
|
|
So does `DELETE`
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
```python
|
|
|
|
cur.execute("DELETE FROM people WHERE name = ?", ['Bilbo'])
|
|
|
|
cursor.execute("DELETE FROM people WHERE name = ?", ['Bilbo'])
|
|
|
|
con.commit()
|
|
|
|
connection.commit()
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
and `UPDATE`
|
|
|
|
and `UPDATE`
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
```python
|
|
|
|
cur.execute("UPDATE people SET name = ? WHERE name = ?", ['Bilbo', 'Matthew'])
|
|
|
|
cursor.execute("UPDATE people SET name = ? WHERE name = ?", ['Bilbo', 'Matthew'])
|
|
|
|
con.commit()
|
|
|
|
connection.commit()
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
At any point, you can use the `sqlite3` terminal command to interface with the database, just like you would with `psql`
|
|
|
|
At any point, you can use the `sqlite3` terminal command to interface with the database, just like you would with `psql`
|
|
|
|
|