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
python8.x Use xampp (Mysql)
mysql.connector
databasename:mask table:mask-ok-or-ng1 Column: 3
| name | format | 
|---|---|
| day | day | 
| time | time | 
| ok-or-ng | text | 
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')")
I've left this as a personal memo, so I hope it's helpful.
Recommended Posts