在python中仅从sqlite调用1个单元格中的数据
我的问题基本上是双重的。首先,我有一个使用SQlite的数据库,我用Python运行它 我的数据库是:在python中仅从sqlite调用1个单元格中的数据,python,sql,sqlite,Python,Sql,Sqlite,我的问题基本上是双重的。首先,我有一个使用SQlite的数据库,我用Python运行它 我的数据库是: CREATE TABLE cards (id integer primary key autoincrement not null, ref text unique check(ref!=''), name text, description text, quantity integer default 0, cat1 text, cat2 text); INSERT INTO "cards
CREATE TABLE cards (id integer primary key autoincrement not null, ref text unique
check(ref!=''), name text, description text, quantity integer default 0, cat1 text,
cat2 text);
INSERT INTO "cards" VALUES(1,'lx247','green door',NULL,20,'abstract','');
INSERT INTO "cards" VALUES(2,'lxx247','green door',NULL,20,'abstract','');
INSERT INTO "cards" VALUES(3,'lxx2f47','green door',NULL,20,'abstract','');
我正在运行以下python代码:
import sqlite3 as lite
import sys
con = lite.connect('cards.db')
idn = raw_input("Enter your ID number")
with con:
cur=con.cursor()
cur.execute("SELECT quantity FROM cards WHERE ref=?", idn)
print "Quantity" + str(cur.fetchone())
首先,当我执行此脚本,并在提示符中输入'lx247'时,我得到以下错误消息:
Enter your ID numberlx247
Traceback (most recent call last):
File "test1.py", line 7, in <module>
cur.execute("SELECT quantity FROM cards WHERE ref=?", idn)
sqlite3.ProgrammingError: Incorrect number of bindings supplied.
The current statement uses 1, and there are 5 supplied.
到
并在提示下输入'2',返回:
Enter your ID number2
Quantity(20,)
因此,这告诉我,它处理idn参数以查找ref列的方式有问题。所以我的第一个问题是如何让它查找我在列ref的idn中输入的值
第二个问题是如何让它以数字的形式输出值,而不是以(20,)的形式输出值,因为我想稍后指示python向其添加或减去另一个数字x,并将数据库中的条目更新为20+x,因此,我希望在python中创建一个变量,该变量是某个ref条目的quantity列的值。您需要传入一系列参数,使其成为一个包含一个元素的元组:
cur.execute("SELECT quantity FROM cards WHERE ref=?", (idn,))
Python字符串也是序列,因此如果idn
是5个字符的字符串,Python会看到长度为5的序列
数据库总是返回列的行,即使结果只有一列。行本质上是一个Python元组,一个序列。这与idn
参数的问题相同,但相反。通过索引从结果行中取出一列:
print "Quantity " + str(cur.fetchone()[0])
示例数据库的快速演示:
>>> import sqlite3
>>> con = sqlite3.connect(':memory:')
>>> con.executescript('''\
... CREATE TABLE cards (id integer primary key autoincrement not null, ref text unique
... check(ref!=''), name text, description text, quantity integer default 0, cat1 text,
... cat2 text);
...
... INSERT INTO "cards" VALUES(1,'lx247','green door',NULL,20,'abstract','');
... INSERT INTO "cards" VALUES(2,'lxx247','green door',NULL,20,'abstract','');
... INSERT INTO "cards" VALUES(3,'lxx2f47','green door',NULL,20,'abstract','');
... ''')
<sqlite3.Cursor object at 0x10ad66ab0>
>>> idn = 'lx247'
>>> cur=con.cursor()
>>> cur.execute("SELECT quantity FROM cards WHERE ref=?", (idn,))
<sqlite3.Cursor object at 0x10ad66b20>
>>> print "Quantity " + str(cur.fetchone()[0])
Quantity 20
导入sqlite3
>>>con=sqlite3.connect(“:内存:”)
>>>con.executescript(“”)\
…创建表卡(id整数主键自动递增不为空,参考文本唯一
…检查(ref!='')、名称文本、说明文本、数量整数默认值0、cat1文本、,
…第二类文本);
...
…在“卡片”值中插入(1,'lx247','green door',NULL,20,'abstract','';
…插入“卡片”值(2,'lxx247','green door',NULL,20,'abstract','';
…插入“卡片”值(3,'lxx2f47','green door',NULL,20,'abstract','';
... ''')
>>>idn='lx247'
>>>cur=con.cursor()
>>>当前执行(“从ref=?”的卡片中选择数量”(idn)
>>>打印“数量”+str(cur.fetchone()[0])
数量20
哇,回答得真快!谢谢你,它真的很有魅力。
print "Quantity " + str(cur.fetchone()[0])
>>> import sqlite3
>>> con = sqlite3.connect(':memory:')
>>> con.executescript('''\
... CREATE TABLE cards (id integer primary key autoincrement not null, ref text unique
... check(ref!=''), name text, description text, quantity integer default 0, cat1 text,
... cat2 text);
...
... INSERT INTO "cards" VALUES(1,'lx247','green door',NULL,20,'abstract','');
... INSERT INTO "cards" VALUES(2,'lxx247','green door',NULL,20,'abstract','');
... INSERT INTO "cards" VALUES(3,'lxx2f47','green door',NULL,20,'abstract','');
... ''')
<sqlite3.Cursor object at 0x10ad66ab0>
>>> idn = 'lx247'
>>> cur=con.cursor()
>>> cur.execute("SELECT quantity FROM cards WHERE ref=?", (idn,))
<sqlite3.Cursor object at 0x10ad66b20>
>>> print "Quantity " + str(cur.fetchone()[0])
Quantity 20