python操作mysql
来源:互联网 发布:qq三国79js单刷孟获 编辑:程序博客网 时间:2024/06/08 02:06
1.python 2.7.2(安装版本)
2.MySQL-python-1.2.3.win32-py2.7 (mysqldb模块)
#连接数据库代码
#coding=utf-8
import MySQLdb
import sys
import time
host ='127.0.0.1'
user ='root'
passwd ='123456'
port = 3306
db_list=[]
tb_list=[]
dbcon=MySQLdb.connect(host,user,passwd,port=3306)
cur=dbcon.cursor()
#列出所有数据库
def check_db():
cur.execute('show databases')
for db in cur.fetchall():
db_list.append(db[0])
return db_list
#获取当前数据库里所有的表的列表
db_list = check_db()
print db_list
#使用test数据库,查询test数据库下面的所有表
cur.execute("use test")
cur.execute("select database()")
print "当前数据库 :%s" %cur.fetchall()[0]
all_table = cur.execute("show tables")
for tb in cur.fetchall():
tb_list.append(tb[0])
print tb_list
#查询数据表userinfo
userList=[]
cur.execute("use test")
cur.execute("select * from userinfo;")
for shuju in cur.fetchall():
userList.append(shuju)
print userList
2.MySQL-python-1.2.3.win32-py2.7 (mysqldb模块)
#连接数据库代码
#coding=utf-8
import MySQLdb
import sys
import time
host ='127.0.0.1'
user ='root'
passwd ='123456'
port = 3306
db_list=[]
tb_list=[]
dbcon=MySQLdb.connect(host,user,passwd,port=3306)
cur=dbcon.cursor()
#列出所有数据库
def check_db():
cur.execute('show databases')
for db in cur.fetchall():
db_list.append(db[0])
return db_list
#获取当前数据库里所有的表的列表
db_list = check_db()
print db_list
#使用test数据库,查询test数据库下面的所有表
cur.execute("use test")
cur.execute("select database()")
print "当前数据库 :%s" %cur.fetchall()[0]
all_table = cur.execute("show tables")
for tb in cur.fetchall():
tb_list.append(tb[0])
print tb_list
#查询数据表userinfo
userList=[]
cur.execute("use test")
cur.execute("select * from userinfo;")
for shuju in cur.fetchall():
userList.append(shuju)
print userList
0 0
- [python]Python操作MySQL
- python操作mysql
- Python操作MySQL
- Python操作Mysql数据库
- python操作MySQL
- python 操作mysql
- python操作mysql
- python操作mysql例子
- python操作mysql
- Python操作Mysql
- Python操作Mysql
- Python操作Mysql
- python操作MySQL数据库
- python操作MySQL数据库
- 用Python操作Mysql
- python操作mysql
- python操作MySQL数据库
- python操作MySQL数据库
- Android :Some file crunching failed, see logs for details解决办法
- 机器学习的实现框架-笔记
- go语言方法Value Receiver, Pointer Receiver
- 自动化读取文件真的好吗?
- 函数返回类的引用
- python操作mysql
- centos环境配置信息
- 基于二叉排序树的基本操作
- java中文汉字按英文字母表排序
- DTW的基本原理及其C语言实现
- STL vector的内部实现原理及基本用法
- 欢迎使用CSDN-markdown编辑器
- 微信小程序开发之弹出菜单
- mongoDB