コード例 #1
0
ファイル: MySQLDemo02.py プロジェクト: smile2015/PythonDemo
def fetchall(sql, args=None):
    mySqlHelper = MySQLHelper()
    # 打开数据库连接
    conn = mySqlHelper.connetMySQL(host, user, pwd, dbname)
    #print  conn
    # 使用cursor()方法获取操作游标
    cursor = mySqlHelper.getCursor()
    #print cursor

    # 使用execute方法执行SQL语句
    #cursor.execute("use " + dbname)
    cursor.execute(sql, args)

    accountList = []

    res = cursor.fetchall()
    #print res
    for row in res:
        #print row[0]
        account = Account()
        account.setId(row[0])
        account.setName(row[1])
        account.setPassword(row[2])
        account.setCreatetime(row[3])
        accountList.append(account)

    for account in accountList:
        print account.toString()

    # 关闭数据库连接
    cursor.close()
    conn.close()
コード例 #2
0
ファイル: MySQLDemo06.py プロジェクト: smile2015/PythonDemo
def fetchall(sql, args=None):
    #下面连个字典都可行
    #conn_dict=dict(host=host,user=user,passwd=pwd,db=dbname,port=3306,charset="utf8")
    conn_dict = dict(host=host, user=user, passwd=pwd, db=dbname)
    mySqlHelper = MySQLHelper()
    # 打开数据库连接
    conn = mySqlHelper.connetMySQLArgsByDict(conn_dict)
    #print  conn
    # 使用cursor()方法获取操作游标
    cursor = mySqlHelper.getCursor()
    #print cursor

    # 使用execute方法执行SQL语句
    #cursor.execute("use " + dbname)
    cursor.execute(sql, args)

    accountList = []

    res = cursor.fetchall()
    #print res
    for row in res:
        #print row[0]
        account = Account()
        account.setId(row[0])
        account.setName(row[1])
        account.setPassword(row[2])
        account.setCreatetime(row[3])
        accountList.append(account)

    for account in accountList:
        print account.toString()

    # 关闭数据库连接
    cursor.close()
    conn.close()
コード例 #3
0
    def queryAccounts(self,sql, args=None):
        '''
        查询账号信息列表
        :param sql: SQL语句
        :param args:  字典/列表类型参数,根据SQL参数化时构造场景而定。SQL语句参数化场景需要传值,其他场景不传,默认使用None
        :return: 返回账号信息列表
        '''
        try:
            rs = self.mySqlHelper.query(sql,args)
            accountList = []
            for row in rs:
                # print row[0]
                account = Account()
                account.setId(row[0])
                account.setName(row[1])
                account.setPassword(row[2])
                account.setCreatetime(row[3])
                accountList.append(account)

            for account in accountList:
                print account.toString()

            return accountList
        except Exception as e:
            print str(e)
            raise "Query account fail."
コード例 #4
0
import sys

reload(sys)
default_encoding = "utf-8"
if (default_encoding != sys.getdefaultencoding()):
    reload(sys)
    sys.setdefaultencoding(default_encoding)

'''
【转载】反射示例
'''

if __name__ == '__main__':
    obj = Account()
    obj.setId(1000)
    obj.setName("Tom")
    obj.setPassword("GDfds")
    obj.setCreatetime("2019-01-19 23:41:54")
    # 获取成员
    ret = getattr(obj, 'toString')  # 获取的是个对象
    r = ret()
    print(r)
    # 检查成员
    ret = hasattr(obj, 'toString')  # 因为有func方法所以返回True
    print(ret)
    # 设置成员
    print(obj.name)  # 设置之前为:abc
    ret = setattr(obj, 'name', "gddd")
    print(obj.name)  # 设置之后为:19
    # 删除成员