Dino Geek, try to help you

How to use sqlite module in Python?


SQLite is a self-contained, serverless, and zero-configuration database engine, included by default in Python using the sqlite3 module. Below are steps to use it:

1. Import the sqlite3 module
```
import sqlite3
```

1. Create a connection to the SQLite database. If the database does not exists it will be created at this point.
```
conn = sqlite3.connect(‘test.db’)
``` The `connect()` function opens a connection to an SQLite database. The database name is `‘test.db’`. It will be created in the same directory where the script is executed from.

1. Get a Cursor object that operates in the context of Connection. A cursor is a Python object that enables you to work with the database.
```
cursor = conn.cursor()
```

1. Perform SQL operations using execute method of the Cursor.
```
cursor.execute(‘’‘CREATE TABLE employees (first_name text, last_name text, pay integer)’‘’)
``` This SQL statement creates a new table named `employees` with three columns: `first_name`, `last_name` and `pay`.

1. After doing some changes in the database, don’t forget to commit those changes by calling `commit()` method of the Connection object.
```
conn.commit()
```

1. Insert an entry into the table.
```
cursor.execute(“INSERT INTO employees VALUES (‘John’, ‘Doe’, 50000)”)
conn.commit()
```
1. Retrieve data from table.
```
cursor.execute(‘SELECT * FROM employees’)
print(cursor.fetchall())
```
1. Close the connection if no further operation is going to be performed.
```
conn.close()
```

So, that is pretty much how to use the sqlite3 module in Python to work with an SQLite database. This is fairly basic and it covers how to connect to an existing database, create a table, insert data, and query data. Nonetheless, these operations are the basics of working with any database.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use