Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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从SQLite数据库中提取值_Python_Sqlite - Fatal编程技术网

用Python从SQLite数据库中提取值

用Python从SQLite数据库中提取值,python,sqlite,Python,Sqlite,我试图找出如何使用python处理SQLite数据库,但似乎遇到了问题。我想我错过了一些基本的东西。我遵循本教程: 我设置了一个数据库,其中包含以下信息: import sqlite3 conn = sqlite3.connect('SQL_test_3') #this creates a seperate file c = conn.cursor() c.execute('''create table stocks (date text, trans text, symbol text, q

我试图找出如何使用python处理SQLite数据库,但似乎遇到了问题。我想我错过了一些基本的东西。我遵循本教程:

我设置了一个数据库,其中包含以下信息:

import sqlite3
conn = sqlite3.connect('SQL_test_3') #this creates a seperate file
c = conn.cursor()
c.execute('''create table stocks
(date text, trans text, symbol text,
 qty real, price real)''')
data = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
          ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
          ('2006-04-06', 'SELL', 'IBM', 500, 53.00),
         ]

for t in data:
    c.execute('insert into stocks values (?,?,?,?,?)', t)

conn.commit()

c.close()
当我试图提取数据时,我的问题出现了;本教程介绍了在满足其中一个特征时如何提取数据,例如:

(当我在另一个文件中打开数据库时)

上面的方法很完美,但是如果我想提取所有资产的信息,而这些资产的价格高于50,我就不能这么做。价格>50和价格>吗?不起作用

因此,我的问题是:

1) 当关键标准符合给定范围(例如价格>50或40<价格<70)时,如何提取资产信息

2) 如果我想要两个标准,例如IBM股票的信息,和 如果股票以高于50的价格交易

我觉得我的问题非常初级/基本,但我在教程中找不到答案

感谢您的帮助

提前谢谢

c.execute('select * from stocks where price > ?', (50,))

c.execute('select * from stocks where price between ? and ?', (40, 70))

c.execute('select * from stocks where price > ? and symbol = ?', (50, 'IBM'))
这可以吗,还是您需要一个通用的解决方案


这可以吗,还是您需要一个通用的解决方案?

我试着运行您的代码,它对我来说很好。请发布您使用
>
尝试过的确切代码以及运行时得到的输出。这肯定是一个SQL问题,而不是Python问题吗?Duncan,我得到了这行代码:price=50 t=(price,)c.execute('select*from stocks where price>?,t)z4=c.fetchall()print z4谢谢我试着运行您的代码,它对我来说很好。请发布您使用
尝试过的确切代码以及运行时得到的输出。这肯定是一个SQL问题,而不是Python问题吗?Duncan,我得到了这行代码:price=50 t=(price,)c.execute('select*from stocks where price>?',t)z4=c.fetchall()打印z4谢谢第一行行不通:你需要一个元组而不是一个值。你的第一行和第二行对我来说仍然行不通。你的第三行做得很好。谢谢,好的。问题出在我这边。我得到了它。谢谢!第一行行不通:你需要一个元组而不是一个值。你的第一行和第二行对我来说仍然行不通。你的第三行做得很好。谢谢,好的。问题出在我这边。我得到了它。谢谢!
c.execute('select * from stocks where price > ?', (50,))

c.execute('select * from stocks where price between ? and ?', (40, 70))

c.execute('select * from stocks where price > ? and symbol = ?', (50, 'IBM'))