Creating Table in python

Creating tables in the database to store data. Before creating tables, we have to select a database first.

Run the following code, to select realprogrammer database which we have selected.


## Connecting to the database

import mysql.connector as mysql

db = mysql.connect(
    host="localhost",
    user="root",
    passwd="[email protected]",
    db="realprogrammer"
)

cursor = db.cursor()

## creating a table called 'users' in the 'datacamp' database
cursor.execute(
    "CREATE TABLE user (name VARCHAR(255), user_name VARCHAR(255))")

Leave a Comment