Manipulating MySQL database with Python (code collation)

Source: Internet
Author: User

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/8A/C7/wKiom1g7x0ix4njuAAFB58eby5Y106.png-wh_500x0-wm_3 -wmp_4-s_2227896382.png "title=" qq picture 20161128135645.png "alt=" Wkiom1g7x0ix4njuaafb58eby5y106.png-wh_50 "/>

index.py here just assume a mock 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 re-login ' else:print ' login successful ' if __name__ = ' __ma In__ ": Main ()

user.py

# -*- coding: utf-8 -*-"" "created on sun nov 27 19:48:03  2016 for database table processing, PY file name and table name one by one correspond so, here the user.py file is the database table for user processing @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, processing the lowest level of the information, such as adding and deleting the function @author: toby "" "Import mysqldbclass mysqlhelper (object):         def __init__ (self):         hosts,users,password,dbname =  ' 127.0.0.1 ', ' root ', ' 1qaz#edc ', ' test_db '          self.conn = mysqldb.connect (Host=hosts,user=users,passwd=password,db=dbname)         self.cur = self.conn.cursor ( MySQLdb.cursors.DictCursor)         def get_dict_data (Self,sql, params):         self.cur.execute (sql,params)          data = self.cur.fetchall ()   #fetchall () Get all data          self.cur.close ()   &Nbsp;     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 data         self.cur.close ()         self.conn.close ()          return data

This article is from the "Fa&it-Q Group: 223843163" blog, please be sure to keep this source http://freshair.blog.51cto.com/8272891/1877284

Manipulating MySQL database with Python (code collation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.