博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python操作mysql总结
阅读量:6717 次
发布时间:2019-06-25

本文共 3271 字,大约阅读时间需要 10 分钟。

Windows系统,python环境搭建。

    下载并安装python2.7.11

    下载并安装python的mysql包:

       

       

    注意:

       如果你使用的是python的64为版本,需要下载对应的64为版本的mysql包。

   

  如果你使用的是python的32为版本,需要下载对应的32为版本的mysql包。  

      

   或

   

否则,在执行导入语句

import MySQLdb

出现如下错误

import _mysqlImportError DLL load failed: %1 不是有效的 Win32 应用程序

安装mysql-python包时,若出现错误

提示“Python 2.7 is required, which was not found in the registry”

请将如下代码保存为register.py,

## script to register Python 2.0 or later for use with win32all# and other extensions that require Python registry settings## written by Joakim Loew for Secret Labs AB / PythonWare## source:# http://www.pythonware.com/products/works/articles/regpy20.htm## modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html import sys from _winreg import * # tweak as necessaryversion = sys.version[:3]installpath = sys.prefix regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)installkey = "InstallPath"pythonkey = "PythonPath"pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (    installpath, installpath, installpath) def RegisterPy():    try:        reg = OpenKey(HKEY_CURRENT_USER, regpath)    except EnvironmentError as e:        try:            reg = CreateKey(HKEY_CURRENT_USER, regpath)            SetValue(reg, installkey, REG_SZ, installpath)            SetValue(reg, pythonkey, REG_SZ, pythonpath)            CloseKey(reg)        except:            print "*** Unable to register!"            return        print "--- Python", version, "is now registered!"        return    if (QueryValue(reg, installkey) == installpath and        QueryValue(reg, pythonkey) == pythonpath):        CloseKey(reg)        print "=== Python", version, "is already registered!"        return    CloseKey(reg)    print "*** Unable to register!"    print "*** You probably have another Python installation!" if __name__ == "__main__":    RegisterPy()

  

执行完毕,显示"python 2.7 is already registered",

表示成功,再次安装mysql-python即可。

 

安装完毕以后,让我们愉快的和mysql-python玩耍吧。

建一张测试用户表

 

-- ------------------------------ Table structure for `t_user`-- ----------------------------USE pydb;DROP TABLE IF EXISTS `t_user`;CREATE TABLE `t_user` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `name` varchar(255) NOT NULL,  `age` int(11) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ------------------------------ Records of t_user-- ----------------------------

 

 

基本的sql操作 

import MySQLdb   conn = MySQLdb.Connect(                       host="127.0.0.1",                       port = 3306,                       user = "user",                       passwd="123456",                       db="pydb",                       charset='utf8')print conncursor = conn.cursor()print cursortry:    sql = "insert into t_user(name,age) values('wyf001', 28)"    cursor.execute(sql)    conn.commit()except Exception as e:    conn.rollback()try:    sql = "update t_user set age=29 where name = 'wyf001'"    cursor.execute(sql)    conn.commit()except Exception as e:    conn.rollback()sql = "select id,name,age from t_user"cursor.execute(sql)rows = cursor.fetchall()for row in rows:    print row    try:    sql = "delete from t_user where name = 'wyf001'"    cursor.execute(sql)    conn.commit()except Exception as e:    conn.rollback()

执行结果:

参考:

转载于:https://www.cnblogs.com/voipman/p/5141516.html

你可能感兴趣的文章
[工具配置]使用requirejs模块化开发多页面一个入口js的使用方式
查看>>
Jenkins具体安装与构建部署使用教程
查看>>
【ES】学习9-聚合2
查看>>
Mindjet MindManager 思维导图软件-使用思维导图跟踪调用流程,绘制软件框架
查看>>
SQLServer判断指定列的默认值是否存在,并修改默认值
查看>>
贝塞尔曲线与CSS3动画、SVG和canvas的应用
查看>>
将NSTimer加入至RunLoop中的两种方法差别
查看>>
[ajax 学习笔记] ajax初试
查看>>
css中合理的使用nth-child实现布局
查看>>
每天一个JavaScript实例-操作元素定位元素
查看>>
架构-到底什么时候该使用MQ【转】
查看>>
split-brain 脑裂问题(Keepalived)
查看>>
清空,再来
查看>>
7.JAVA编程思想笔记隐藏实施过程
查看>>
wps文档忘记保存关闭了怎么恢复
查看>>
LeetCode(29)-Plus One
查看>>
python解析XML笔记(etree)
查看>>
CentOS7 以下安装Mysql MMM
查看>>
windows系统里Cygwin中如何正确安装wget(图文详解)
查看>>
让你快速了解并掌握如何进行iOS开发技能
查看>>