update:完善数据库和Kafka相关结构
This commit is contained in:
23
dao/db/example.py
Normal file
23
dao/db/example.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from dao.db.util import get_connet
|
||||
|
||||
def exmaple_query():
|
||||
with get_connet() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select * from user")
|
||||
for row in cursor.fetchall():
|
||||
print(row)
|
||||
cursor.close()
|
||||
|
||||
def exmaple_insert():
|
||||
with get_connet() as conn:
|
||||
with conn.cursor() as cursor:
|
||||
cursor.execute("insert into user(name, age) values('test', 18)")
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
|
||||
def exmaple_update():
|
||||
with get_connet() as conn:
|
||||
with conn.cursor() as cursor:
|
||||
cursor.execute("update user set age=19 where name='test'")
|
||||
conn.commit()
|
||||
cursor.close()
|
15
dao/db/util.py
Normal file
15
dao/db/util.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from conf.util import get_config_object
|
||||
import pymysql
|
||||
|
||||
conf = get_config_object()
|
||||
|
||||
|
||||
|
||||
host = conf.get('database', 'host')
|
||||
port = conf.getint('database', 'port')
|
||||
user = conf.get('database', 'user')
|
||||
password = conf.get('database', 'password')
|
||||
database = conf.get('database', 'database')
|
||||
|
||||
def get_connet() -> pymysql.Connection:
|
||||
return pymysql.connect(host=host, port=port, user=user, password=password, database=database, charset='utf8')
|
Reference in New Issue
Block a user