将字典中的值插入sqlite数据库
我无法解决这个问题。
我想将字典的值插入sqlite数据库。
I cannot get my head around it. I want to insert the values of a dictionary into a sqlite databse.
url = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=5f...1b&per_page=250&accuracy=1&has_geo=1&extras=geo,tags,views,description"
soup = BeautifulSoup(urlopen(url)) #soup it up
for data in soup.find_all('photo'): #parsing the data
dict = { #filter the data, find_all creats dictionary KEY:VALUE
"id_p": data.get('id'),
"title_p": data.get('title'),
"tags_p": data.get('tags'),
"latitude_p": data.get('latitude'),
"longitude_p": data.get('longitude'),
}
#print (dict)
connector.execute("insert into DATAGERMANY values (?,?,?,?,?)", );
connector.commit()
connector.close
我的键是 id_p
, title_p
等,以及我通过 data.get检索的值
。
My keys are id_p
, title_p
etc. and the values I retrieve through data.get
.
但是,我无法插入它们。
当我尝试在后面写
我得到 id,标题,标签,纬度,经度
... DATAGERMANY值(?,?,?, ?,?),); NameError:未定义名称'title'
。
However, I cannot insert them.
When I try to write id, title, tags, latitude, longitude
behind ...DATAGERMANY values (?,?,?,?,?)", );
I get
NameError: name 'title' is not defined
.
我用 dict.values
和 dict
尝试了一下,但后来却说了表DATAGERMANY有6列,但提供了5个值
。
I tried it with dict.values
and dict
but then its saying table DATAGERMANY has 6 columns but 5 values were supplied
.
添加另一个?
给我错误(带有dict.values):ValueError:参数的类型不受支持
Adding another ?
gives me the error (with `dict.values): ValueError: parameters are of unsupported type
这是我创建数据库和表的方式。
This is how I created the db and table.
#creating SQLite Database and Table
connector = sqlite3.connect("GERMANY.db") #create Database and Table, check if NOT NULL is a good idea
connector.execute('''CREATE TABLE DATAGERMANY
(id_db INTEGER PRIMARY KEY AUTOINCREMENT,
id_photo INTEGER NOT NULL,
title TEXT,
tags TEXT,
latitude NUMERIC NOT NULL,
longitude NUMERIC NOT NULL);''')
即使没有值
填充到数据库中……这也可能发生。
The method should work even if there is no value
to fill in into the database... That can happen as well.
按照书面规定,您的 connector.execute()
语句缺少 parameters
参数。
As written, your connector.execute()
statement is missing the parameters
argument.
应该这样使用:
connector.execute("insert into some_time values (?, ?)", ["question_mark_1", "question_mark_2"])
除非您需要以下字典稍后,我实际上会改用列表或元组:
Unless you need the dictionary for later, I would actually use a list or tuple instead:
row = [
data.get('id'),
data.get('title'),
data.get('tags'),
data.get('latitude'),
data.get('longitude'),
]
然后您的插入语句变为:
Then your insert statement becomes:
connector.execute("insert into DATAGERMANY values (NULL,?,?,?,?,?)", *row)
为什么要进行这些更改?
Why these changes?
-
值中的
是这样,因此自动递增主键将起作用NULL
(NULL,...) - 列表而不是字典,因为顺序很重要,而字典不重要t保留订单
-
*行
,因此五元素row
变量将进行扩展(有关详细信息,请参见此处 )。 - 最后,您不应该使用
dict
作为变量名,因为这是Python中的内置变量。 / li>
- The
NULL
in thevalues (NULL, ...)
is so the auto-incrementing primary key will work - The list instead of the dictionary because order is important, and dictionaries don't preserve order
- The
*row
so the five-elementrow
variable will be expanded (see here for details). - Lastly, you shouldn't use
dict
as a variable name, since that's a built-in variable in Python.