zoukankan      html  css  js  c++  java
  • Python基础之Mysql

    1.MySQLdb 是用于Python链接Mysql数据库的接口,它实现了 Python 数据库 API 规范 V2.0,基于 MySQL C API 上建立的。

    2.Python下MySQLdb安装。

      (1)先下载MySQLdb安装包,解压。

      (2)cmd以管理员身份运行,进行MySQLdb目录,将MySQLdb安装至Python下:python setup.py install

      PyMySqlUtil.py

    #coding=utf-8
    
    import pymysql
    
    class PyMySqlUtil:
        
        def __init__(self,host,port,userName,userPwd,database):
            self._host=host
            self._port=port
            self._userName=userName
            self._userPwd=userPwd
            self._database=database
            
        #进行数据查询操作,返回元组
        def executeQuery(self,sql):
            conn = pymysql.connect(host=self._host,port= self._port,user = self._userName,passwd=self._userPwd,db=self._database)
            cursor = conn.cursor()
            cursor.execute(sql)
            result=cursor.fetchall()
            cursor.close()
            conn.close()
            return result
            
        #进行数据增、删、改操作
        def executeNoneQuery(self,sql):
            conn = pymysql.connect(host=self._host,port= self._port,user = self._userName,passwd=self._userPwd,db=self._database)
            cursor = conn.cursor()
            try:
                cursor.execute(sql)
                conn.commit()
            except:
                db.rollback()
            cursor.close()
            conn.close()
            
        

    调用代码:

    Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
    Type "copyright", "credits" or "license()" for more information.
    >>> from PyMySqlUtil  import *
    >>> db=PyMySqlUtil("localhost",3306,"root","root","dgcms")
    >>> result=db.executeQuery("select * from blog")
    >>> print(result)
    ((7, 'go', 'VB.NET'), (4, '  C#', 'RWRRWWW'), (6, 'object c', 'object c'), (5, 'python', 'haaaa'))
    >>> 
  • 相关阅读:
    c#个人记录常用方法(更新中)
    Newtonsoft.Json.dll解析json的dll文件使用
    组织http请求
    ado.net中的几个对象
    jquery-easyui使用
    aspx与mvc页面验证码
    aspx页面状态管理(查询字符串Request与Application)
    aspx页面状态管理Cookie和ViewState
    在网页中插入qq连接
    ASP.NET中上传图片检测其是否为真实的图片 防范病毒上传至服务器
  • 原文地址:https://www.cnblogs.com/joyet-john/p/7087452.html
Copyright © 2011-2022 走看看