目前cur.fetchone
返回"无"。但是,如果ID=?, (number_row,)
(或其他数字)的更改Id=2
全部按计划运行。改变" cur.fetchone" to" cur.fetchone"正在返回空元组。我哪里出错了?
import sqlite3
import numpy as np
con = sqlite3.connect('database_all.db')
power_list = np.array([-70, 200, 300, 400, 480, 520, 600])
temperature = 90
number_row = np.searchsorted(power_list, temperature, side='right')
print(number_row)
cur = con.cursor()
cur.execute("SELECT VARIANTS from P_steel_3_2 WHERE ID=?", (number_row,))
pressure_variants = cur.fetchone()
print(pressure_variants)
cur.close()
我的表格(在DB浏览器中为SQLite生成)
BEGIN TRANSACTION;
CREATE TABLE "P_steel_3_2" (
`ID` INTEGER,
`TEMPERATURE` REAL,
`VARIANTS` REAL
);
INSERT INTO `P_steel_3_2` (ID,TEMPERATURE,VARIANTS) VALUES (0,'-70_200','0.1, 0.25, 0.4, 0.6 ,1 ,1.6, 2.5 ,4, 6.4, 10, 16');
INSERT INTO `P_steel_3_2` (ID,TEMPERATURE,VARIANTS) VALUES (1,'200-300','0.09, 0.22, 0.36, 0.56, 0.9, 1.4, 2.2, 3.6, 5.6, 9, 14');
INSERT INTO `P_steel_3_2` (ID,TEMPERATURE,VARIANTS) VALUES (2,'300-400','0.08, 0.2, 0.32, 0.5, 0.8, 1.25, 2, 3.2, 5, 8, 12.5');
INSERT INTO `P_steel_3_2` (ID,TEMPERATURE,VARIANTS) VALUES (3,'400-480','0.07, 0.18, 0.28, 0.45, 0.7, 1.1, 1.8, 2.8, 4.5, 7.1, 10.2');
INSERT INTO `P_steel_3_2` (ID,TEMPERATURE,VARIANTS) VALUES (5,'520-600','0.05, 0.11, 0.18, 0.28, 0.45, 0.7, 1.1, 1.8, 2.8, 4.5, 7.1');
INSERT INTO `P_steel_3_2` (ID,TEMPERATURE,VARIANTS) VALUES (6,'600-700','0, 0.06, 0.09, 0.15, 0.22, 0.36, 0.56, 0.9, 1.4, 2.2, 3.6');
INSERT INTO `P_steel_3_2` (ID,TEMPERATURE,VARIANTS) VALUES (4,'480-520','0.06, 0.16, 0.25, 0.4, 0.64, 1, 1.6, 2.5, 4, 6.4, 10');
COMMIT;
答案 0 :(得分:1)
np.searchsorted
会返回一系列索引,因此它会返回[2]
而不是2
。您需要提取元素以在查询中使用它。
if number_row.ndim > 0 and number_row.shape[0] > 0:
id = number_row[0]
cur = con.cursor()
cur.execute("SELECT VARIANTS from P_steel_3_2 WHERE ID=?", (id,))
pressure_variants = cur.fetchone()
print(pressure_variants)
cur.close()