psycopg2

ERROR: Failed building wheel for mysqlclient

荒凉一梦 提交于 2020-01-24 20:56:05
问题 I'm unable to 'pip install mysqlclient', and every attempt returns the error: 'ERROR: Failed building wheel for mysqlclient'. For context, I'm running on macOS Mojave 10.14.5. The rest of this post assumes that both 'python==3.6' and 'virtualenv' are already downloaded. In addition, x-code command line tools are already installed (not that I think that matters). The steps to this are (from command line): 'virtualenv ~/venv' Output: Using base prefix '/usr/local/Cellar/python/3.6.5_1

psycopg2: Can't adapt type 'UUID'?

跟風遠走 提交于 2020-01-24 12:02:08
问题 I am using psycopg2 to try to insert an entry into a table where the type of the data is the Postgres type 'uuid'. According to this page, I should be able to directly use the Python type uuid.UUID, as in the following code: uuid_entry = uuid.uuid4() command = "INSERT INTO MyTable (uuid) VALUES (%s)" cursor.execute(command, (uuid_entry,)) However, when I try to do this, it throws the error: ProgrammingError(can't adapt type 'UUID') Any ideas on why this is happening? Thanks. 回答1: uuid_entry =

Sharing a postgres connection pool between python multiproccess

亡梦爱人 提交于 2020-01-24 07:29:45
问题 I am trying to use psycopg2's connection pool with python's multiprocess library. Currently, attempting to share the connection pool amongst threads in the manner described above causes: psycopg2.OperationalError: SSL error: decryption failed or bad record mac The following code should reproduce the error, which the caveat that the reader has to set up a simple postgres database. from multiprocessing import Pool from psycopg2 import pool import psycopg2 import psycopg2.extras connection_pool

Cursors with postgres, where is the data stored and how many calls to the DB

╄→尐↘猪︶ㄣ 提交于 2020-01-24 05:00:06
问题 Hi I am using psycopg2 for postgres access. I am trying to understand where "cursor" stores the returned rows. Does it store it in the database as a temporary table or is it on the clients end? Does cursor (when you specify to fetch many rows) hit the database a query at a time or does it hit the database once ,get the first set of results then when you iterate over the returned values, it gets the next set (buffering). I have read multiple articles on cursor but nothing really gives the

Meta commands in Psycopg2 - \d not working

自作多情 提交于 2020-01-23 05:37:05
问题 I wish to list all the column names of a table using psycopg2 package of Python (2.7). But I am unable to execute the following query - cur.execute("\d my_table"); psycopg2.ProgrammingError: syntax error at or near "\" Is there an alternate as to how I can list the column names of a table using psycopg2 ? Please point out any duplicates. Thanks ! 回答1: Command line psql has some shortcuts like \d but it's not part of SQL. What you need is to query information_schema : SELECT column_name FROM

How can I get affected row count from psycopg2 connection.commit()?

那年仲夏 提交于 2020-01-22 13:08:06
问题 Currently, I have the following method to execute INSERT/UPDATE/DELETE statements using psycopg2 in Python : def exec_statement(_cxn, _stmt): try: db_crsr = _cxn.cursor() db_crsr.execute(_stmt) _cxn.commit() db_crsr.close() return True except: return False But what I would really like it to do, instead of bool, is return the row count affected by the transaction or -1 if the operation fails. Is there a way to get a number of rows affected by _cxn.commit() ? E.g. for a single INSERT it would

How can I get affected row count from psycopg2 connection.commit()?

不羁岁月 提交于 2020-01-22 13:07:56
问题 Currently, I have the following method to execute INSERT/UPDATE/DELETE statements using psycopg2 in Python : def exec_statement(_cxn, _stmt): try: db_crsr = _cxn.cursor() db_crsr.execute(_stmt) _cxn.commit() db_crsr.close() return True except: return False But what I would really like it to do, instead of bool, is return the row count affected by the transaction or -1 if the operation fails. Is there a way to get a number of rows affected by _cxn.commit() ? E.g. for a single INSERT it would

How do I install psycopg2 for Python 3.x?

我们两清 提交于 2020-01-22 06:56:04
问题 Just started Python a few days ago and I'm using PyCharm to develop a web application with Django. I have libpq-dev python-dev packages already installed, but it's still throwing me the same error: ./psycopg/psycopg.h:30:20: fatal error: Python.h: No such file or directory which according to Google is the issue that occurs when python-dev package isn't installed. Note that I'm running the install from within the PyCharm interface for a virtualenv that I created for 3.2 and 3.3 (not sure how

How do I install psycopg2 for Python 3.x?

北城余情 提交于 2020-01-22 06:55:25
问题 Just started Python a few days ago and I'm using PyCharm to develop a web application with Django. I have libpq-dev python-dev packages already installed, but it's still throwing me the same error: ./psycopg/psycopg.h:30:20: fatal error: Python.h: No such file or directory which according to Google is the issue that occurs when python-dev package isn't installed. Note that I'm running the install from within the PyCharm interface for a virtualenv that I created for 3.2 and 3.3 (not sure how

我终于学会了使用python操作postgresql

落爺英雄遲暮 提交于 2020-01-22 03:52:38
一 前言 这篇文章不仅适合pgsql,更适合mysql,思路都是一致的,如果读者学会使用psycopg2操作pgsql,那么使用PyMySQL 操作mysql也是很简单;本篇文章涵盖内容广泛,提供的操作选择性很多,比如多种数据插入操作,防止sql注入方式,异常处理,sql语句打印处理,显示行号等操作,一篇文章真的收益匪浅;觉得知识追寻者文章不错,随手点赞谢谢。如果看了作者文章有收获,不关注都是耍流氓。 二 数据库连接 2.1 安装 psycopg2 # pip install psycopg2 2.2 连接数据库 每条完整的sql执行步骤如下,读者应谨记; 建立连接获得 connect 对象 获得游标对象,一个游标对象可以对数据库进行执行操作,非线程安全,多个应用会在同一个连接种创建多个光标; 书写sql语句 调用execute()方法执行sql 抓取数据(可选操作) 提交事物 关闭连接 # -*- coding: utf-8 -*- import psycopg2 # 获得连接 conn = psycopg2.connect(database="python", user="postgres", password="123456", host="127.0.0.1", port="5432") # 获得游标对象 cursor = conn.cursor() # sql语句 sql