Python + Postgres

#python 3.4 
#Импортируем модуль и делаем псевдоним
#http://python.projects.pgfoundry.org/

import postgresql.driver as pg_driver

#Создаем подключение к базе

db = pg_driver.connect(user='**', password='**', host='**', database='*', port=5432)

#Запрос и его перебор

#for name, in db.prepare("SELECT name FROM public.docs_types"):

f = open('ajax.php', 'w')
r=[]
e=[]
c=[]

for F_ConvertedValue,F_Date,F_TagName_ID, in db.prepare('''SELECT "F_ConvertedValue", 
 EXTRACT(EPOCH FROM "F_Date"),

 "F_TagName_ID" FROM

 "SC_Tag"."T_TagData"

 where

 "F_Date" > (localtimestamp - interval'1 day') ORDER BY "F_Date" asc;'''):

 r.append(F_ConvertedValue)
 e.append(F_Date)
 c.append(F_TagName_ID)
f.write('[')
for i in range(len(r)):
 f.write('['+str(r[i])+','+str(e[i])[:-2]+','+str(c[i])+']')
f.write("]")
f.close()

#Закрываем подключениеdb.close()

--------------
#python 2
#https://wiki.postgresql.org/wiki/Psycopg
import psycopg2

conn = psycopg2.connect(user='**', password='**', host='127.0.0.1', dbname='DB_**', port=5432)
cur = conn.cursor()

#for name, in db.prepare("SELECT name FROM public.docs_types"):
f = open('ajax.php', 'w')
r=[]
e=[]
c=[]
cur.execute('''SELECT   "F_ConvertedValue", EXTRACT(EPOCH FROM   "F_Date"), 
 
                                                             "F_TagName_ID" FROM 
                                                             "SC_Tag"."T_TagData" 
                                                             where 
                                                             "F_Date" > (localtimestamp - interval'1 day') ORDER BY "F_Date" asc;''')
row_count = 0
for row in cur:
 row_count += 1
        r.append(row[0])
        e.append(row[1])
        c.append(row[2])
f.write('[')
for i in range(len(r)):
    f.write('['+str(r[i])+','+str(e[i])[:-2]+','+str(c[i])+']')
f.write("]")
f.close()

cur.close()
conn.close()

Комментарии

Популярные сообщения из этого блога

Пишем логи на C# (.NET). Легкий способ.

Учебник yii2