Monday, April 21, 2025

Create MySQL Table - One or More - Python

import mysql.connector

db_handle = mysql.connector.connect(
    host = 'localhost',
    user = 'root',
    passwd = '12345',
    database = '192_168_0_103'
)

print(db_handle)

kursor = db_handle.cursor()

kursor.execute("CREATE TABLE table_1 (name VARCHAR(255), address VARCHAR(255))")

kursor.execute("CREATE TABLE table_2 (name VARCHAR(255), address VARCHAR(255))")

This code imports two modules, "mysql.connector" and "time".

The "mysql.connector" module is used to connect to a MySQL database server, and execute queries on it. It is a Python library that provides a standardized way to interact with the MySQL database.

The code establishes a connection to the MySQL database server running on "localhost" with the username "root" and password "12345", and specifies the database that we want to connect to - '192_168_0_103'. It then creates a cursor object to execute queries on the database.

The code then executes two queries using the cursor object:

  • The first query creates a new table called "table_1" with two columns: "name" and "address". The data type for both columns is "VARCHAR(255)", which means that each column can store a string of up to 255 characters.

  • The second query creates another new table called "table_2" with the same columns ("name" and "address") and data type as the first table.

Both queries are executed using the cursor object, and the tables are created in the currently connected database - '192_168_0_103'.

After executing both queries, the code does not perform any other action and terminates. 

mysql> use 192_168_0_103;
Database changed
mysql> show tables;
+-------------------------+
| Tables_in_192_168_0_103 |
+-------------------------+
| table_1                 |
| table_2                 |
+-------------------------+
2 rows in set (0.00 sec)

mysql> describe table_1;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| name    | varchar(255) | YES  |     | NULL    |       |
| address | varchar(255) | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
2 rows in set (0.05 sec)

mysql>

The first command "use 192_168_0_103;" switches to the database named '192_168_0_103', making it the active database for subsequent queries.

The second command "show tables;" lists all the tables that are present in the currently active database. In this case, there are two tables in the '192_168_0_103' database - "table_1" and "table_2".

The third command "describe table_1;" displays the structure of the "table_1" table. It shows the names of the columns and their data types. In this case, the table has two columns named "name" and "address", both of type "VARCHAR(255)".

The output of each command is shown along with the time taken to execute the command, in seconds.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...