Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有人知道如何在sqllite中将数据库中的列提取到数组中吗?_Python_Sqlite - Fatal编程技术网

Python 有人知道如何在sqllite中将数据库中的列提取到数组中吗?

Python 有人知道如何在sqllite中将数据库中的列提取到数组中吗?,python,sqlite,Python,Sqlite,我有python中的数组,如: Temperature = ([34, 23.4, 25.5, 16.7]) Humidity = ([89, 93, 78, 59, 61)] 我已经将它们作为.db文件存储在SQLlite中 Temperature Humidity 34 89 23.4 93 25.5 78 25.5 59 16.7 61 现在,我想从我的存储器中取出数据。有人知道如何将列取回

我有python中的数组,如:

Temperature = ([34, 23.4, 25.5, 16.7])
Humidity = ([89, 93, 78, 59, 61)]
我已经将它们作为.db文件存储在SQLlite中

Temperature  Humidity
34            89
23.4          93
25.5          78
25.5          59
16.7          61
现在,我想从我的存储器中取出数据。有人知道如何将列取回数组吗


我不知道您使用的是SQLite还是MySQL,但是方法是相同的,如果不是非常相似的话

您需要做的第一件事是用python与数据库建立连接。如果您使用的是mySQL,则需要导入一个库。一旦建立了连接,您将需要设置一个游标,使您能够使用数据库功能

#This is for a mySQL database
import mysql.connector

conn = mysql.connector.connect(host='#Your hose name goes here',
                               user='#your user name goes here',
                               password='#your password goes here',
                               db='#name of your db goes here')

#This cursor enables database functions
cur = conn.cursor()

cur.execute('USE #enter database name here;')
cur.execute('SELECT Temperature FROM #insert name of database here;')

#To access the column we will store the data from the Temperature column into the  
#temperature var

temperature=cur.fetchall()

#Create an empty array 
temp_data=[]

#The for loop will be used to iterate through the data in the Temperature column
#and store it in the array
for data in temperature:
    temp_data.append(data)
如果您使用的是SQLite,则需要一个不同的库来连接到数据库。您只需像这样连接到数据库:

import sqlite3
conn= sqlite3.connect('#name of your file.db')

希望这有帮助

如果您使用的是SQLite,那么SQLITE3moduleforPython是实现这一点的常用方法。谢谢,它工作得很好。目前,我将湿度存储在数据库中,但不知怎的。它不会像文本一样显示温度值,而是二进制BLOB…然后当我收集数据回来时。我得到的不是数字,而是类似于:b'Z\x00\x00\x00',b'\\\x00\x00\x00',,…嗯,我不完全明白你在说什么。因此,代码适用于温度列,但不适用于湿度列??在循环add conn.commit的温度末尾。这将结束您在数据库中执行的当前查询,这样一个查询就不会干扰另一个查询。很高兴听到这个消息!如果我的答案有帮助,你介意接受它作为答案吗?