千家信息网

MySQLdb模块如何操作MySQL数据库

发表于:2025-11-11 作者:千家信息网编辑
千家信息网最后更新 2025年11月11日,下面一起来了解下MySQLdb模块如何操作MySQL数据库,相信大家看完肯定会受益匪浅,文字在精不在多,希望MySQLdb模块如何操作MySQL数据库这篇短内容是你想要的。1. python连接mys
千家信息网最后更新 2025年11月11日MySQLdb模块如何操作MySQL数据库

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

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

2. 让python支持MySQLdb模块

#pip2.7 install MySQL-python

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

# 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. 简单封装的模块,测试没问题

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数据库这篇文章后,很多读者朋友肯定会想要了解更多的相关内容,如需获取更多的行业信息,可以关注我们的行业资讯栏目。

模块 数据 数据库 内容 更多 行业 肯定 受益匪浅 信息 实例 属性 文字 朋友 栏目 游标 篇文章 语句 读者 资讯 问题 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 计算机网络技术主要学什么的 计算机网络安全隐患主要内容 浦东新区自动网络技术转让产品 excel统计数据库 开源软件开发框架 国家网络安全实行什么样的 上网时应当如何注意网络安全 702研究所软件开发工资 国家网络安全中心 格力软件开发面试经验 相城区智能化软件开发备案 互联网加科技图片大全 ps4版2k21无法连接服务器 云服务器平台选择申请注册 供电所护网行动网络安全 网络安全法中的一般刑事责任 东北凌云网络技术有限公司 北京快鱼网络技术 江苏浪潮服务器续保一年多少钱 软件开发人员的管理方法 pg数据库网络要求 数据库时间长了卡顿 软件开发过程中的各个模型 ping服务器 数据库有什么软件开发 广东三农互联网科技有限公司 简单的财务软件开发合同 有果网络技术有限公司怎么样 长治网络安全宣传周 美的烤箱服务器异常
0