How to fetch just one column as array in python with pymysql;
for example sql:
select name from users
data:
["Tom", "Ben", "Jon"]
cursor = conn.cursor() # where conn is your connection
cursor.execute('select name from users')
rows = cursor.fetchall()
result_list = [row[0] for row in rows]
Related
I am trying to capture the only the record from a PostgreSQL statement. The select statements outputs one row with column named as updated_at and the value is a timestamp- '2008-01-01 00:50:01'. I want to just capture/collect that value so when I call that variable, it just outputs '2008-01-01 00:50:01'.
Here is my code:
def get_etl_record():
pg_hook = PostgresHook(postgre_conn_id="post", schema='schema1')
connection = pg_hook.get_conn()
cursor = connection.cursor()
cursor2 = connection.cursor()
latest_update_query = "select max(updated_at) from my_table group by updated_at"
cursor.execute(latest_update_query)
#results= cursor.fetchall()
columns = [col[0] for col in cursor.description]
rows = [dict(zip(columns, row[0])) for row in cursor.fetchall()]
print(rows)
However this code doesnt give me an output.
Any ideas or suggestions?
There is no way to do what you want, but 3 ways to do very similar:
1.
cursor = connection.cursor()
cursor.execute(sql)
result = cursor.fetchone()
max_updated_at = result[0]
2.
dict_cur = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
dict_cur.execute('select max(updated_at) as max_updated_at ...')
result = dict_cur.fetchone()
max_updated_at = result['max_updated_at']
3.
nt_cur = connection.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
nt_cur.execute('select max(updated_at) as max_updated_at ...')
result = nt_cur.fetchone()
max_updated_at = result.max_updated_at
I am running a SQL statement and fetching the results(column/header) and I am trying to collect only the headers that have _check at the end of it and then do a .replace command to get a list of the newly named columns without the _check at the end.
Here is my code:
pg_hook = PostgresHook(postgre_conn_id="postgres_default", schema='schema1')
connection = pg_hook.get_conn()
col_query = "select * from schema.table"
cursor = connection.cursor()
cursor.execute(col_query)
#fetchall to dictonary
desc = cursor.description
column_names = [col[0] for col in desc]
data = [dict(zip(column_names, row)) for row in cursor.fetchall()]
for x in column_names:
if x='updated_check':
x.replace('_check','')
connection.commit()
connection.close()
Any ideas or suggestions on how to replace the name of the columns that have _check in them and them put them inside a list? Any help would be appreciated. I am using airflow, python, mysqldb, and psycopg2
I am using python3, postgress 10 and Psycopg2 to query multiple records like so
import psycopg2
conn = psycopg2.connect(<my connection string>)
with conn:
with conn.cursor() as cur:
cur.execute('select id,field1 from table1')
for id, field1 from cur.fetchall():
print(id,field1)
#todo: how up update field1 to be f(field1) where f is an arbitrary python function
My question is: how do i update the value of the rows that I am reading and set the value of field1 to some arbitrary python-based calculation
edit: the purpose is to update the rows in the table
You need another cursor, e.g.:
with conn:
with conn.cursor() as cur:
cur.execute('select id,field1 from table1')
for id, field1 in cur.fetchall():
print(id,field1)
with conn.cursor() as cur_update:
cur_update.execute('update table1 set field1 = %s where id = %s', (f(field1), id))
Note however that this involves as many updates as selected rows, which is obviously not efficient. The update can be done in a single query using psycopg2.extras.execute_values():
from psycopg2.extras import execute_values
with conn:
with conn.cursor() as cur:
cur.execute('select id,field1 from table1')
rows = cur.fetchall()
for id, field1 in rows:
print(id,field1)
# convert rows to new values of field1
values = [(id, f(field1)) for id, field1 in rows]
sql = '''
with upd (id, field1) as (values %s)
update table1 t
set field1 = upd.field1
from upd
where upd.id = t.id
'''
with conn.cursor() as cur:
execute_values(cur, sql, values)
I have a python script that should insert a list into a sqlite table. It appears my insert statement is not working.
links = ['a', 'b', 'c']
conn = sqlite3.connect('example.db')
#create a data structure
c = conn.cursor()
#Create table
c.execute('''Create TABLE if not exists server("sites")''')
#Insert links into table
def data_entry():
sites = links
c.execute("INSERT INTO server(sites) VALUES(?)", (sites))
conn.commit()
#query database
c.execute("SELECT * FROM server")
rows = c.fetchall()
for row in rows:
print(row)
conn.close
I checked the database at command line but the "server" table is empty:
C:\App\sqlite\sqlite_databases>sqlite3
SQLite version 3.17.0 2017-02-13 16:02:40
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .tables
server
sqlite> SELECT * FROM server
...> ;
sqlite>
So it doesn't appear that the list is actually being inserted.
Iterate over list_ and execute INSERT for each item. And call data_entry() to actually insert data.
import sqlite3
list_ = ['a', 'b', 'c']
#create a data structure
conn = sqlite3.connect('example.db')
c = conn.cursor()
#Create table
c.execute('''Create TABLE if not exists server("sites")''')
#Insert links into table
def data_entry():
for item in list_:
c.execute("INSERT INTO server(sites) VALUES(?)", (item,))
conn.commit()
data_entry() # ==> call the function
#query database
c.execute("SELECT * FROM server")
rows = c.fetchall()
for row in rows:
print(row)
conn.close()
Make a 2-dimensional list and use executemany().
links = ['a', 'b', 'c']
conn = sqlite3.connect('example.db')
#create a data structure
c = conn.cursor()
#Create table
c.execute('''Create TABLE if not exists server("sites")''')
#Insert links into table
def data_entry(links):
sites = [(s,) for s in links]
c.executemany("INSERT INTO server(sites) VALUES(?)", sites)
conn.commit()
data_entry(links)
#query database
c.execute("SELECT * FROM server")
rows = c.fetchall()
for row in rows:
print(row)
conn.close
import sqlite3
#connect to the sqlite database
conn = sqlite3.connect('database.db')
#create a cursor
c = conn.cursor()
#select query to return a single row
c.execute('SELECT NAME FROM T1')
#row contains the returned result
row = c.fetchone()
#print the result
print(row)
It prints something like --> (u'John',), but I only want John
You are printing the whole row, which is always going to be a tuple.
If you wanted to print just the first column, use subscription:
print(row[0])