Get the App
SLTechnology News&Howtos  ›  Database  › 

Use python to operate mysql database (code classification)

Shulou Source: shulou.com Published: 2022-06-01 06:09:59 10月02日 Update

Index.py, this is just assuming a simulated landing.

#-*-coding: utf-8-*-"Created on Sun Nov 27 18:54:29 2016 this is the main program file @ author: toby" from model.user import Userdef main (): username = "tantianran1" user = User () result = user.Check_Username (username) if not result: print 'user does not exist Please log in 'else: print' login succeeded'if _ _ name__ = = "_ _ main__": main ()

User.py

#-*-coding: utf-8-*-"" Created on Sun Nov 27 19:48:03 2016 processing of database tables, py file name and table name correspond one to one, so The user.py file here is the processing of database table user @ author: toby "" import syssys.path.append ("/ home/toby/workspace/date20161128") from utility.sql_helper import MysqlHelperclass User (object): def _ _ init__ (self): self.__helper = MysqlHelper () def Get_data_by_id (self,ids): sql = "select * from user where id=%s" params = (ids ) return self.__helper.Get_One_Data (sql,params) def Check_Username (self,name): sql = "select * from user where name=%s" params = (name,) return self.__helper.Get_One_Data (sql,params)'a = User () print a.Check_Username ('tantianran')''

Sql_helper.py

#-*-coding: utf-8-*-"Created on Sun Nov 27 18:57:44 2016 data processing layer, the lowest layer of data processing For example, the function @ author: toby "import MySQLdbclass MysqlHelper (object): def _ _ init__ (self): hosts,users,password,dbname = '127.0.0.1." Params): self.cur.execute (sql,params) data = self.cur.fetchall () # fetchall () get all data self.cur.close () self.conn.close () return data def Get_One_Data (self,sql,params): self.cur.execute (sql) Params) data = self.cur.fetchone () # fetchone () is to get a piece of data self.cur.close () self.conn.close () return data

Tags: Data processing file utf-8 database login success one-to-one correspondence main program function just data processing file name bottom layer user this is login code classification. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn Redmi MySQL Docker