工具
vscode + py3.6 + pylint
注:在pylint规范下代码的注释必不可少,直接上示例代码如下:
mysql封装
# -*- coding: UTF-8 -*-
"""必须要有注释才不会报错c0111错误"""
import pymysql
class Mysql(object):
"""
function main 我是类说明
"""
# 服务器配置信息
__CONFIG = {
'host':"localhost",
'port':"3306",
'username':"root",
'password':"密码",
'database':"db_name",
'charset' :"utf8"
}
def __init__(self):
try:
self.conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
passwd='',
db='your_db',
charset='utf8'
)
except Warning:
print('错误')
else:
print('连接成功')
self.cur = self.conn.cursor()
def create_table(self):
"""
function main 我是函数说明
"""
sql = 'create table testtb(id int, name varchar(10),age int)'
res = self.cur.execute(sql)
print(res)
def close(self):
"""
function main 我是函数说明
"""
self.cur.close()
self.conn.close()
def add(self):
"""
function main 我是函数说明
"""
sql = 'insert into testtb values(1,"Tom",18),(2,"Jerry",16),(3,"Hank",24)'
res = self.cur.execute(sql)
if res:
self.conn.commit()
else:
self.conn.rollback()
print(res)
def rem(self):
"""
function main 我是函数说明
"""
sql = 'delete from testtb where id=1'
res = self.cur.execute(sql)
if res:
self.conn.commit()
else:
self.conn.rollback()
print(res)
def mod(self):
"""
function main 我是函数说明
"""
sql = 'update testtb set name="Tom Ding" where id=2'
res = self.cur.execute(sql)
if res:
self.conn.commit()
else:
self.conn.rollback()
print(res)
def show(self):
"""
function main 我是函数说明
"""
sql = 'select * from import_lock'
self.cur.execute(sql)
res = self.cur.fetchall()
for i in res:
print(i)
"""
if __name__ == "__main__":
MYSQL = Mysql()
# MYSQL.create_table()
# MYSQL.add()
# MYSQL.mod()
# MYSQL.rem()
MYSQL.show()
MYSQL.close()
"""
应用
# -*- coding: UTF-8 -*-
"""必须要有注释才不会报错c0111错误"""
import mysql as sql
MYSQL = sql.Mysql()
MYSQL.show()
MYSQL.close()
单独写法
# -*- coding: UTF-8 -*-
"""必须要有注释才不会报错c0111错误"""
import pymysql.cursors
def main(strs):
"""
function main 我是函数说明
"""
print(strs)
# 连接数据库
CONN = pymysql.Connect(
host='localhost',
port=3306,
user='root',
passwd='密码',
db='your_dbname',
charset='utf8'
)
# 获取游标
CURSOR = CONN.cursor()
# 查询数据
SQL = "SELECT tar,inputime FROM tablename WHERE id in (%s) "
DATA = ('1,2',)
CURSOR.execute(SQL % DATA)
for row in CURSOR.fetchall():
print("Tar:%s\tInputime:%s" % row)
print('共查找出', CURSOR.rowcount, '条数据')
main('hello world!')
# 关闭连接
CURSOR.close()
CONN.close()
# 运行结果
# Tar:xxxfile_20171102.tar.gz Inputime:1511316146
# Tar:xxxfile_20171103.tar.gz Inputime:1511316306
# 共查找出 2 条数据
# hello world!