Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How the MySQLdb module operates the MySQL database

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Next, let's learn about how the MySQLdb module operates the MySQL database. I believe you will benefit a lot after reading it. The text is not much in the essence. I hope the MySQLdb module how to operate the MySQL database is what you want.

1. There are many connector for python to connect to mysql. We choose MySQLdb.

two。 Let python support MySQLdb module

# pip2.7 install MySQL-python

3. Check to see if MySQLdb exists in modules that can be used by python2.7

# ipythonWARNING: IPython History requires SQLite, your history will not be savedPython 2.7.11 (default, Mar 10 2016, 09:45:30) Type "copyright", "credits" or "license" for more information.IPython 4.1.2-An enhanced Interactive Python.?-> Introduction and overview of IPython's features.%quickref-> Quick reference.help-> Python's own help system.object?-> Details about 'object', use' object??' For extra details.In [1]: help ('modules') Please wait a moment while I gather a list of all available modules...MySQLdb calendar marshal

4. Simple encapsulated module, no problem in testing

Script-name:mysql-conn.py

#! / usr/bin/python27#-*-coding:utf-8-*-# Import MySQL driver native module import MySQLdb class mysqldb (): # Constructor sets the initial value def _ _ init__ (self,host='192.168.17.1',port=4306,user='root' Passwd='zcy'): self.host = host self.port = port self.user = user self.passwd = passwd # the instance has connection and cursor attributes self.conn1 = MySQLdb.connect (host=self.host,port=self.port,user=self.user Passwd=self.passwd) self.cur1 = self.conn1.cursor () # defines the Select query statement def find (self,field,db) in mysql Table): "field-> query field db-> query database table-> query table"self.field = field self.db = db self.table = table self .cur1.execute ('select' + self.field + 'from' + self.db +'. + self.table) return self.cur1.fetchall () # build library def createdb (self Db): self.cur1.execute ('create database if not exists' + db) # create table def createtable (self,db Table): self.conn1.select_db (db) self.cur1.execute (''create table''+ table +''(id int (5) not null primary key auto_increment, name char (10) not null) Phone varchar (12) not null, class char (20)) '') # insert data def insert (self,db,table,*l): self.cur1.execute ('insert into' + db +'. + table + 'values (null,%s,%s,%s)', * l) self.conn1.commit ()

After reading this article on how the MySQLdb module operates the MySQL database, many readers will want to know more about it. If you need more industry information, you can follow our industry information section.

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report