zoukankan      html  css  js  c++  java
  • postgres数据库小记

    因为工作中需要查询其他部门数据库,刚好是postgres数据库,用python链接的话,可以用psycopg2库,具体用法其实和MysqlDB是一样的,就是安装的时候遇到点问题.

    安装的时候,提示:

    要解决这个这个问题的话,可以安装下这个库:

    apt-get build-dep python-psycopg2

    就可以安装成功了,具体的用法:

    #-*-coding:utf-8
    import psycopg2
    import logging
    
    log=logging.getLogger(__name__)
    
    class PostgreSql(object):
    
        def __init__(self):
            self.database="postgres"
            self.user="guest"
            self.password="guest_110"
            self.host="172.20.205.108"
            self.port=2345
    
        def execute(self,sql,params=None):
            results=None
            try:
                with psycopg2.connect(host=self.host,port=self.port,database=self.database,
                    user=self.user,password=self.password) as conn:
                    with conn.cursor() as cur:
                        cur.execute(sql,params)
                        results=cur.fetchall()
            except psycopg2.OperationalError,e:
                log.error("psysqlerror:%s"%(e.message))
            return results
  • 相关阅读:
    AJAX 跨域请求与 JSONP详解
    深入理解PHP的mvc框架
    读文文件md5值
    快速排序
    编写简单GUI程序
    简单的加减法
    rallway.py
    用列表构建栈结构
    模拟用户登陆注册
    密码生成
  • 原文地址:https://www.cnblogs.com/xiamuyouren/p/3300989.html
Copyright © 2011-2022 走看看