You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.1 KiB
48 lines
1.1 KiB
6 months ago
|
'''
|
||
|
数据库连接
|
||
|
'''
|
||
|
import pymysql
|
||
|
|
||
|
class dbConnect():
|
||
|
def __init__(self):
|
||
|
self.conn = pymysql.connect(user='wang123',
|
||
|
password='wang123',
|
||
|
database='myweb',
|
||
|
host='127.0.0.1',
|
||
|
charset='utf8')
|
||
|
self.cursor = self.conn.cursor()
|
||
|
|
||
|
def dbManage(self,sql=None):
|
||
|
'''
|
||
|
实现对数据表里的定义、增加、删除、修改操作
|
||
|
:param sql:
|
||
|
:return:
|
||
|
'''
|
||
|
flag = False
|
||
|
try:
|
||
|
self.cursor.execute(sql)
|
||
|
self.conn.commit()
|
||
|
flag=True
|
||
|
except:pass
|
||
|
return flag
|
||
|
|
||
|
def dbQuery(self,sql):
|
||
|
'''
|
||
|
数据库的查询业务
|
||
|
:param sql:
|
||
|
:return:
|
||
|
'''
|
||
|
try:
|
||
|
self.cursor.execute(sql)
|
||
|
return self.cursor.fetchall()
|
||
|
except:
|
||
|
return None
|
||
|
|
||
|
|
||
|
if __name__=='__main__':
|
||
|
d = dbConnect()
|
||
|
# print(d.dbManage(sql="insert into dblog values(null,'111','2','aap','okdfdfd')"))
|
||
|
print(d.dbQuery(sql="select * from dblog"))
|
||
|
|
||
|
|