psycopg2

Psycopg2 - How to include 'Null' values in inserting dictionary of list to table in postgres?

空扰寡人 提交于 2019-12-24 07:53:19
问题 I'm parsing my xml files and store them to a dictionary of list, where I will insert them into table at posgres using psycopg2. However, not all rows got inserted in the tables (it only inserted to the lowest number of values in the list). Here is the snippet of the dictionary of list: dict_songs = {'title' : ['Need You Now', 'GTFO'...], 'format': ['MP4', 'MP3'...], 'type' : ['Country Pop', 'R&B Pop'..], 'year': [2010,2018..]} dict_movie = {'title' : ['Searching', 'Sidewalk of New York'...],

Psycopg2 : Create a table in a stored procedure Postgres

雨燕双飞 提交于 2019-12-24 07:48:04
问题 Stored Procedure : CREATE OR REPLACE FUNCTION try_create() RETURNS INT AS $$ BEGIN CREATE TABLE hello(id SERIAL PRIMARY KEY, name TEXT); RETURN 1; END ; $$ LANGUAGE plpgsql; test.py import psycopg2 conn = psycopg2.connect(user='a', password='a', dbname='a') cur = conn.cursor() cur.callproc('try_create', ()) print cur.fetchall() I am trying to create a stored procedure which will create a table named hello . I am invoking the same using a python script. Upon running the above script I see the

Error loading psycopg2 module when installed from a 'wheel'

旧时模样 提交于 2019-12-24 05:14:12
问题 I am trying to install some python requirements from a local package directory containing wheel archives. I am installing the requirements inside a Docker container. The steps I'm following are: $ pip install wheel # wheel runs, outputs .whl files to wheelhouse directory $ pip wheel --wheel-dir wheelhouse -r requirements.txt Then, inside my Dockerfile : ADD requirements.txt /tmp/requirements.txt ADD wheelhouse /tmp/wheelhouse # install requirements. Leave file in /tmp for now - may be useful.

TypeError: argument 1 must be a string or unicode object

谁都会走 提交于 2019-12-24 02:59:36
问题 Using this query: 04/25/2017 00:42:28.180 INFO (u"UPDATE posts SET translated_text='Unroll.me CEO dice que es "desgarrador" que los usuarios se molesten Unroll.me vendi\xf3 sus datos de correo electr\xf3nico anonimizado a Uber', detected_language='en' WHERE post_id=2", u'Unroll.me CEO dice que es "desgarrador" que los usuarios se molesten Unroll.me vendi\xf3 sus datos de correo electr\xf3nico anonimizado a Uber') With psycopg2 , when I use: cur.execute(query) I get: TypeError: argument 1 must

How do I use the Postgresql ANY operator in a NOT IN statement

人盡茶涼 提交于 2019-12-24 01:18:47
问题 Using Pyscopg2, how do I pass a Python list into an SQL statement using the ANY Operator? Normal Working SQL reads (See SQL Fiddle): SELECT * FROM student WHERE id NOT IN (3); Using Psycopg2 as below: Psycopg2: Query 1 The query below fails with psycopg2.ProgrammingError: syntax error at or near "ANY" id_list = [2,3,4] cursor.execute("SELECT * FROM student WHERE id NOT IN ANY(%s)) %(id_list); Psycopg2: Query 2 The query below doesn't throw an error but it gives a wrong result because it doesn

Can not “COPY FROM” with Postgres & Python

旧巷老猫 提交于 2019-12-24 00:55:09
问题 As the topic, this is the code and there is no error message but data did not get insert. This is my code, and can anyone tell me what's wrong with it? import psycopg2 import sys import os import glob import csv #open the csv folder dictfile='******' os.chdir(dictfile) total=[] for file in glob.glob("*.csv"): total.append(file) con = None try: con = psycopg2.connect(host='localhost',database='*****',user='postgres', password='*****') cur = con.cursor() for i in range(0,1): filename='/Users

Use python to execute line in postgresql

爷,独闯天下 提交于 2019-12-24 00:53:01
问题 I have imported one shapefile named tc_bf25 using qgis, and the following is my python script typed in pyscripter, import sys import psycopg2 conn = psycopg2.connect("dbname = 'routing_template' user = 'postgres' host = 'localhost' password = '****'") cur = conn.cursor() query = """ ALTER TABLE tc_bf25 ADD COLUMN source integer; ALTER TABLE tc_bf25 ADD COLUMN target integer; SELECT assign_vertex_id('tc_bf25', 0.0001, 'the_geom', 'gid') ;""" cur.execute(query) query = """ CREATE OR REPLACE

psycopg2.ProgrammingError: syntax error at or near “stdin” error when trying to copy_from redshift

吃可爱长大的小学妹 提交于 2019-12-23 16:34:41
问题 I am having this problem when I am trying to copy to AWS redshift. This is the code I am trying to run: with open('path/to/files, 'rb') as fo: cursor.copy_from(fo, 'schema.table', sep=',') cursor.commit() And I encountered the error: psycopg2.ProgrammingError: syntax error at or near "stdin" LINE 1: ...Y schema.table FROM stdin WITH... I am running python 3.5 with psycopg2. Hope that you guys can help! Thx in advance! 回答1: AWS Redshift is not PostgreSQL, though it supports a subset of

Password authentication fails with complex password

佐手、 提交于 2019-12-23 15:36:27
问题 I wrote a Python script which connects to the local PostgreSQL database using psycopg2 2.6 and Python 2.7.8 . The connection settings and commands are the following: HOST = '127.0.0.1' DATABASE_NAME = 'myappdatabase' DATABASE_PORT = '5432' DATABASE_USER = 'myappuser' DATABASE_PASSWORD = 'secret' DATABASE_TABLE = 'myappdata' def _database_connection(): conn_string = "host='{0}' dbname='{1}' port='{2}' user='{3}' \ password='{4}'".format(HOST, DATABASE_NAME, DATABASE_PORT, \ DATABASE_USER,

Python psycopg2 check row exists

北慕城南 提交于 2019-12-23 07:11:00
问题 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 "<stdin>", line 1, in <module> File "mumu.py", line 38, in track_exists if cur.fetchall() > 0: TypeError: 'NoneType' object has no attribute '__getitem__' 回答1: Don't use fetchall()