Select/Fetch PostgreSQL/Python 中的有符号整数

Select/Fetch signed integer in PostgreSQL/Python

我想 Select 并从 measurement_id 列中获取整数 table measurements,否则,我通过 Python 的一些后处理以某种方式将单项大小的元组转换为有符号整数。 我在 MySQL 中知道这个 Selecting/casting output as integer in SQL 我的 PostgreSQL 9.4

伪代码
CREATE TABLE measurements (
        measurement_id SERIAL PRIMARY KEY NOT NULL,
        measurement_size_in_bytes INTEGER NOT NULL,
        time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
);  

-- sample data
INSERT INTO measurements (measurement_size_in_bytes) VALUES (77777), (5065), (888);

SELECT CAST(measurement_id AS SIGNED) FROM measurements ORDER BY time desc limit 1;

投射不起作用,甚至 measurement_id::int 也不行。 可能 format 是这里的正确工具。 做 print "measurement_id ", _measurement_id; 给出元组 ((21,)),而它应该只给出 21

发起并运行 Python

sudo -u postgres psql detector -f 12.7.2015_creates.sql;
DROP TABLE
CREATE TABLE
$ sudo -u postgres psql detector -c "INSERT INTO measurements (measurement_size_in_bytes) VALUES (77777), (5065), (888);"
INSERT 0 3
$ sudo -u postgres psql detector -c "INSERT INTO measurements (measurement_size_in_bytes) VALUES (77777), (5065), (888);"
INSERT 0 3
$ sudo -u postgres psql detector -c "SELECT measurement_id FROM measurements ORDER BY time desc limit 1";
 measurement_id 
----------------
              7
(1 row)

但在 Python 我需要使用数据的地方做同样的事情

import sys
import os
import struct
import psycopg2
conn_string = "dbname=detector user=postgres password=1234 host=localhost port=5432"
print "Connecting to database\n    ->%s" % (conn_string)     
conn = psycopg2.connect(conn_string)
cursor = connection.cursor()
cursor.execute(
    'SELECT measurement_id FROM measurements ORDER BY time desc limit 1;'
);
_measurement_id = cursor.fetchone();
print "measurement_id ", _measurement_id, " javist!";

回归

Connecting to database
    ->dbname=detector user=postgres password=1234 host=localhost port=5432
measurement_id  (11,)  javist!

我想打印 measurement_id 11 javist!

如何从 PostgreSQL/Python 中的元组 select/fetch 签署整数?

OS:Debian 8.5
硬件:联想

fetchone returns 一个元组。只获取它的第一个元素:

_measurement_id = cursor.fetchone()[0];