Python psycopg2: Access Tuple - python

We have a PostGres Database which I am accessing with Python. When Querying for a column with type bigint I get back a dictionary with in the following format:
[[263778L], [30188L], [97L], [12215192L], [702819L], [1301581L], [11101568L], [4712L], [1107866L]]
I need to add these values up together, but I cannot access them as integers.
Fails:
...
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT column1 FROM relation1
rec = cur.fetchall()
for row in rec:
print(re.findall('\d+', row))
Python is returning:
TypeError: expected string or buffer
How to achieve what I want?

This is a list of lists:
[[263778L], [30188L], [97L], [12215192L], [702819L], [1301581L], [11101568L], [4712L], [1107866L]]
not a dictionary. To print each value:
for row in rec:
print(row[0])

Related

Inserting String arrays in a oracle db single column table using python

json_object = []
for i in range(len(df_dictionary)):
json_object.append(json.dumps(df_dictionary[i], indent=4, default=str))
How do I insert my json_object containing list of json strings in array (40) into DB with single column with 40 rows of string?
cmd = "INSERT INTO STG_ETL_RBT(PAYLOAD) VALUES (:1)"
cursor.executemany(cmd, json_object)
how to change the sql statement according to the requirement mentioned?
cmd = "INSERT INTO STG_ETL_RBT(PAYLOAD) VALUES (:1)"
cursor.executemany(cmd, [(i,) for i in json_object])
[(i,) for i in json_object] adding this code in parameter does the job 👍

psycopg2.extras.DictCursor not returning dict in postgres

Im using psycopg2 to access postgres database using the below query. In order to return a dictionary from the executed query, im using DictCursor in my cursor but still my output is a list and not a dictonary.
Here is the program and output below.
import psycopg2.extras
try:
conn = psycopg2.connect("user='postgres' host='localhost' password='postgres'",
)
except:
print "I am unable to connect to the database"
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("""SELECT datname from pg_database""")
rows = cur.fetchall()
print "\nShow me the databases:\n"
print rows
Output:-
[['template1'], ['template0'], ['postgres'], ['iip'], ['test'], ['test_postgres'], ['testdb']]
It looks like a list, smells like a list, but it's a DictRow.
rows = cur.fetchall()
for row in rows :
print(type(row))
#>>> <class 'psycopg2.extras.DictRow'>
This means that you can still use the column names as keys to access the data :
rows = cur.fetchall()
print([row['datname'] for row in rows])
This class inherits directly from the builtinlist and add all the needed methods to implement a dictionary logic, but it doesn't change the representation __repr__ or __str__, so the output is the same as a list.
class DictRow(list):
"""A row object that allow by-column-name access to data."""
fetchall() packs all the queried rows in a list without specifying the exact type.
Btw, maybe you are looking for this kind of cursor : RealDictCursor ?
For those who came where because they really like the easy reference of the dictionary for column:value record representation, the answer by PRMoureu which notes that the DictRow has all the usual dictionary logic means that you can iterate over the DictRow with .items() and get the key:value pairs.
rows = cur.fetchall()
row_dict = [{k:v for k, v in record.items()} for record in rows]
Will turn your list of DictRow records into a list of dict records.

How to retrieve SQL result column value using column name in Python using MySql data base?

Is there a way to retrieve SQL result column value using column name instead of column index in Python?
result = `select name, deptname from employee;`
I tried below one:
cursor = conn.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("SELECT name, category FROM animal")
result_set = cursor.fetchall()
for row in result_set:
print "%s, %s" % (row["name"], row["deptname"])
Here I got an error like:
TypeError: tuple indices must be integers, not str
Any help will be awesome?
Actually you are getting what you wanted.
You just need to use the row not as a dictionary but as a tuple.
That means that the indices are numbers, not strings.
Try
print "%s, %s" % (row[0], row[1])
instead
You are not correctly specifying that you need a dictionary cursor. Do that when you call the connect() method, set the cursorclass:
import MySQLdb
conn = MySQLdb.connect(user='user',
passwd='password',
db='db_name',
cursorclass=MySQLdb.cursors.DictCursor)
cursor = conn.cursor()

Converting a tuple into an integer in python 3?

I have a tuple with a single value that's the result of a database query (it gives me the max ID # currently in the database). I need to add 1 to the value to utilize for my subsequent query to create a new profile associated with the next ID #.
Having trouble converting the tuple into an integer so that I can add 1 (tried the roundabout way here by turning the values into a string and then turning into a int). Help, please.
sql = """
SELECT id
FROM profiles
ORDER BY id DESC
LIMIT 1
"""
cursor.execute(sql)
results = cursor.fetchall()
maxID = int(','.join(str(results)))
newID = maxID + 1
If you are expecting just the one row, then use cursor.fetchone() instead of fetchall() and simply index into the one row that that method returns:
cursor.execute(sql)
row = cursor.fetchone()
newID = row[0] + 1
Rather than use an ORDER BY, you can ask the database directly for the maximum value:
sql = """SELECT MAX(id) FROM profiles"""

get column names from query result using pymssql

Is there any way to get the column names from the pymssql results? If i specify as_dict=True I get back a dictionary, which does contain all the column headers, but since it is a dictionary they are not ordered.
pymssql claims to support the Python DB-API, so you should be able to get the .description attribute from your cursor object.
.description
This read-only attribute is a sequence of 7-item
sequences.
Each of these sequences contains information describing
one result column:
(name,
type_code,
display_size,
internal_size,
precision,
scale,
null_ok)
So, the first item in each of the "inner" sequences is the name for each column.
You can create a list of ordered column names using list comprehension on the cursor description attribute:
column_names = [item[0] for item in cursor.description]
To get the column names on a single comma separated line.
colNames = ""
for i in range(len(cursor.description)):
desc = cursor.description[i]
if i == 0:
colNames = str(desc[0])
else:
colNames += ',' + str(desc[0])
print colNames
Alternatively, pass the column names to a list and use .join to get them as string.
colNameList = []
for i in range(len(cursor.description)):
desc = cursor.description[i]
colNameList.append(desc[0])
colNames = ','.join(colNameList)
print colNames
It's a basic solution and need optimizing but the below example returns both column header and column value in a list.
import pymssql
def return_mssql_dict(sql):
try:
con = pymssql.connect(server, user, password, database_name)
cur = con.cursor()
cur.execute(sql)
def return_dict_pair(row_item):
return_dict = {}
for column_name, row in zip(cur.description, row_item):
return_dict[column_name[0]] = row
return return_dict
return_list = []
for row in cur:
row_item = return_dict_pair(row)
return_list.append(row_item)
con.close()
return return_list
except Exception, e:
print '%s' % (e)

Categories