[root@centos-01 python]# cat test001.py
#coding=utf-8
import MySQLdb
class MysqlSearch(object):
def __init__(self):
self.get_conn()
#获取连接
def get_conn(self):
try:
self.conn = MySQLdb.connect(
host='127.0.0.1',
user='root',
passwd='123456',
db='news',
port=3306,
charset='utf8'
)
except MySQLdb.Error as e:
print('Error: %s' % e)
def get_one(self):
sql = 'SELECT * FROM `news` where `types` = %s order by `created_at` DESC;'
cursor = self.conn.cursor()
cursor.execute(sql, ('百家', ))
rest = cursor.fetchone()
print(rest)
cursor.close()
self.close_conn()
def close_conn(self):
try:
if self.conn:
# 关闭链接
self.conn.close()
except MySQLdb.Error as e:
print('Error: %s' % e)
def main():
obj = MysqlSearch()
obj.get_one()
if __name__ == '__main__':
main()
[root@centos-01 python]# python test001.py
File “test001.py”, line 10
def get_conn(self):
^
IndentationError: unindent does not match any outer indentation level
我是直接在 centos7 下直接运行的, 一直报这个错误, 不知道什么原因
一次实战同时掌握Python操作MySQL,MongoDB,Redis 三大数据库使用技巧
了解课程
1.5k 13
1.4k 12
3.5k 12
2.0k 11
6.2k 10