(For myself) Flask_8 (Add / Edit / Delete in database with python)

item

  1. A quick review of the database
  2. Add to the database
  3. Editing in the database
  4. Delete in database

1. Easy review

python


from flask import Flask, render_template
import pymysql
app = Flask(__name__)

def getConnection():
    return pymysql.connect(
        host='localhost',
        db='mydb',
        user='root',
        password='',
        charset='utf8',
        cursorclass=pymysql.cursors.DictCursor
    )

@app.route('/')
def select_sql():
  
    connection = getConnection()
    message = "Hello world"

    sql = "SELECT * FROM players"
    cursor = connection.cursor()
    cursor.execute(sql)
    players = cursor.fetchall()

    cursor.close()
    connection.close()

    return render_template('view.html', message = message, players = players)

--Declared to edit with pymysql --Select the database information to edit with .connect () --Enter SQL command in the database selected with def select_sql

2. Addendum in the database

python


@app.route('/')
def select_sql():
  
    connection = getConnection()
    message = "Hello world"

    cursor = connection.cursor()

    sql = "INSERT INTO players (name, level, job_id) VALUES ('Kirishima No. 1', 1, 1)"
    cursor.execute(sql)
    connection.commit()

    sql = "SELECT * FROM players"
    cursor.execute(sql)
    players = cursor.fetchall()

    cursor.close()
    connection.close()

    return render_template('view.html', message = message, players = players)

--The basics are the same --It is better to do cursor = connection.cursor () first so that you can refer to cursor. --sql =" INSERT INTO players (name, level, job_id) VALUES ('Kirishima No. 1', 1, 1) ", select table with ʻINSERT INTO

, and select the column you want to edit. After that, add data to the column selected by VALUES ('Kirishima No. 1', 1, 1). <br> Column, order and order of additional data need to be linked --Is the SQL command running on the database with connection.commit ()`?

3. Editing in the database

python


    sql = "UPDATE players SET level = 10 WHERE id = 12"
    cursor.execute(sql)
    connection.commit()

--Replace ʻINSERTwith this --ʻUPDATE <table name>to select the table you want to edit. SET level = 10 to select and instruct to change the value of the selected level column to 10, and WHERE id = 12 to select which of the level columns. Select whether the data will be edited

4. Delete in database

python


    sql = "DELETE FROM players WHERE id = 12"
    cursor.execute(sql)
    connection.commit()

--Basically the same --Declare to delete from the players table with DELETE FROM players --WHERE id = 12 "What?" "The guy with id 12"

5. At the end

――I couldn't do much today ――It may be better to take a rest for about a day at a time

Recommended Posts

(For myself) Flask_8 (Add / Edit / Delete in database with python)
(For myself) Flask_7 (Open database from Flask)
(For myself) Flask_5 (Add to txt file)
Tips for dealing with binaries in Python
Page cache in Python + Flask with Flask-Caching
(For myself) Put Flask in VS Code
Process multiple lists with for in Python
AtCoder cheat sheet in python (for myself)
python [for myself]
Add quotation marks ">" for replying emails in Python3
[Introduction for beginners] Working with MySQL in Python
Settings for getting started with MongoDB in python
Edit fonts in Python
Programming with Python Flask
Python memo (for myself): Array
Scraping with selenium in Python
Working with LibreOffice in Python
Scraping with chromedriver in python
Tips for developing apps with Azure Cosmos DB in Python
[LDAP environment construction: 7] Add / search / change / delete users with Python
Debugging with pdb in Python
Create a child account for connect with Stripe in Python
[For beginners] Summary of standard input in Python (with explanation)
Search for strings in Python
Python Tkinter notes (for myself)
Scraping with Selenium in Python
Techniques for sorting in Python
(For myself) Flask_6 (Open db from python, Mysql basic (phpMyAdmin))
Scraping with Tor in Python
Tweet with image in Python
Combined with permutations in Python
Web application with Python + Flask ② ③
About "for _ in range ():" in python
Web application with Python + Flask ④
Dockerfile with the necessary libraries for natural language processing in python
Number recognition in images with Python
Check for memory leaks in Python
SNS Python basics made with Flask
Check for external commands in python
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Next, use Python (Flask) for Heroku!
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
Heatmap with Dendrogram in Python + matplotlib
Edit videos in Python using MoviePy
Read files in parallel with Python
Password generation in texto with python
Use OpenCV with Python 3 in Window
Run unittests in Python (for beginners)
Delete multiple elements in python list
Until dealing with python in Atom
Getting Started with Python for PHPer-Functions
Application development with Docker + Python + Flask
Get started with Python in Blender
Working with DICOM images in Python
[Python] How to create a dictionary type list, add / change / delete elements, and extract with a for statement