MySQLdb模块如何操作MySQL数据库-创新互联

下面一起来了解下MySQLdb模块如何操作MySQL数据库,相信大家看完肯定会受益匪浅,文字在精不在多,希望MySQLdb模块如何操作MySQL数据库这篇短内容是你想要的。

创新互联公司是一家集成都网站制作、成都网站建设、外贸营销网站建设、网站页面设计、网站优化SEO优化为一体的专业的建站公司,已为成都等多地近百家企业提供网站建设服务。追求良好的浏览体验,以探求精品塑造与理念升华,设计最适合用户的网站页面。 合作只是第一步,服务才是根本,我们始终坚持讲诚信,负责任的原则,为您进行细心、贴心、认真的服务,与众多客户在蓬勃发展的市场环境中,互促共生。

1. python连接mysql的connector有很多,我们选择MySQLdb

2. 让python支持MySQLdb模块

#pip2.7 install MySQL-python

3. 查看python2.7可使用的模块是否存在MySQLdb

# ipython WARNING: IPython History requires SQLite, your history will not be saved Python 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. 简单封装的模块,测试没问题

script-name:mysql-conn.py

#!/usr/bin/python27 #-*-coding:utf-8-*- #导入MySQL驱动原生模块 import MySQLdb   class mysqldb():         #构造器设定初始值 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 #实例一开始就具备了连接和游标属性 self.conn1 = MySQLdb.connect(host=self.host,port=self.port,user=self.user,passwd=self.passwd)   self.cur1 = self.conn1.cursor()                    #定义mysql中的Select查询语句 def find(self,field,db,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() #建库 def createdb(self,db):   self.cur1.execute('create database if not exists ' + db)                 #建表 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) );  ''')                                  #插入数据 def insert(self,db,table,*l):  self.cur1.execute('insert into '+ db + '.' + table +  ' values(null,%s,%s,%s)' , *l) self.conn1.commit()

看完MySQLdb模块如何操作MySQL数据库这篇文章后,很多读者朋友肯定会想要了解更多的相关内容,如需获取更多的行业信息,可以关注我们的行业资讯栏目。

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

网站题目:MySQLdb模块如何操作MySQL数据库-创新互联
标题URL:https://www.cdcxhl.com/article38/dpoesp.html

成都网站建设公司_创新互联,为您提供网站维护品牌网站制作App开发软件开发自适应网站云服务器

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

微信小程序开发