Python使用web.py读取Mysql的数据
2016-04-18 18:07
811 查看
读取mysql 的数据有两种方式
1.MySQLdb模块操作的MYSQL数据库
import MySQLdb;
import urllib2;
import os;
import re;
from urllib import urlopen;
#import urllib2.request;
conn = MySQLdb.connect(host="127.0.0.1",user="root",passwd="root",charset="utf8");
cur = conn.cursor();
cur.execute("USE test");#指定数据库
cur.execute("TRUNCATE TABLE cri");#指定数据库中的数据表
content = urllib2.urlopen(("xx.txt"));#读取在线文件或指定路径的文件
idPattern = r"\d{8}\.\d{2}";
pattern = re.compile(idPattern);
match = pattern.findall(content.read());
sql = "INSERT INTO cri (CRI) VALUES(%s)";
for i in match:
cur.execute(sql, i);
#cur.execute("SELECT * FROM cri");
cur.close();
conn.commit();
conn.close()
2.使用web.py
class getPltfList(object):
def __init__(self):
self.db = web.database(dbn='data_name', db = 'table_name', user = 'root', pw= ' ', host = '127.0.0.1')
self.db.printing = True#是指打印mysql语句
def ___call__(self):
return self.createPltfList()
def createPltfList(self):
self.pltfList = list(self.db.query('select name, ip from EPISODE_INSTANT_INFO order by id DESC'))
if __name__ == '__main__' :
pltfList = getPltfList()()
for pltf in pltfList:
print pltf['name'], pltf['ip']
1.MySQLdb模块操作的MYSQL数据库
import MySQLdb;
import urllib2;
import os;
import re;
from urllib import urlopen;
#import urllib2.request;
conn = MySQLdb.connect(host="127.0.0.1",user="root",passwd="root",charset="utf8");
cur = conn.cursor();
cur.execute("USE test");#指定数据库
cur.execute("TRUNCATE TABLE cri");#指定数据库中的数据表
content = urllib2.urlopen(("xx.txt"));#读取在线文件或指定路径的文件
idPattern = r"\d{8}\.\d{2}";
pattern = re.compile(idPattern);
match = pattern.findall(content.read());
sql = "INSERT INTO cri (CRI) VALUES(%s)";
for i in match:
cur.execute(sql, i);
#cur.execute("SELECT * FROM cri");
cur.close();
conn.commit();
conn.close()
2.使用web.py
class getPltfList(object):
def __init__(self):
self.db = web.database(dbn='data_name', db = 'table_name', user = 'root', pw= ' ', host = '127.0.0.1')
self.db.printing = True#是指打印mysql语句
def ___call__(self):
return self.createPltfList()
def createPltfList(self):
self.pltfList = list(self.db.query('select name, ip from EPISODE_INSTANT_INFO order by id DESC'))
if __name__ == '__main__' :
pltfList = getPltfList()()
for pltf in pltfList:
print pltf['name'], pltf['ip']
相关文章推荐
- MySQL中的integer 数据类型
- MySQL存储过程
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- 使用BAE的基于Web.py的简单博客程序
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- mysql load data 导出、导入 csv
- install and upgrade scrapy
- source命令执行SQL脚本文件
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- MySQL创建用户及权限控制