avatarDataScience Nexus

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2320

Abstract

rds to the table.</p><div id="317c"><pre><span class="hljs-comment"># Sample data</span> employees = [ (1, <span class="hljs-string">'Alice Johnson'</span>, 25, <span class="hljs-string">'Marketing'</span>), (2, <span class="hljs-string">'Bob Smith'</span>, 30, <span class="hljs-string">'Sales'</span>), (3, <span class="hljs-string">'Charlie Brown'</span>, 35, <span class="hljs-string">'IT'</span>), (4, <span class="hljs-string">'David White'</span>, 28, <span class="hljs-string">'Finance'</span>), (5, <span class="hljs-string">'Eve Black'</span>, 40, <span class="hljs-string">'HR'</span>), (6, <span class="hljs-string">'Frank Green'</span>, 45, <span class="hljs-string">'Operations'</span>), (7, <span class="hljs-string">'Grace Kelly'</span>, 29, <span class="hljs-string">'IT'</span>), (8, <span class="hljs-string">'Hank Ford'</span>, 50, <span class="hljs-string">'Logistics'</span>), (9, <span class="hljs-string">'Ivy Adams'</span>, 27, <span class="hljs-string">'Sales'</span>), (10, <span class="hljs-string">'Jack Carter'</span>, 32, <span class="hljs-string">'Marketing'</span>), (11, <span class="hljs-string">'Kim Lee'</span>, 33, <span class="hljs-string">'Finance'</span>), (12, <span class="hljs-string">'Laura Hill'</span>, 31, <span class="hljs-string">'HR'</span>), (13, <span class="hljs-string">'Mike Brown'</span>, 41, <span class="hljs-string">'Operations'</span>), (14, <span class="hljs-string">'Nancy Drew'</span>, 29, <span class="hljs-string">'IT'</span>), (15, <span class="hljs-string">'Oscar Wilde'</span>, 26, <span class="hljs-string">'Logistics'</span>), (16, <span class="hljs-string">'Paula Green'</span>, 34, <span class="hljs-string">'Marketing'</span>), (17, <span class="hljs-string">'Quincy Adams'</span>, 37, <span class="hljs-string">'Sales'</span>), (18, <span class="hljs-string">'Rachel Black'</span>, 30, <span class="hljs-string">'Finance'</span>), (19, <span class="hljs-string">'Steve Jobs'</span>, 39, <span class="hljs-string">'IT'</span>), (20, <span class="hljs-string">'Tom Ford'</span>, 28, <span class="hljs-string">'HR'</span>) ]

<span class="hljs-comment"># Insert data into the table</span> cursor.executemany(<span class="hljs-string">'INSERT INTO employees VALUES (?,

Options

?, ?, ?)'</span>, employees) <span class="hljs-comment"># Commit the changes</span> connection.commit() <span class="hljs-built_in">print</span>(<span class="hljs-string">"20 records inserted successfully!"</span>)</pre></div><p id="eea2">Explanation:</p><ul><li><b>executemany()</b>: Adds multiple rows to the table in one go.</li><li><b>?</b>: Placeholder for values, making it easier to insert data dynamically.</li><li><b>commit()</b>: Saves the changes to the database.</li></ul><h1 id="17f1">Step 4: Viewing the Data</h1><p id="b2ed">Let’s verify that the records have been added.</p><div id="7574"><pre># Retrieve <span class="hljs-keyword">and</span> display <span class="hljs-keyword">all</span> records cursor.execute(<span class="hljs-string">'SELECT * FROM employees'</span>) <span class="hljs-keyword">rows</span> <span class="hljs-operator">=</span> cursor.fetchall()

<span class="hljs-keyword">for</span> <span class="hljs-type">row</span> <span class="hljs-keyword">in</span> <span class="hljs-keyword">rows</span>: print(<span class="hljs-type">row</span>)

<span class="hljs-keyword">Close</span> the connection

connection.close()</pre></div><p id="ab43">This code fetches and prints all records in the table. You should see 20 records displayed in the output.</p><h1 id="d84e">Real-World Example</h1><p id="f2b7">Imagine you run a small business, and you need to track your employees. Instead of using spreadsheets, you can use Python and SQLite to create a lightweight database. This approach is more scalable and easier to integrate with web applications.</p><p id="066b">For instance, you can expand this database to include salary information, performance reviews, or attendance records. You can also connect this database to a web interface for easier management.</p><h1 id="8e30">Final Thoughts</h1><p id="2ad9">Creating tables and adding records in Python is a powerful yet beginner-friendly way to handle data. By following the steps above, you now have the foundation to build more advanced applications, whether for personal projects or business needs.</p><p id="5487">The best way to learn is by doing — so try creating your own tables with different data sets. Who knows? This could be the start of your journey into database management and software development!</p></article></body>

Creating a Table and Adding Records in Python: A Beginner-Friendly Guide

Working with data is a key skill in programming. One of the simplest ways to store and manipulate data is by using tables. In this article, we’ll learn how to create a table and add 20 records using Python. Don’t worry if you’re new to programming — I’ll explain everything step-by-step with real-world examples.

Step 1: Setting Up the Environment

Before we start, make sure Python is installed on your computer. You can download it from python.org.

Next, install SQLite, a lightweight database that comes pre-installed with Python. We’ll use SQLite to create our table.

pip install sqlite3  # For older Python versions, but usually no need to install.

Step 2: Creating the Table

Now, let’s write a Python script to create a database, define a table, and insert 20 records.

import sqlite3

# Connect to the database (creates a file if it doesn't exist)
connection = sqlite3.connect('company.db')
cursor = connection.cursor()
# Create a table
cursor.execute('''CREATE TABLE IF NOT EXISTS employees (
                  id INTEGER PRIMARY KEY,
                  name TEXT,
                  age INTEGER,
                  department TEXT)''')
print("Table created successfully!")

Here’s what’s happening:

  • sqlite3.connect(): Connects to a database named company.db. If it doesn’t exist, Python creates it.
  • cursor(): Allows us to execute SQL commands.
  • CREATE TABLE: Creates a table named employees with four columns.

Step 3: Inserting Records

Let’s add 20 sample employee records to the table.

# Sample data
employees = [
    (1, 'Alice Johnson', 25, 'Marketing'),
    (2, 'Bob Smith', 30, 'Sales'),
    (3, 'Charlie Brown', 35, 'IT'),
    (4, 'David White', 28, 'Finance'),
    (5, 'Eve Black', 40, 'HR'),
    (6, 'Frank Green', 45, 'Operations'),
    (7, 'Grace Kelly', 29, 'IT'),
    (8, 'Hank Ford', 50, 'Logistics'),
    (9, 'Ivy Adams', 27, 'Sales'),
    (10, 'Jack Carter', 32, 'Marketing'),
    (11, 'Kim Lee', 33, 'Finance'),
    (12, 'Laura Hill', 31, 'HR'),
    (13, 'Mike Brown', 41, 'Operations'),
    (14, 'Nancy Drew', 29, 'IT'),
    (15, 'Oscar Wilde', 26, 'Logistics'),
    (16, 'Paula Green', 34, 'Marketing'),
    (17, 'Quincy Adams', 37, 'Sales'),
    (18, 'Rachel Black', 30, 'Finance'),
    (19, 'Steve Jobs', 39, 'IT'),
    (20, 'Tom Ford', 28, 'HR')
]


# Insert data into the table
cursor.executemany('INSERT INTO employees VALUES (?, ?, ?, ?)', employees)
# Commit the changes
connection.commit()
print("20 records inserted successfully!")

Explanation:

  • executemany(): Adds multiple rows to the table in one go.
  • ?: Placeholder for values, making it easier to insert data dynamically.
  • commit(): Saves the changes to the database.

Step 4: Viewing the Data

Let’s verify that the records have been added.

# Retrieve and display all records
cursor.execute('SELECT * FROM employees')
rows = cursor.fetchall()

for row in rows:
    print(row)
# Close the connection
connection.close()

This code fetches and prints all records in the table. You should see 20 records displayed in the output.

Real-World Example

Imagine you run a small business, and you need to track your employees. Instead of using spreadsheets, you can use Python and SQLite to create a lightweight database. This approach is more scalable and easier to integrate with web applications.

For instance, you can expand this database to include salary information, performance reviews, or attendance records. You can also connect this database to a web interface for easier management.

Final Thoughts

Creating tables and adding records in Python is a powerful yet beginner-friendly way to handle data. By following the steps above, you now have the foundation to build more advanced applications, whether for personal projects or business needs.

The best way to learn is by doing — so try creating your own tables with different data sets. Who knows? This could be the start of your journey into database management and software development!

Python
Python Programming
Python3
Sql
Table
Recommended from ReadMedium