Example #1
0
    def sql_error(self, sql, err_msg=None, err_sign=None):
        print '-' * 50
        print 'sql_error: >>> ', sql
        print err_msg, err_sign
        print '-' * 50
        import time
        from sgLib.core import Gcore
        if Gcore.IsServer:  #只服务器模式才记录错误日志
            now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
            #---------暂都保存多条 note表已删-----------
            #            if Gcore.TEST:
            #                try:
            #                    sqllog = "INSERT INTO temp_sqlerror_logall (MySQL,MySQLError,RecordTime) VALUES('%s','%s','%s')"\
            #                    %(self.escape_string(sql),self.escape_string(str(err_msg)),now)
            #                    con,cursor = self._create()
            #                    cursor.execute(sqllog)  #此处不能用self.execute否则出错会循环调用
            #                    con.commit()
            #                    self._close(con,cursor)
            #                except:
            #                    print 'sql_error: >>>  logall error',sqllog
            #---------暂都保存多条-----------

            try:
                sqllog = "INSERT INTO temp_sqlerror_log (MySQL,MySQLError,RecordTime)  VALUES ('%s','%s','%s')" % (
                    self.escape_string(sql), self.escape_string(
                        str(err_msg)), now)
                sqllog += " ON DUPLICATE KEY UPDATE ApearTimes=ApearTimes+1,RecordTime='%s'" % now
                from sgLib.core import Gcore
                Gcore.sqldelay(sqllog)
            except:
                print 'sql_error: >>>  sql_error delay', sqllog
Example #2
0
 def execute(self, sql, args=None, isdelay=False):
     '''增 删 改
     @note:涉及连接
     @param isdelay: 是否到日志中心统一处理 
     '''
     self._set(sql)
     if isdelay:
         from sgLib.core import Gcore
         Gcore.sqldelay(sql)
     else:
         try:
             con, cursor = self._create()
             result = cursor.execute(sql, args)  #如果是更新直接就是影响行数
             con.commit()  #提交事务,兼容InnoDB
             if sql.lower().startswith('insert'):
                 return cursor.lastrowid  #如果是插入语句,返回lastid
             else:
                 return result
         except Exception, e:  #Exception MySQLdb.Error
             self.sql_error(sql, e, 'execute')
             return False
         finally: