In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
#-*-coding: utf-8-*-# knowledge point: reflection # imports the module in the form of a string, which is reflection # and executes the function in the form of a string # first look at the simple small particle temp = 'mysql' # this string is the name of the module (that is, mysql.py or sqlserver.py The suffix name is not required) func = 'count' # this string is the name of the function (that is, the function count in mysql.py/sqlserver.py) model = _ _ import__ (temp) # import the module in the form of a string # the first method of the function in the execution module print model.count () # the function in the execution module the second method Function = getattr (model Func) # execute the function print Function () as a string below simulate a database connection of small particles # prepare the following two files' mysql.py file to simulate connection to the mysql database Normal connection returns "- *-coding: utf-8-*-def count (): return 1sqlserver.py file simulates connecting to sqlserver database, and normal connection returns"-*-coding: utf-8-*-def count (): return 2 subscription. # Note: mysql.py and sqlserver.py files can be defined by themselves. Here is the statement def db_count (db_name) that is not actually connected to the database: temp = db_name # module name (that is, mysql or sqlserver) func = 'count' # function (that is, function count in mysql.py/sqlserver.py) model = _ _ import__ (temp) Function = getattr (model) Func) # execute the function return Function () as a string # currently simulates the default connection to the sqlserver database status = db_count ('sqlserver') # check the status of the connection sqlserver (normal state: 2) If you cannot connect, switch to mysqlif status! = 2: status = db_count ('mysql') print' has been switched to mysql, connection ID:%d'% statuselse: print 'sqlserver connection is normal, connection ID:%d'% status# test action: # 1, the count return value in sqlserver.py can be modified to 3
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.