请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

关于self和init的问题

想问一下源代码里的self,一般指的是什么?不写可以吗?
另外,我把讲课的代码copy了之后,我发现在main方法里要加上 init() 才能运行成功,假如删掉就会报错,说没有connection

import pymysql as my

class Mysqlsearch(object):
    def __int__(self):
        self.get_conn()

    def get_conn(self):
        try:
            self.conn = my.connect(
                host='127.0.0.1', user='vince', passwd='', db='imooctest', port=3306, charset='utf8'
            )
        except Exception as e:
            print('ERROR:%s' % e)

    def close_conn(self):
        try:
            if self.conn:
                # 关闭链接
                self.conn.close()
        except Exception as e:
            print('Error: %s' % e)

    def get_one(self):
        # 准备SQL
        sql = 'SELECT * FROM news WHERE `types` = %s;'
        # 找到cursor
        cursor = self.conn.cursor()
        # 执行SQL
        cursor.execute(sql, ('央视', ))
        # print(dir(cursor))
        # 拿到结果
        rest = dict(zip([k[0] for k in cursor.description], cursor.fetchone()))
        # 处理数据
        # 关闭cursor/链接
        cursor.close()
        self.close_conn()
        return rest

    def get_more(self):
        # 准备SQL
        sql = 'SELECT * FROM `news` WHERE `types` = %s ORDER BY `created_at` DESC;'
        # 找到cursor
        cursor = self.conn.cursor()
        # 执行SQL
        cursor.execute(sql, ('央视', ))
        # print(dir(cursor))
        # 拿到结果
        rest = [dict(zip([k[0] for k in cursor.description], row))
            for row in cursor.fetchall()]
        # 处理数据
        # 关闭cursor/链接
        cursor.close()
        self.close_conn()
        return rest

    def get_more_by_page(self, page, page_size):
        ''' 分页查询数据 '''
        offset = (page - 1) * page_size
        # 准备SQL
        sql = 'SELECT * FROM `news` WHERE `types` = %s ORDER BY `create_at` DESC LIMIT %s, %s;'
        # 找到cursor
        cursor = self.conn.cursor()
        # 执行SQL
        cursor.execute(sql, ('百家', offset, page_size))
        # print(dir(cursor))
        # 拿到结果
        rest = [dict(zip([k[0] for k in cursor.description], row))
            for row in cursor.fetchall()]
        # 处理数据
        # 关闭cursor/链接
        cursor.close()
        self.close_conn()
        return rest

    def add_one(self):
        ''' 插入数据 '''
        # 受影响的行数
        row_count = 0
        try:
            # 准备SQL
            sql = (
                "INSERT INTO `news`(`title`,`image`, `content`, `types`, `is_valid`) VALUE"
                "(%s, %s, %s, %s, %s);"
            )
            # 获取链接和cursor
            cursor = self.conn.cursor()
            # 执行sql
            # 提交数据到数据库
            cursor.execute(sql, ('标题11', '/static/img/news/01.png', '新闻内容5', '推荐', 1))
            # cursor.execute(sql, ('标题12', '/static/img/news/01.png', '新闻内容6', '推荐', 1))
            # 提交事务
            self.conn.commit()
        except :
            print('error')
            # 回滚事务
            self.conn.rollback()
        # 执行最后一条SQL影响的行数
        row_count = cursor.rowcount
        # 关闭cursor和链接
        cursor.close()
        self.close_conn()
        # row_count > 0 表示成功
        return row_count > 0


def main():
    obj = Mysqlsearch()
    obj.__int__()
    rest=obj.get_one()
    print(rest)
    # rest = obj.get_more()
    # for item in rest:
    #     print(item)
    #     print('------')

    # rest = obj.get_more_by_page(2, 3)
    # for item in rest:
    #     print(item)
    #     print('------')

    #rest = obj.add_one()
    #print(rest)


if __name__ == '__main__':
    main()

正在回答 回答被采纳积分+3

1回答

NavCat 2019-05-27 08:59:23

self可以理解为类的实例,静态方法可以不用传递改参数。__init__属于构造方法,在类的实例生成时调用。这些是Python的基础知识哦。

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号