博客
关于我
Python连接MySQL数据库
阅读量:457 次
发布时间:2019-03-06

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

连接MySQL数据库并执行查询操作

在代码中首先需要导入MySQLdb模块

print '连接数据库'

如果host处如果填写'localhost'报错的话,请尝试使用'127.0.0.1'

db=MySQLdb.connect(host='127.0.0.1',user='root',passwd='michael',db='test')

获取数据库游标

cur=db.cursor()

执行SQL语句

r=cur.execute('select * from people')

获取查询结果

r=cur.fetchall()

输出查询结果

print '输出数据库查询结果:'

print r

关闭游标

cur.close()

关闭数据库连接

db.close()

raw_input与input的区别

raw_input获取用户输入之后的返回结果是一个字符串类型数据

input获取用户输入之后的返回值是一个数值型的数据

等价于eval(raw_input(提示信息))

转载地址:http://lsubz.baihongyu.com/

你可能感兴趣的文章
npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
查看>>
npm—小记
查看>>
NPM使用前设置和升级
查看>>
npm入门,这篇就够了
查看>>
npm切换到淘宝源
查看>>
npm前端包管理工具简介---npm工作笔记001
查看>>
npm升级以及使用淘宝npm镜像
查看>>
npm发布自己的组件UI包(详细步骤,图文并茂)
查看>>
npm和yarn清理缓存命令
查看>>
npm和yarn的使用对比
查看>>
npm学习(十一)之package-lock.json
查看>>
npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
查看>>
npm报错Failed at the node-sass@4.14.1 postinstall script
查看>>
npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
查看>>
npm的安装和更新---npm工作笔记002
查看>>
npm的常用配置项---npm工作笔记004
查看>>
npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
查看>>
npm编译报错You may need an additional loader to handle the result of these loaders
查看>>
npm设置淘宝镜像、升级等
查看>>
npm配置安装最新淘宝镜像,旧镜像会errror
查看>>