zoukankan      html  css  js  c++  java
  • MySQLdb模块操作

    Linux

    • 安装mysql: apt-get install mysql-server
    • 安装python-mysql模块:apt-get install python-mysqldb

    Windows

    • 下载安装mysql
    • python操作mysql模块:MySQL-python-1.2.3.win32-py2.7.exe 或 MySQL-python-1.2.3.win-amd64-py2.7.exe
    • mysql图形界面:Navicat_for_MySQL

    安装完成后,导入MySQLdb测试是否安装成功




    #!/usr/bin/env python #coding:utf-8 import MySQLdb ''' conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db') cur = conn.cursor() reCount = cur.execute('insert into UserInfo(Name,Address) values(%s,%s)',('alex','usa')) conn.commit() cur.close() conn.close() print reCount ''' ''' conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db') cur = conn.cursor() reCount = cur.execute('delete from UserInfo') conn.commit() cur.close() conn.close() print reCount ''' ''' conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db') cur = conn.cursor() li =[ ('alex','usa'), ('sb','usa'), ] reCount = cur.executemany('insert into UserInfo(Name,Address) values(%s,%s)',li) conn.commit() cur.close() conn.close() print reCount ''' ''' conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db') cur = conn.cursor() reCount = cur.execute('update UserInfo set Name = %s',('alin',)) conn.commit() cur.close() conn.close() print reCount ''' ''' #fetchone/fetchmany(num) conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db') cur = conn.cursor() reCount = cur.execute('select * from UserInfo') print cur.fetchone() print cur.fetchone() cur.scroll(-1,mode='relative') print cur.fetchone() print cur.fetchone() cur.scroll(0,mode='absolute') print cur.fetchone() print cur.fetchone() cur.close() conn.close() print reCount ''' #fetchall conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db') #cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor) #以字典形式,显示结果 cur = conn.cursor() reCount = cur.execute('select Name,Address from UserInfo') nRet = cur.fetchall() cur.close() conn.close() print reCount print nRet for i in nRet: print i[0],i[1]
  • 相关阅读:
    Windows Server 2008 R2 服务器系统安装及配置全过程图文详解
    c#多线程
    DevExpress之TreeList控件用作导航使用说明
    Windows操作系统下搭建Git服务器和客户端。
    D^3CTF 2021 8-bit-pub
    shiro-1.2.4反序列化分析踩坑
    *CTF2021 oh-my-bet
    无线网络密码破解初体验
    瑞昱rtl819x-SDK-v3.4.14b的watchdog分析
    RC4加密算法
  • 原文地址:https://www.cnblogs.com/fengjian2016/p/5250721.html
Copyright © 2011-2022 走看看