广告
返回顶部
首页 > 资讯 > 数据库 >Python 对mysql数据库的操作
  • 249
分享到

Python 对mysql数据库的操作

操作数据库Python 2023-01-31 01:01:02 249人浏览 泡泡鱼
摘要

Python 对mysql数据库的操作 #!/usr/bin/python # -*- coding: utf-8 -*- import Mysqldb class mysql:     def __init__(self, sql, h



Python 对mysql数据库的操作


#!/usr/bin/python
# -*- coding: utf-8 -*-
import Mysqldb

class mysql:
    def __init__(self, sql, host='127.0.0.1', username='root', passWord='root', dbname='dbname'):
        self.username = username
        self.password = password
        self.dbname = dbname
        self.sql = sql
        self.mysqldb = MySQLdb.connect(host, self.username, self.password, self.dbname, charset="utf8")

    # 查询操作
    def query(self):
        try:
            cursor = self.mysqldb.cursor()
            cursor.execute(self.sql)
            data = cursor.fetchall()
            return data
            
        except Exception as e:
            print e

    # 插入操作
    def insert(self):
        try:
            cursor = self.mysqldb.cursor()
            cursor.execute(self.sql)
            self.mysqldb.commit()
            self.mysqldb.close()
            return 'ok'
        except Exception as e:
            print e

    # 删除操作
    def delete(self):
        try:
            cursor = self.mysqldb.cursor()
            cursor.execute(self.sql)
            self.mysqldb.commit()
            self.mysqldb.close()
        except Exception as e:
            print e

    # 修改操作
    def update(self):
        try:
            cursor = self.mysqldb.cursor()
            cursor.execute(self.sql)
            self.mysqldb.commit()
            self.mysqldb.close()
        except Exception as e:
            print e

if __name__=="__main__":
	pass



#!/usr/bin/Python
# -*- coding: utf-8 -*-

__author__ = 'gaogd'

import  MySQLdb

try:
    conn = MySQLdb.connect(host='localhost', user='root', passwd='root', port=3306)
    cur = conn.cursor()
    cur.execute('create database if not exists python')
    conn.select_db('python')
    cur.execute('create table test(id int,info varchar(20))')

    value = [1, 'hi rollen']
    cur.execute('insert into test values(%s,%s)', value)

    values = []
    for i in range(20):
        values.append((i, 'hi rollen' + str(i)))

    cur.executemany('insert into test values(%s,%s)', values)
    ## 重点:这个  cur.executemany 可以一次性插入多个值  
    cur.execute('update test set info="I am rollen" where id=3')

    conn.commit()
    cur.close()
    conn.close()

except MySQLdb.Error, e:
    print "Mysql Error %d: %s" % (e.args[0], e.args[1])



常用函数:


然后,这个连接对象也提供了对事务操作的支持,标准的方法

commit()提交

rollback()回滚


cursor用来执行命令的方法:

callproc(self,procname,args):用来执行存储过程,接收的参数为存储过程名和参数列表,返回值为受影响的行数

execute(self, query, args):执行单条sql语句,接收的参数为sql语句本身和使用的参数列表,返回值为受影响的行数

executemany(self, query, args):执行单挑sql语句,但是重复执行参数列表里的参数,返回值为受影响的行数

nextset(self):移动到下一个结果集


cursor用来接收返回值的方法:

fetchall(self):接收全部的返回结果行.

fetchmany(self, size=None):接收size条返回结果行.如果size的值大于返回的结果行的数量,则会返回cursor.arraysize条数据.

fetchone(self):返回一条结果行.

scroll(self, value, mode='relative'):移动指针到某一行.如果mode='relative',则表示从当前所在行移动value条,如果 mode='absolute',则表示从结果集的第一行移动value条.



您可能感兴趣的文档:

--结束END--

本文标题: Python 对mysql数据库的操作

本文链接: https://www.lsjlt.com/news/185046.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作