Home Backend Development Python Tutorial Connect to multiple databases, make or generate SQL queries, analyze or visualize.

Connect to multiple databases, make or generate SQL queries, analyze or visualize.

Dec 12, 2024 pm 05:07 PM

Connect to multiple databases, make or generate SQL queries, analyze or visualize.

Source: https://github.com/HimrajDas/SQTHON

SQTHON

Connect to multiple databases, run raw SQL queries, perform analysis and make visualization.

Currently working on:

  • SqthonAI: generate SQL queries using a LLM of your choice ?
  • Security improvements?
  • New Features
  • custom exception for better error showcase ?

Package is not published to pypi yet and is being made using poetry. ?

Currently, this package will work on windows only.

And for your safety create a virtual environment.?

Installation ?

1. Clone the repository.

https://github.com/HimrajDas/SQTHON.git
Copy after login
Copy after login
cd sqthon
Copy after login
Copy after login

2. Install poetry (if not installed)

Using Windows powershell

(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -
Copy after login

Using Linux, macOS, Windows (WSL)

curl -sSL https://install.python-poetry.org | python3 -
Copy after login

Using pipx

pipx install poetry
Copy after login

3. Install dependencies using poetry

poetry install
Copy after login

Alternative install ?

pip install git https://github.com/HimrajDas/SQTHON

Now how do I use it?

1. Create a .env file in your project root. [a must-do step]

  • set database passwords like this: password

2. Let's connect to a database.

from sqthon import Sqthon
# Instantiate the class. Passwords gets fetch from the .env file (that's why you have to create it)
sq = Sqthon(dialect="mysql", user="root", host="localhost", service_instance_name="MySQL service instance name")

# Connects to a database
conn1 = sq.connect_to_database(database="dbname", local_infile=True) # local_infile controls the infile settings for the client.
conn2 = sq.connect_to_database("dbname")

# or you can connect like this:
conn3 = sq.connect_db.connect(database="dbname") # not preferred ❌.
Copy after login

If your MySQL server is not running then providing service_instance_name will start the server automatically.
If you are not running the script as an administrator, it will ask for admin privilege to start the server.

3. Queries.

Suppose you have a database named dummy ?

Connect to the database.

dummy_conn = sq.connect_to_database(database="dummy")
Copy after login

Now, how do I run some queries?

# Suppose, You have a table named sales in the dummy database.
query = """
SELECT customer_name FROM sales;
"""

customer_names = dummy_conn.run_query(query=query) # it will return the result as pandas dataframe.
Copy after login

run_query have several params other than query, they are: visualize: bool = False,
plot_type: str = None,
x=None,
y=None,
title=None.
If you make visualize=True and provide x, y and plot_type args then it will return a graph along with
the data which I don't think is good for later use of the variable.

4. Visualization.

https://github.com/HimrajDas/SQTHON.git
Copy after login
Copy after login

5. Importing CSV to a Table.

I have isolated this feature for several security reasons. What do I mean is that it uses a separate
engine to import the csv to a table which you don't need to worry about ?

It exists in the util.py as a separate method devoid of life from others.
Currently it supports mysql only.

Method Name: import_csv_to_mysqltable

Params it has:

  • user: str
  • host: str
  • database: str
  • csv_path: str
  • service_instance: str = None
  • table: str

user: username,
host: host,
database: database name,
csv_path: relative or absolute path to the csv file.

table: table name, if it doesn't exist then it will create the table according to the csv file.
You don't need to worry about data types. It will handle it.

cd sqthon
Copy after login
Copy after login

The above is the detailed content of Connect to multiple databases, make or generate SQL queries, analyze or visualize.. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What are regular expressions? What are regular expressions? Mar 20, 2025 pm 06:25 PM

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

See all articles