SQL将输出从整数转换为float

发布于 2025-01-25 02:39:56 字数 1378 浏览 1 评论 0原文

我的结果中的数字是类型的整数,但我想将它们转换为float。

import sqlite3
connection = sqlite3.connect("mydatabase.db")
my_cursor = connection.cursor()
my_cursor.execute("DROP TABLE IF EXISTS Weather")
my_cursor.execute("CREATE TABLE Weather(City, State, High, Low)")
my_cursor.execute("INSERT INTO Weather Values('Phoenix', 'Arizona', 105, 90)")
my_cursor.execute("INSERT INTO Weather Values('Tucson', 'Arizona', 101, 92)")
my_cursor.execute("INSERT INTO Weather Values('Flag Staff', 'Arizona', 105, 90)")
my_cursor.execute("INSERT INTO Weather Values('San Diego', 'California', 77, 60)")
my_cursor.execute("INSERT INTO Weather Values('Albuquerqu', 'New Mexico', 80, 72)")
my_cursor.execute("INSERT INTO Weather Values('Nome', 'Alaska', 64, -54)")

if __name__=="__main__":

    
    for i in my_cursor.execute("SELECT * FROM Weather"):
        print(i)
    

输出是:

('Phoenix', 'Arizona', 105, 90)
('Tucson', 'Arizona', 101, 92)
('Flag Staff', 'Arizona', 105, 90)
('San Diego', 'California', 77, 60)
('Albuquerqu', 'New Mexico', 80, 72)
('Nome', 'Alaska', 64, -54)

我很困惑如何掩盖整数浮动。修复后,输出应如下:

('Phoenix', 'Arizona', 105.0, 90.0)
('Tucson', 'Arizona', 101.0, 92.0)
('Flag Staff', 'Arizona', 105.0, 90.0)
('San Diego', 'California', 77.0, 60.0)
('Albuquerqu', 'New Mexico', 80.0, 72.0)
('Nome', 'Alaska', 64.0, -54.0)

The numbers in my results are of type integer, but I want to convert them to float.

import sqlite3
connection = sqlite3.connect("mydatabase.db")
my_cursor = connection.cursor()
my_cursor.execute("DROP TABLE IF EXISTS Weather")
my_cursor.execute("CREATE TABLE Weather(City, State, High, Low)")
my_cursor.execute("INSERT INTO Weather Values('Phoenix', 'Arizona', 105, 90)")
my_cursor.execute("INSERT INTO Weather Values('Tucson', 'Arizona', 101, 92)")
my_cursor.execute("INSERT INTO Weather Values('Flag Staff', 'Arizona', 105, 90)")
my_cursor.execute("INSERT INTO Weather Values('San Diego', 'California', 77, 60)")
my_cursor.execute("INSERT INTO Weather Values('Albuquerqu', 'New Mexico', 80, 72)")
my_cursor.execute("INSERT INTO Weather Values('Nome', 'Alaska', 64, -54)")

if __name__=="__main__":

    
    for i in my_cursor.execute("SELECT * FROM Weather"):
        print(i)
    

The output is:

('Phoenix', 'Arizona', 105, 90)
('Tucson', 'Arizona', 101, 92)
('Flag Staff', 'Arizona', 105, 90)
('San Diego', 'California', 77, 60)
('Albuquerqu', 'New Mexico', 80, 72)
('Nome', 'Alaska', 64, -54)

I am confused that how to covert the integer to float. After fixing, the output should be like:

('Phoenix', 'Arizona', 105.0, 90.0)
('Tucson', 'Arizona', 101.0, 92.0)
('Flag Staff', 'Arizona', 105.0, 90.0)
('San Diego', 'California', 77.0, 60.0)
('Albuquerqu', 'New Mexico', 80.0, 72.0)
('Nome', 'Alaska', 64.0, -54.0)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅浅 2025-02-01 02:39:56

允许回答的权限,也许您可​​以尝试一下:

CREATE TABLE weather (
  city VARCHAR(100),
  state VARCHAR(100),
  high DEC(4,1),
  low DEC(4,1)
  );

INSERT INTO weather VALUES('Phoenix', 'Arizona', 105, 90);
INSERT INTO weather VALUES('Tucson', 'Arizona', 101, 92);
INSERT INTO weather VALUES('Flag Staff', 'Arizona', 105, 90);
INSERT INTO weather VALUES('San Diego', 'California', 77, 60);
INSERT INTO weather VALUES('Albuquerqu', 'New Mexico', 80, 72);
INSERT INTO weather VALUES('Nome', 'Alaska', 64, -54);

SELECT * FROM weather;

谢谢...

Permission to answer, maybe you can try this:

CREATE TABLE weather (
  city VARCHAR(100),
  state VARCHAR(100),
  high DEC(4,1),
  low DEC(4,1)
  );

INSERT INTO weather VALUES('Phoenix', 'Arizona', 105, 90);
INSERT INTO weather VALUES('Tucson', 'Arizona', 101, 92);
INSERT INTO weather VALUES('Flag Staff', 'Arizona', 105, 90);
INSERT INTO weather VALUES('San Diego', 'California', 77, 60);
INSERT INTO weather VALUES('Albuquerqu', 'New Mexico', 80, 72);
INSERT INTO weather VALUES('Nome', 'Alaska', 64, -54);

SELECT * FROM weather;

Thank you...

望喜 2025-02-01 02:39:56

sqlite3使用明显键入打字

在清单键入中,数据类型是值本身的属性,而不是存储该值的列的属性。

值可以在插入时间存储为浮子,例如:

INSERT INTO Weather Values('Phoenix', 'Arizona', 105.0, 90.0)

可以在选择时显示为浮子,例如:

SELECT city, state, high * 1.0, low * 1.0 from weather

sqlite3 uses manifest typing

In manifest typing, the datatype is a property of the value itself, not of the column in which the value is stored.

The values can be stored as floats at INSERT time, eg:

INSERT INTO Weather Values('Phoenix', 'Arizona', 105.0, 90.0)

The values can be displayed as floats at SELECT time, eg:

SELECT city, state, high * 1.0, low * 1.0 from weather
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文