python
  1. python-creating-tables

Creating Tables in Python

Python provides multiple libraries to work with databases. One such library is the 'sqlite3' library that allows us to create tables for database management.

Syntax

Here is the basic syntax for creating a table in Python:

import sqlite3

# Create a connection object
conn = sqlite3.connect('database_name.db')

# Create a cursor object
cursor = conn.cursor()

# Execute a query to create a table
cursor.execute('CREATE TABLE table_name (column1 datatype, column2 datatype, ..)')

# Commit the changes
conn.commit()

# Close the connection
conn.close()

Example

Let's say we want to create a table named 'students' with columns 'name', 'roll_no', and 'marks'. Here is an example of how to do this:

import sqlite3

# Create a connection object
conn = sqlite3.connect('school.db')

# Create a cursor object
cursor = conn.cursor()

# Execute a query to create the students table
cursor.execute('''CREATE TABLE students
                  (name TEXT, roll_no TEXT, marks INT)''')

# Commit the changes
conn.commit()

# Close the connection
conn.close()

Output

When the above code is executed, a new database file named 'school.db' will be created with a table named 'students' that has columns 'name', 'roll_no', and 'marks'.

Explanation

The code above starts by importing the 'sqlite3' library, which allows us to work with databases. We then create a connection object that connects to the 'school.db' database file using the sqlite3.connect() method.

Next, we create a cursor object using the conn.cursor() method, which allows us to execute SQL queries. We then use the cursor.execute() method to execute a query that creates a table named 'students' with three columns: 'name' of data type 'TEXT', 'roll_no' of data type 'TEXT', and 'marks' of data type 'INT'.

After executing the query, we commit the changes to the database using conn.commit(), and finally close the connection using conn.close().

Use

Creating tables is an important step in managing and organizing data in a database. By creating tables, we can establish a structure for the data and ensure data consistency.

Important Points

  • The sqlite3.connect() method creates a connection object that represents the database file.
  • The conn.cursor() method creates a cursor object that allows us to execute SQL queries.
  • The cursor.execute() method is used to execute SQL queries.
  • After executing a query that changes the database, we need to commit the changes using conn.commit().
  • Always remember to close the connection using conn.close() after finishing database operations.

Summary

In this tutorial, we learned how to create tables in Python using the 'sqlite3' library. We covered the basic syntax and structure of a table-creating SQL query and provided an example of how to create a table in Python. We also discussed the importance of creating tables in database management and mentioned some important points to keep in mind while working with databases.

Published on: