# Python SQLite API
###### tags: `Python Notes`
## Importing SQLite in Python
```python=
import sqlite3
from sqlite3 import Error
```
## Getting connection
```python=
# after importing
conn = sqlite3.connect(db_name)
# db_name is the name of database
```
## Executing a SQLite statement
```python=
# after connecting
c = conn.cursor() # this is essential before executing any statement
c.execute(stmt)
# stmt is a full SQLite statement
```
## Executing a SQLite prepared Statement
```python=
# after connecting
c.execute(pstmt, values)
# pstmt is a SQLite prepared statement
# value is a tupple
```
## Comminting changes
When to commit: edit, delete, alter table
When not needed: query, creating table
```python=
# after executing
conn.commit()
```
## Closing connecion
Always close connection after using. Do not try to use after colsing.
```python=
# use database here
# after using
conn.close()
# do not use database here
```
## Error handing
```python=
try:
# connetion, cursor, execution
except Error as e:
print(e)
finally:
#close connection
```
|Made with :hearts: by [Kavyas](https://hackmd.io/team/kavyas "View our other notes")|
|-|
|Learn SQLite on https://www.sqlitetutorial.net/|