pythonでmysqlを操作

$ pip3 install mysql-connector-python

mysql側の操作

create database reddit;
use reddit
create table polygon (
	id int auto_increment primary key,
	title varchar(255),
	score int
);

insert into polygon (title, score) values
("Daily Polygon Discussion Thread", 25);

db.py

import mysql.connector

conn = mysql.connector.connect(
user='root',
passwd='hogehoge',
host='localhost',
db='reddit'
)

cur = conn.cursor()

sql = "select * from polygon"
cur.execute(sql)

rows = cur.fetchall()

for row in rows:
	print(row)

cur.close

conn.close

$ python3 db.py
(1, ‘Daily Polygon Discussion Thread’, 25)

### insertの場合

sql1 = "truncate table polygon"
cur.execute(sql1)

sql2 = "INSERT INTO polygon (title, score) VALUES('Daily Polygon Discussion Thread', 4)"
cur.execute(sql2)
conn.commit()

mysql> select * from polygon;
+—-+———————————+——-+
| id | title | score |
+—-+———————————+——-+
| 1 | Daily Polygon Discussion Thread | 4 |
+—-+———————————+——-+
1 row in set (0.00 sec)

OK
これに翻訳を入れたい

from googletrans import Translator
translator = Translator()

text = "Daily Polygon Discussion Thread"
 
translation = translator.translate(text, dest='ja', src='en')
print(translation.text)

$ python3 tran.py
毎日のポリゴンディスカッションスレッド

OK
これを全部つなげる

mysql> select * from polygon;
+—-+———————————+———————————————————–+——-+———————+
| id | title | title_ja | score | date |
+—-+———————————+———————————————————–+——-+———————+
| 1 | Daily Polygon Discussion Thread | 毎日のポリゴンディスカッションスレッド | 4 | 2022-01-08 05:14:42 |
+—-+———————————+———————————————————–+——-+———————+
1 row in set (0.00 sec)