How to use Mysql in python

Introduction

In this article, I will describe how to use Mysql as a method to visualize based on the data received by Yolo. I would like to make an article about how to use Mysql by referring to the following article.

Python + mysql-connector-Summary of how to use python https://qiita.com/valzer0/items/2f27ba98397fa7ff0d74

environment

python8.x Use xampp (Mysql)

Library you are using

mysql.connector

Database configuration

databasename:mask table:mask-ok-or-ng1 Column: 3

name format
day day
time time
ok-or-ng text

Implemented code

def sqlng():
 day = datetime.date.today()
 time1 = datetime.datetime.now()
 conn = mydb.connect(
        host='127.0.0.1',
        port='3306',
        user='userid',#Enter user id
        password='password',#Enter password
        database='databasename'#Enter the name of the database
    )
 #Set to reconnect when the connection is lost
 conn.ping(reconnect=True)
 #Check if you can connect
 print(conn.is_connected())
 cur = conn.cursor()
 time2=str(time1.hour)+":"+str(time1.minute)+":"+str(time1.second)
 print(day)
 print(time2)
 cur.execute("INSERT INTO `mask-ok-or-ng1` (`day`, `time`, `ok-or-ng`)"+ "VALUES"+ "("+"'"+str(day)+"'"+","+"'"+str(time1)+"'"+","+"'NG')")
 conn.commit()
 cur.close()
 conn.close()

As a point to devise, when inserting an arbitrary variable into the database, I got an error that the tuple type can not be used with the method of the reference article, so I did it with the above method.

#Insert multiple data at once with execute many
records = [
  (5, 'MONA', 3000),
  (6, 'XP', 1000),
]
cur.executemany("INSERT INTO test_table VALUES (%s, %s, %s)", records)

I got an error this way. Here is the following.

 cur.execute("INSERT INTO `mask-ok-or-ng1` (`day`, `time`, `ok-or-ng`)"+ "VALUES"+ "("+"'"+str(day)+"'"+","+"'"+str(time1)+"'"+","+"'NG')")

Finally

I've left this as a personal memo, so I hope it's helpful.

Recommended Posts

How to use Mysql in python
How to use SQLite in Python
How to use ChemSpider in Python
How to use PubChem in Python
[Introduction to Python] How to use class in Python?
How to use __slots__ in Python class
How to use regular expressions in Python
How to use is and == in Python
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to develop in Python
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to use the C library in Python
How to use Python Image Library in python3 series
How to use tkinter with python in pyenv
[For beginners] How to use say command in python!
[Python] How to do PCA in Python
Python: How to use async with
How to use classes in Theano
How to collect images in Python
How to use Requests (Python Library)
[Python] How to use list 3 Added
How to use OpenPose's Python API
How to wrap C in Python
How to use FTP with Python
Python: How to use pydub (playback)
I tried to summarize how to use pandas in python
How to use the model learned in Lobe in Python
How to use python zip function
How to handle Japanese in Python
[Python] How to use Typetalk API
How to use the __call__ method in a Python class
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Comparison of how to use higher-order functions in Python 2 and 3
How to search by string to use mysql json_contains in SQLAlchemy
How to use calculated columns in CASTable
[Python] Summary of how to use pandas
How to access environment variables in Python
How to do R chartr () in Python
[Itertools.permutations] How to put permutations in Python
How to use Google Test in C
[python] How to use __command__, function explanation
How to work with BigQuery in Python
[Python] How to use import sys sys.argv
How to get a stacktrace in python
How to display multiplication table in python
Easy way to use Wikipedia in Python
How to extract polygon area in Python
[Python] Organizing how to use for statements
Memorandum on how to use gremlin python
How to check opencv version in python
[Python2.7] Summary of how to use unittest
How to use Anaconda interpreter in PyCharm
python: How to use locals () and globals ()