精华内容
下载资源
问答
  • psycopg2

    2017-10-11 12:42:00
    示例2:批量表操作 用法补充 回到顶部 简介 Psycopg 是Python语言的PostgreSQL数据库接口。 它的主要优势在于完全支持Python DB API 2.0,以及安全的多线程支持。它适用于随时创建、销毁大量游标的、和产生大量...

    简介

    Psycopg 是Python语言的PostgreSQL数据库接口。 它的主要优势在于完全支持Python DB API 2.0,以及安全的多线程支持。它适用于随时创建、销毁大量游标的、和产生大量并发INSERT、UPDATE操作的多线程数据库应用。Psycopg包内含 ZPsycopgDA,一个Zope数据库接口。

    示例1:新建表、插入、修改

    复制代码
    #-*- coding: utf-8 -*-
    import psycopg2
    
    def main(user,pwd,ip,port,dbname):
        connection = "dbname=%s user=%s password=%s host=%s port=%s" % (dbname, user,pwd, ip, port)
    
        db = psycopg2.connect(connection)
        cur = db.cursor()
        #创建表
        sql_stat = "CREATE TABLE test_class_id(id INT PRIMARY kEY NOT NULL, test_class TEXT, test_id CHAR(10))";
        cur.execute(sql_stat)
        #插入表
        sql_stat = "insert into test_class_id(id, test_class, test_id) values (1, 'a', '3')"
        cur.execute(sql_stat)
    
        sql_stat = "insert into test_class_id(id, test_class, test_id) values (2, 'a', '3')"
        cur.execute(sql_stat)
        #更改字段值
        sql_stat = "update test_class_id set test_class='b' where id=1"
        cur.execute(sql_stat)
    
        db.commit()
    
    if __name__ == "__main__":
        user = '****'
        pwd = '****'
        ip = '***'
        port = '5432'
        dbname = '****'
        main(user, pwd, ip, port, dbname)
        print "Done~~"
    复制代码

    结果

    :这里有多次提交execute  最后一次提交commit,如果没有最后的一次commit,你们前面的提交也是不执行的。

    示例2:批量表操作

    复制代码
     -*- coding: utf-8 -*-
    import psycopg2
    
    class_ids = []
    class_ids.append({"test_id": 1, "test_class":"A", "test_id":"1"})
    class_ids.append({"test_id": 2, "test_class":"A", "test_id":"2"})
    class_ids.append({"test_id": 3, "test_class":"B", "test_id":"3"})
    
    def main(user,pwd,ip,port,dbname):
        connection = "dbname=%s user=%s password=%s host=%s port=%s" % (dbname, user,pwd, ip, port)
        db = psycopg2.connect(connection)
        cur = db.cursor()
    
        #sql_del = "delete from cdb_chk_group"
        #cur.execute(sql_del)
        #批量操作
        sql_stat = 'insert into test_class_id(id, test_class, test_id) values (%(test_id)s, %(test_class)s,%(test_id)s)'
        cur.executemany(sql_stat, class_ids)
    
        db.commit()
    
    if __name__ == "__main__":
        user = '****'
        pwd = '****'
        ip = '****'
        port = '5432'
        dbname = '****'
        main(user, pwd, ip, port, dbname)
        print "Done~~"
    复制代码

    验证

    用法补充

    1. 查询数据,并输出结果

    查询一条

    >>> cur.execute("select * from test;")
    >>> cur.fetchone()
    (1, 100, "abc'def")

    查询所有条

    >>> cur.execute("select * from test;")
    >>> cur.fetchall()
    [(1, 100, "abc'def"),(2, 100, "abc'def")]

     







    本文转自jihite博客园博客,原文链接:http://www.cnblogs.com/kaituorensheng/p/5180104.html,如需转载请自行联系原作者


    展开全文
  • psycopg2-λ层 适用于psycopg2的AWS Lambda层 要在您的serverless.yml中使用: functions: hello: handler: handler.hello layers: # py 3.6: - arn:aws:lambda:us-east-1:898466741470:layer:psycopg2-py36:3...
  • python-relations-psycopg2 psycopg2的DB / API建模
  • 龙卷风Psycopg2 支持Psycopg2异步模式的Tornado驱动程序。
  • psycopg2-windows, & virtualenv 友好版本的psycopg2用于 Windows psycopg2-windowsPip,Windows,和 virtualenv 友好版本的psycopg2 ! 这工作是因为一切都预先编译好。很酷的东西在 Windows 上轻松安装 psycopg2...
  • I have a fairly light amount of experience with Python and got stuck on what should be a pretty simple ... I am using Jupyter Notebook on a Mac.I wanted to install psycopg2 in Python so I typed in...

    I have a fairly light amount of experience with Python and got stuck on what should be a pretty simple issue. I am using Jupyter Notebook on a Mac.

    I wanted to install psycopg2 in Python so I typed in

    import psycopg2 as pg2

    But, I got the following error:

    ModuleNotFoundError Traceback (most recent call

    last)

    in

    ----> 1 import psycopg2 as pg2

    ModuleNotFoundError: No module named 'psycopg2'

    I then went over to the terminal and typed in:

    pip install psycopg2

    which resulted in another error:

    Error: pg_config executable not found.

    pg_config is required to build psycopg2 from source. Please add the

    directory

    containing pg_config to the $PATH or specify the full executable path

    with the

    option:

    python setup.py build_ext --pg-config /path/to/pg_config build ...

    I'm not sure how to go about using this information in the error message. Can someone help point me in the right direction?

    Also follow up question:

    a.) Why would I need to use pip install modulename in the terminal when I don't have to do that with either pandas or numpy?

    Any help would be appreciated.

    解决方案

    Please try installing psycopg2-binary as you probaly do not have all the C library dependencies installed on your computer.

    pip install psycopg2-binary

    展开全文
  • In Python psycopg2 how can I check if a row exists?def track_exists(self, track_id):cur = self.conn.cursor()cur.execute("SELECT fma_track_id FROM tracks WHERE fma_track_id = %s", (track_id,))if cur.fe...

    In Python psycopg2 how can I check if a row exists?

    def track_exists(self, track_id):

    cur = self.conn.cursor()

    cur.execute("SELECT fma_track_id FROM tracks WHERE fma_track_id = %s", (track_id,))

    if cur.fetchall() > 0:

    return true

    else:

    return false

    Currently I am getting

    Traceback (most recent call last):

    File "", line 1, in

    File "mumu.py", line 38, in track_exists

    if cur.fetchall() > 0:

    TypeError: 'NoneType' object has no attribute '__getitem__'

    解决方案

    Don't use fetchall() (which returns a list, which is never 'larger than 0'), use fetchone():

    def track_exists(self, track_id):

    cur = self.conn.cursor()

    cur.execute("SELECT fma_track_id FROM tracks WHERE fma_track_id = %s", (track_id,))

    return cur.fetchone() is not None

    fetchone() returns None if there is nothing to fetch, and testing against is not None gives you a handy boolean value to return directly.

    展开全文
  • 其实在Python中可以用来连接PostgreSQL的模块很多,这里比较推荐psycopg2psycopg2安装起来非常的简单(pip install psycopg2),这里主要重点介绍下如何使用。 安装psycopg2模块: 怎么验证是否已经安装过psycopy2...
  • pip install psycopg2出现错误: Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting psycopg2 Using cached ...
  • psycopg2
  • psycopg2的安装依赖于python-dev,而mac的python一般是不会自动安装dev,python-dev可以支持一些未在社区正式发布的功能,为了兼容mac,psycopg2有专门的psycopg2-binary版本,所以,在搭建mac环境时,需要安装的时...

    报错代码:

    pip install psycopg2

    报错:

    pg_config executable not found

    错误原因:

    psycopg2的安装依赖于python-dev,而mac的python一般是不会自动安装dev,python-dev可以支持一些未在社区正式发布的功能,为了兼容mac,psycopg2有专门的psycopg2-binary版本,所以,在搭建mac环境时,需要安装的时psycopg2-binary。

    解决:

    先卸载环境中的psycopg2,
    然后pip install psycopg2-binary

    展开全文
  • I'm writing a script to do a copy of some data between two machines on the same network using psycopg2. I'm replacing some old, ugly bash that does the copy withpsql -c -h remote.host "COPY table TO S...
  • psycopg2-2.6.2

    2016-10-08 16:23:26
    psycopg2-2.6.2挺好用的连接lib
  • 在使用Django连接postgresql数据库的时候,使用python ...django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2' 解决办法: apt install psycopg2如果失败了...
  • 在windows下安装psycopg2出现问题,如下面两个图所示: 调查方案: 确认pip install psycopg2成功, pip list也显示psycopg2在列表内,结果无论在pycharm还是python console界面都显示无法找到psycopg2模块...
  • centos7.6安装psycopg2

    2021-03-24 11:41:40
    pip3 install psycopg2 异常: Collecting psycopg2 Using cached psycopg2-2.8.6.tar.gz (383 kB) ERROR: Command errored out with exit status 1: command: /usr/local/python3/bin/python
  • postgresql Psycopg2

    2017-09-05 16:47:06
    import psycopg2 import psycopg2.extras conn = psycopg2.connect(host=’localhost’, port=5432,user=’postgres’, password=’postgres’, database=’test’) # connect()也可以使用一个
  • 关于导包psycopg2

    2021-02-02 18:24:15
    <p>import psycopg2 报错 <p>ImportError: /lib64/libgssapi_krb5.so.2: symbol krb5_ser_context_init, version krb5_3_MIT not defined in file libkrb5.so.3 with link time reference</p>
  • Python PostgreSQL Psycopg2

    2019-10-04 17:35:52
    【转】... Python如果要操作Postgresql,需要一个API,这就需要Psycopg2 1. 链接PostgreSQL并设定Cursorimport psycopg2import psycopg2.extrasconn = psycopg2.connect(host=’localhost’, por...
  • psycopg2笔记

    2019-09-23 17:49:50
    使用psycopg2.connect函数获得connection对象。 使用connection对象创建cursor对象。 使用cursor对象执行sql语句提交或者回滚transaction。 使用cursor对象fetchone获得查询结果。 关闭curso...
  • <p>UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: ...
  • psycopg2模块安装问题

    千次阅读 2019-07-19 11:31:11
    我的平台是win10(x64)、python3.7,打算通过psycopg2模块来操作Greenplum数据库,我通过pip installpsycopg2 安装了psycopg2模块,也提示安装成功了,可是我在importpsycopg2的时候提示我该模块未知、不存在,这让我...
  • postgres sql psycopg2最新版本,外面的资源部豪侠
  • psycopg2使用大全

    万次阅读 2019-04-07 15:06:32
    之前写过python连接postgresql的方法,今天在网上详细总结了一下,psycopg2的使用方法 建立连接 使用*.ini文件(python的configparser包可以解析这种类型的配置文件)保存数据库连接的配置信息。 使用psycopg...
  • 今天在安装服务器环境时,使用Python 安装psycopg2 报错如下 pip3 install psycopg2 Looking in indexes: http://mirrors.cloud.aliyuncs.com/pypi/simple/ Collecting psycopg2 Downloading ...
  • Python:安装 psycopg2

    2021-03-22 18:25:45
    安装psycopg2 的时候,需要添加PostgreSQL 的 bin 位置到 PATH中,否则使用 pip 安装psycopg2 的时候会报错,因为psycopg2 需要根据pg_config 获取 PostgreSQL 程序和库的位置: export PATH=$PATH:/opt/common/...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 6,751
精华内容 2,700
关键字:

psycopg2