当前位置: 首页>>代码示例>>Python>>正文


Python MySQL_Interface.insert_asQuery方法代码示例

本文整理汇总了Python中DB_Interface.MySQL_Interface.insert_asQuery方法的典型用法代码示例。如果您正苦于以下问题:Python MySQL_Interface.insert_asQuery方法的具体用法?Python MySQL_Interface.insert_asQuery怎么用?Python MySQL_Interface.insert_asQuery使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DB_Interface.MySQL_Interface的用法示例。


在下文中一共展示了MySQL_Interface.insert_asQuery方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: post

# 需要导入模块: from DB_Interface import MySQL_Interface [as 别名]
# 或者: from DB_Interface.MySQL_Interface import insert_asQuery [as 别名]
    def post(self):

        # 从客户端获取信息
        try:
            user_history=self.get_argument('user_history')
            latest_time=self.get_argument('latest_time')
            latest_timestamp=self.get_argument('latest_timestamp')
            container_id=self.get_argument('container_id')
            isDivided=self.get_argument('isDivided')
            user_history=eval(user_history)
            if isDivided==1 or isDivided=='1' :
                block_num=self.get_argument('block_num')
                current_block=self.get_argument('current_block')
            self.write('success to return user history')
            self.finish()
            print('Success: to get data from web')
        except Exception as e:
            self.write('fail to return user history')
            self.finish()
            print('Error:server-HistoryReturn:'
                  'Unable to get value from http package,Reason:')
            print(e)
            return


        # 连接
        try:
            dbi=MySQL_Interface()
        except:
            print('Error:server-HistoryReturn:'
                  'Unable to connect to MySQL')

        # 从MYSQL获取该用户相关信息
        try:
            query='select * from user_info_table where container_id=\'{cid}\''\
                .format(cid=container_id)
            user_info=dbi.select_asQuery(query)[0]
            col_name=dbi.get_col_name('user_info_table')
        except Exception as e:
            print('Error:server-HistoryReturn:'
                  'No such user in MySQL.user_info_table,Reason:')
            print(e)

        # 将数据存入Mongodb以后将相关信息存入mysql,并将isGettingBlog字段设为空
        try:
            blog_len=user_history.__len__()
            wanted_blog_len=user_info[col_name.index('blog_num')]
            blog_accuracy=blog_len/wanted_blog_len
            time_stick=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
            if not user_info[col_name.index('update_time')]:
                save_data_inMongo(user_history)
                # query='update user_info_table set ' \
                #       'update_time=\'{up_time}\',' \
                #       'latest_blog=\'{latest_blog}\',' \
                #       'isGettingBlog=null ' \
                #       'where container_id=\'{cid}\';'\
                #     .format(up_time=time_stick,latest_blog=latest_time,cid=container_id)
                query='update user_info_table set ' \
                      'update_time=\'{up_time}\',' \
                      'latest_blog=\'{latest_blog}\'' \
                      'where container_id=\'{cid}\';' \
                    .format(up_time=time_stick,latest_blog=latest_time,cid=container_id)
                dbi.update_asQuery(query)
            else:
                query='update user_info_table set isGettingBlog=null where container_id=\'{cid}\''\
                    .format(cid=container_id)
                dbi.update_asQuery(query)

            query='insert into accuracy_table values ({acc},\'{t_s}\') ;'\
                .format(acc=blog_accuracy,t_s=time_stick)
            dbi.insert_asQuery(query)

            print('Success: insert user into MongoDB, the num of data is {len}'
                  .format(len=blog_len))
        except Exception as e:
            print('Error:server-HistoryReturn:'
                  'Unable to update data in MySQL.user_info_tabe,Reason:')
            print(e)
开发者ID:liangkai,项目名称:Distributed_Microblog_Spider,代码行数:80,代码来源:server.py

示例2: run

# 需要导入模块: from DB_Interface import MySQL_Interface [as 别名]
# 或者: from DB_Interface.MySQL_Interface import insert_asQuery [as 别名]

#.........这里部分代码省略.........
                    # --------------------------------
                except Exception as e:
                    print('Error:server-HistoryReturn:'
                        'Unable to get data from MongoDB, assemble factory,Reason:')
                    print(e)

                # 长度大于预期,说明有重复信息,需要去重
                if  id_list.__len__() > num :
                    unique_data_list = []
                    check_dict = {}
                    for i in range(id_list.__len__()) :
                        try:
                            # 这里使用字典去重,(算是hash吧)
                            check_dict[str(id_list[i])]
                            continue
                        except:
                            check_dict[str(id_list[i])] = True
                            unique_data_list.append(data_list[i])
                            # print('data_list.len :{len}'.format(len=data_list.__len__()))
                            # print('id_list.len :{len}'.format(len=id_list.__len__()))
                            # print(i)
                    data_list = unique_data_list

                # 将碎片拼接
                try:
                    data_final = []
                    for i in data_list:
                        data_final = data_final+i
                    # todo fro debug-------------
                    print('task {cid} debug->数据拼接完毕,len {len}'.format(len=data_final.__len__(),cid=container_id))
                    # --------------------------------
                except Exception as e:
                    print('Error:server-HistoryReport:'
                          'Unable to contact the pieces of information,Reason:')
                    print(e)

                # 将本次信息录入accuracy_table 用以进一步分析
                blog_len = data_final.__len__()
                wanted_blog_len = user_info[col_name.index('blog_num')]
                blog_accuracy = blog_len/wanted_blog_len
                time_stick = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                query = 'insert into accuracy_table values ({acc},\'{t_s}\',{num}) ;' \
                    .format(acc=blog_accuracy, t_s=time_stick, num=wanted_blog_len)
                dbi.insert_asQuery(query)

                # 将数据录入Mongodb 更改Mysql,删除assemble中相关内容
                try:
                    if not user_info[col_name.index('update_time')]:
                        # 将数据存入 Mongodb 的formal collection
                        save_data_seperately(data_final)
                        print('task {cid} Success: Data has saved in Mongodb, size is {size}'
                              .format(size=sys.getsizeof(data_final),cid=container_id))

                        # # 将关键信息录入Mydql
                        query = 'update user_info_table set ' \
                              'update_time=\'{up_time}\',' \
                              'latest_blog=\'{latest_blog}\',' \
                              'isGettingBlog=null ' \
                              'where container_id=\'{cid}\';'\
                            .format(up_time=time_stick,latest_blog=latest_time,cid=container_id)
                        # query='update user_info_table set ' \
                        #       'update_time=\'{up_time}\',' \
                        #       'latest_blog=\'{latest_blog}\'' \
                        #       'where container_id=\'{cid}\';' \
                        #     .format(up_time=time_stick,latest_blog=latest_time,cid=container_id)
                        #TODO 这里为了方便统计,去掉了抹除isGetting这一项,但是正式运行的时候是要加上的
                        dbi.update_asQuery(query)
                        print('task {cid} Success: insert user into MongoDB, the num of data is {len}'
                              .format(len=blog_len,cid=container_id))
                    else:
                        query='update user_info_table set isGettingBlog=null where container_id=\'{cid}\'' \
                            .format(cid=container_id)
                        dbi.update_asQuery(query)

                except Exception as e:
                    print('task {cid} Error:server->HistoryReport:'
                          'Reason:'.format(cid=container_id))
                    print(e)
            else:
                # 如果所有子包不全,则抹掉isGettingBlog,将装配车间中数据删除
                print('task {cid} :Error: the package is not complete ,{a} of {b}'
                      .format(a=id_list.__len__(),b=num,cid=container_id))
                query='update user_info_table set isGettingBlog=null where container_id=\'{cid}\'' \
                    .format(cid=container_id)
                dbi.update_asQuery(query)

            # 将数据从assemble factory 去掉
            assemble_table.remove({'container_id':container_id})
            print('task {cid} Success: Data has been removed from assemble factory'
                    .format(cid=container_id))

            # 将cache_history中的相应行删掉,表示已经处理完该事物了
            query='delete from cache_history where container_id=\'{cid}\'' \
                .format(cid=container_id)
            dbi.update_asQuery(query)

            end_time = time.time()
            deal_time = end_time - start_time
            print('task {cid} :Success : the user {cid} is completed, length is {len}, use {t} seconds'
                  .format(cid = container_id, len = data_final.__len__(), t = deal_time))
开发者ID:multiangle,项目名称:Distributed_Microblog_Spider,代码行数:104,代码来源:server_database.py


注:本文中的DB_Interface.MySQL_Interface.insert_asQuery方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。