将字典中的值插入sqlite数据库 [英] Insert Values from dictionary into sqlite database

查看:291
本文介绍了将字典中的值插入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 valueto 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 the values (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-element row 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.

这篇关于将字典中的值插入sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆