狐狸情 发表于 2015-12-15 12:54:53

python连接几种数据库方法

1、连接oracle数据库:



[*]#encoding:utf-8

[*]from sqlalchemy.ext.sqlsoup import SqlSoup
[*]from sqlalchemy import create_engine
[*]"""连接数据库"""
[*]db_data = create_engine('oracle://username:password@xxx.xxx.xxx.xxx:1521/logpub',pool_recycle=60)
[*]db = SqlSoup(db_data)
[*]
[*]"""查询数据"""
[*]sql = "select t.user_name,t.channel_name,t.channel_id,t.local_path from PUB_URL t where t.content_date = to_date('2015-02-26','yyyy-mm-dd') and t.local_path like '%%/data/Data%%'"
[*]channels_massage = db.connection().execute(sql).fetchall()
[*]db.session.close()
2、连接mysql数据库:



[*]import MySQLdb

[*]
[*]conn_athena = MySQLdb.connect(host="192.168.xx.xx",user='xxx',passwd='xxxx',db='xxx')
[*]cursor_athena = conn_athena.cursor()
[*]sql = "update hadoop_jobs t set t.status = 0"
[*]cursor_athena.execute(sql)
[*]cursor_athena.commit()
[*]cursor_athena.close()
[*]conn_athena.close()
3、连接redis数据库:



[*]import redis

[*]
[*]r = redis.Redis(host=ip, port=port, password=pwd, db=db)
[*]keys = r.keys()
[*]print keys
仅用于个人总结,仅供参考!
页: [1]
查看完整版本: python连接几种数据库方法