使用Python对MySQL进行相关操作

news/2024/7/7 10:45:00

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

# -*- coding: utf-8 -*-
import MySQLdb
import uuid

DBKWARGS = {'db': 'yy2.0', 'user': 'root', 'passwd': 'root',
            'host': 'localhost', 'use_unicode': True, 'charset': 'utf8'}
getRC = lambda cur: cur.rowcount if hasattr(cur, 'rowcount') else -1


class handleDataBase:
    def __init__(self, kwargs):
        self.conn = MySQLdb.connect(**kwargs)
        self.cur = self.conn.cursor()

    # 插入操作
    def insert_one(self, sql, value):
        res = self.cur.execute(sql, value)
        # 插入成功,res 返回值为1
        if 1 != res:
            print 'failed'
        else:
            print 'success'

    # 插入多条记录
    def insert_many(self, sql, values):
        res = self.cur.executemany(sql, values)
        # 插入成功,res 返回值为1
        if 1 != res:
            print 'failed'
        else:
            print 'success'

    # 更新操作
    def update(self, sql, params):
        self.cur.execute(sql, params)
        return getRC(self.cur)

    # 删除操作
    def delete(self, sql, params):
        self.cur.execute(sql, params)
        return getRC(self.cur)

    # 只获取一条记录,返回的是一个元组
    def fetch_one(self, sql):
        count = self.cur.execute(sql)
        print count
        result = self.cur.fetchone()
        return result

    # 获取多条数据;返回的是二维元组;
    def fetch_all(self, sql):
        count = self.cur.execute(sql)
        print count
        results = self.cur.fetchall()

        '''
        print results
        for r in results:
            print r
        '''
        return results

    # 提交的完成操作
    def finish(self):
        self.conn.commit()
        self.conn.close()

# 实战
if __name__ == "__main__":
    # 接下来就访问数据库了
    handle_db = handleDataBase(DBKWARGS)
    select_sql = 'select id from las_question'
    insert_sql = 'INSERT INTO las_question_use_count (id,paperCount,questionId,resolveCount,wrongCount) VALUES (%s,0,%s,0,0)'
    results = handle_db.fetch_all(select_sql)
    values=[]
    print values
    for r in results:
        target = r[0]
        if target:
            use_count_id=uuid.uuid1()
            params = (use_count_id, target)
            values.append(params)
    handle_db.insert_many(insert_sql,values)
    handle_db.finish()

 

转载于:https://my.oschina.net/liuyuantao/blog/758448


http://www.niftyadmin.cn/n/4428419.html

相关文章

ddd的战术篇: domain event(事件)

承接上篇文章谈到的aggregate的设计策略。aggregate是用来保证数据(*注: 之前的文章都是用数据完整性这个说法,其实是想表达data consistency这个意思,查了一下翻译,感觉还是数据一致性比较贴切)一致性的一个单位&…

jvm栈大小设置

1、栈内存大小设置栈内存为线程私有的空间,每个线程都会创建私有的栈内存。栈空间内存设置过大,创建线程数量较多时会出现栈内存溢出StackOverflowError。同时,栈内存也决定方法调用的深度,栈内存过小则会导致方法调用的深度较小&…

unable to connect to :5555

有可能批处理文件用的adb和eclipse的adb不兼容。把你的批处理文件用的adb换成eclipse的adb就可以了: 运行结果: 转载于:https://www.cnblogs.com/johnsonwei/p/5965643.html

固执的程序员学习函数式编程的收获 之 一

最近因为写node js,开始有机会接触js的函数式写法。关于函数式语言,其实久闻其名,但只是大概了解过一些概念罢了。刚开始听到这个概念觉得不会就是面向过程编程的改良版吧?(自己还是太无知了…) 由于自己的编程语言主…

固执的程序员学习函数式编程的收获 之 二 说说monad

之前说了函数式编程的收获。比如说函数可以当作变量,然后尽量避免写副作用的程序。 之后可以说遇到了一个超级难理解的东西–monad。 一切要从和小田君的对话说起 当我在写java时,大概是下面的一段代码 List.map( item -> item.getName()); List.…

MAYA影视动漫高级模型制作全解析出_完整版PDF电子书下载 带索引书签目录高清版...

MAYA影视动漫高级模型制作全解析_页数384_出版日期2016.04_完整版PDF电子书下载 带索引书签目录高清版_13936277 下载链接 http://pan.baidu.com/s/1skA4FZf 【作 者】CGWANG动漫教育著【形态项】 384【出版项】 北京:人民邮电出版社 , 2016.04【ISBN号】7-115-41…

自己写deque

//deque /* what is a deque? In Chinese, its called "双端队列". Its different from a queue. Its elements can be added to or removed from either the front(head) or back(tail) ,called a head-tail linked list.输入限制deque An input-restricted deque …

ddd的战术篇: CQRS

之前的文章介绍了ddd在战术层面的要素,包括entity,value object,aggregate和一些设计模式如repository。在ddd中,repository几乎成为了等同于entity一样的基本要素。 关于aggregate与repository的回顾 aggregate是entity和value…