I tried to make a function to retrieve data from database column by column using sql with sqlite3 of python [sqlite3, sql, pandas]

motivation

I usually study only machine learning by myself, but I often see people who need knowledge of sql on twitter etc. So, by learning little by little in parallel, I would like to make a function from the point of extracting data first.

In this article we will:

I'm sorry if you already have it. SQL is a complete beginner so please take a look (-_-;)

Import of what is used this time

import sqlite3
import pandas as pd
import pandas.io.sql as sql

Make a DB

Create a trial DB using pandas. I would like to create a dataframe as follows. The value is appropriate. image.png

city = ["tokyo","kyoto","oosaka"]
num = [30,20,28]
df_1 = pd.DataFrame({"city":city,"number_of_city":num})
to dataframe-> database
path = "tamesi.db"
conn = sqlite3.connect(path)
cur = conn.cursor()
sql.to_sql(df_1,"study",conn,if_exists="replace",index=None)
cur.close()
con.close()

This time, I created it in a database called tamesi with a table called study.

Create a function to retrieve the specified column

I made.

def ret_column(db,table,column=""):
    assert bool(column), "no column is spcified"
    conn= sqlite3.connect(db)
    cur = conn.cursor()
    cur.execute("select {} from {}".format(column,table))
    val = cur.fetchall()
    cur.close()
    conn.close()
    return val

Put what you want to take out in the column and take it out. I tried to return an error if there is nothing in the column. All I'm doing is simply calling the sql statement with db and table specified.

Create a function to retrieve all data

Let's create a function that retrieves all the data. I made.

def ret_all(db,table):
    conn = sqlite3.connect(db)
    cur = conn.cursor()
    cur.execute("select * from {}".format(table))
    val = cur.fetchall()
    cur.close()
    conn.close()
    return val

This was easier because there was no column designation.

Try using

path = "tamesi.db"
table="study"
val_1 = ret_column(path,table,column="city")
val_2 = ret_all(path,table)
print(val_1)
print(val_2)

#Output below
[('tokyo',), ('kyoto',), ('oosaka',)]
[('tokyo', 30), ('kyoto', 20), ('oosaka', 28)]

I was able to confirm that it was taken out properly.

I will try to instantiate it

I tried to implement it easily with class.

class ret_from_db():
    def __init__(self,db):
        self.db = db

    def ret_column(self,table,column=""):
        assert bool(column), "no column is spcified"
        conn= sqlite3.connect(self.db)
        cur = conn.cursor()
        cur.execute("select {} from {}".format(column,table))
        val = cur.fetchall()
        cur.close()
        conn.close()
        return val

    def ret_all(self,table):
        conn = sqlite3.connect(self.db)
        cur = conn.cursor()
        cur.execute("select * from {}".format(table))
        val = cur.fetchall()
        cur.close()
        conn.close()
        return val

I just stuck the top two together, nothing special.

I tried using it
get_db = ret_from_db(db="tamesi.db")
val_1 = get_db.ret_column(table="study",column="city")
val_2 = get_db.ret_all(table="study")
print(val_1)
print(val_2)

#Output below
[('tokyo',), ('kyoto',), ('oosaka',)]
[('tokyo', 30), ('kyoto', 20), ('oosaka', 28)]

I was able to do it properly.

the end

I don't know if this is practical because I haven't used it (-_-;) Next time, if it can be read by pandas, it will be easier to use for sklean etc., so I would like to make it.

Recommended Posts

I tried to make a function to retrieve data from database column by column using sql with sqlite3 of python [sqlite3, sql, pandas]
I tried to get a database of horse racing using Pandas
I tried to make a regular expression of "amount" using Python
I tried to make a regular expression of "time" using Python
I tried to make a regular expression of "date" using Python
I tried to make a todo application using bottle with python
I tried using a database (sqlite3) with kivy
I tried to make a simple mail sending application with tkinter of Python
I tried scraping food recall information with Python to create a pandas data frame
I tried to make various "dummy data" with Python faker
I tried to make a stopwatch using tkinter in python
I tried to make a generator that generates a C # container class from CSV with Python
[2nd] I tried to make a certain authenticator-like tool with python
[3rd] I tried to make a certain authenticator-like tool with python
I tried to create a list of prime numbers with python
[Pandas] I tried to analyze sales data with Python [For beginners]
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I tried to make an image similarity function with Python + OpenCV
I tried to make a mechanism of exclusive control with Go
Python: I tried to make a flat / flat_map just right with a generator
[Data science basics] I tried saving from csv to mysql with python
I tried to communicate with a remote server by Socket communication with Python.
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to perform a cluster analysis of customers using purchasing data
I tried hundreds of millions of SQLite with python
A super introduction to Django by Python beginners! Part 3 I tried using the template file inheritance function
I want to make a game with Python
I tried to get CloudWatch data with Python
I tried to open the latest data of the Excel file managed by date in the folder with Python
A super introduction to Django by Python beginners! Part 2 I tried using the convenient functions of the template
I tried to make a ○ ✕ game using TensorFlow
[Patent analysis] I tried to make a patent map with Python without spending money
[Python] Smasher tried to make the video loading process a function using a generator
[Python] I tried to get the type name as a string from the type function
I tried to make a suspicious person MAP quickly using Geolonia address data
[Python & SQLite] I tried to analyze the expected value of a race with horses in the 1x win range ①
I tried to draw a route map with Python
I tried to automatically generate a password with Python3
I tried collecting data from a website with Scrapy
I tried to analyze J League data with Python
I tried reading data from a file using Node.js.
I tried using Python (3) instead of a scientific calculator
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
I tried to make a real-time sound source separation mock with Python machine learning
I tried to extract various information of remote PC from Python by WMI Library
[Python] I tried to automatically create a daily report of YWT with Outlook mail
I tried to aggregate & compare unit price data by language with Real Gachi by Python
I tried to compare the processing speed with dplyr of R and pandas of Python
I tried to make the weather forecast on the official line by referring to the weather forecast bot of "Dialogue system made with python".
I tried using the Python library from Ruby with PyCall
[Python] How to make a list of character strings character by character
I want to start a lot of processes from python
[Introduction to Pandas] I tried to increase exchange data by data interpolation ♬
I tried to sort a random FizzBuzz column with bubble sort.
[Python] I tried collecting data using the API of wikipedia
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried a stochastic simulation of a bingo game with Python
I tried to make a simple text editor using PyQt