__author__ = 'Administrator' 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
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()
if __name__ == '__main__': sql_insert = "INSERT INTO account (name,password) VALUES ('test42','test');" sql_insert1 = "INSERT INTO account (name,password) VALUES (%(name)s,%(password)s);" sql_update = "UPDATE account SET password= '******' WHERE name = 'test42';" sql_update1 = "UPDATE account SET password= %(password)s WHERE name = %(name)s;" sql_delete = "DELETE FROM account WHERE name = 'test';" # CAST(createtime AS CHAR) AS createtime --在MySQL端处理日期类型数据,将datetime类型转字符串后再返回 select_sql = "SELECT id, name, password,CAST(createtime AS CHAR) AS createtime FROM account;" select_sql1 = "SELECT id, name, password,CAST(createtime AS CHAR) AS createtime FROM account WHERE password=%(password)s;" print "====dbname: " + dbname print "==============sql_update=======================: \n" update(sql_update) print "==============sql_update1=======字典传参================: \n" account = Account() account.setName("test41") account.setPassword("testmodify") account_dict = {} account_dict["name"] = account.getName() account_dict["password"] = account.getPassword() print account_dict update(sql_update1, account_dict) print "==============fetchall select_sql1======字典传参=================: \n" fetchall(select_sql1, {'password': '******'})
add(sql_insert) print "==============sql_insert1=======字典传参================: \n" import random # 生成0到100的随机数 rannum = random.randint(0, 100) import time # 生成当前时间戳 timenum = time.time() # 将两个数据变成字符串 strrannum = str(rannum) print strrannum strtimenum = str(timenum) print strtimenum rantime = "".join((strrannum, strtimenum)) print rantime account=Account() account.setName("rtest"+rantime) account.setPassword("test") account_dict={} account_dict["name"]=account.getName() account_dict["password"]=account.getPassword() print account_dict add(sql_insert1,account_dict) print "==============fetchall select_sql1======字典传参=================: \n" fetchall(select_sql1, {'password': '******'})
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()
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."
sql_update1 = "UPDATE account SET password= %(password)s WHERE name = %(name)s;" sql_delete = "DELETE FROM account WHERE name = 'test1231';" sql_delete1 = "DELETE FROM account WHERE name = %(name)s;" sql_delete2 = "DELETE FROM account WHERE name = %s;" #CAST(createtime AS CHAR) AS createtime --在MySQL端处理日期类型数据,将datetime类型转字符串后再返回 select_sql = "SELECT id, name, password,CAST(createtime AS CHAR) AS createtime FROM account;" select_sql1 = "SELECT id, name, password,CAST(createtime AS CHAR) AS createtime FROM account WHERE password=%(password)s;" print "====dbname: " + dbname print "==============sql_delete=======================: \n" delete(sql_delete) print "==============sql_delete1=======字典传参================: \n" account = Account() account.setName("test121") account.setPassword("testmodify") account_dict = {} account_dict["name"] = account.getName() print account_dict delete(sql_delete1, account_dict) print "==============fetchall select_sql1=======================: \n" fetchall(select_sql) print "==============sql_delete1=======列表传参================: \n" account = Account()