简体   繁体   English

用 python 中的链接填充 sqlite3 db

[英]populating sqlite3 db with links in python

I'm trying to populate a database with a single column with a list of strings (links).我正在尝试使用带有字符串列表(链接)的单列填充数据库。 I scraped the list and I must modify every single link before sending it to the database.我刮掉了列表,在将其发送到数据库之前,我必须修改每个链接。 This is the code:这是代码:

for event in events:
    link_url = "https://www.website.com"+event+"#all"
    c.execute("INSERT INTO table (links) VALUES(?)", link_url)

I can get it working if I modify the variables and send a tuple, like this:如果我修改变量并发送一个元组,我可以让它工作,如下所示:

for event in events:
    link_url = "https://www.website.com"+event+"#all"
    link = (link_url,)
    c.execute("INSERT INTO seriea (links) VALUES(?)", link_url)

but I don't want to use this solution since I want to get a list of strings back out later:但我不想使用这个解决方案,因为我想稍后得到一个字符串列表:

c = connection.execute('select links from table')
list_of_urls = c.fetchall()

But this gives me a list of tuples.但这给了我一个元组列表。

This is the error I have: ProgrammingError: Incorrect number of bindings supplied.这是我遇到的错误: ProgrammingError: 提供的绑定数量不正确。 The current statement uses 1, and there are 80 supplied.当前语句使用 1,提供了 80 个。

I think that's because the string characters are counted (actually more but I noticed that the number before "supplied" changes with the link fed)我认为这是因为字符串字符被计算在内(实际上更多,但我注意到“提供”之前的数字随着链接的变化而变化)

I don't want to use this solution since I want to get a list of strings back out later:我不想使用此解决方案,因为我想稍后获取字符串列表:

 c = connection.execute('select links from table') list_of_urls = c.fetchall()

But this gives me a list of tuples.但这给了我一个元组列表。

The list of tuples you're getting when you do a select have nothing to do with the way you insert data.执行 select 时获得的元组列表与插入数据的方式无关。 Remember, tables have two dimensions:请记住,表格有两个维度:

id ID links链接 something某物 else别的
1 1 "foo" “富” "bar" “酒吧” "baz" “巴兹”
2 2 "quux" “曲子” "herp" “疱疹” "derp" “德普”

When you do a select you get a list that corresponds to the rows here.当您执行 select 时,您会得到一个与此处的行相对应的列表。 But each row has multiple fields: id , links , something , and else .但每一行都有多个字段: idlinkssomethingelse Each tuple in the list contains the values for each of the fields in the table.列表中的每个元组都包含表中每个字段的值。

If you just want the URLs as a list of strings you can use a list comprehension or similar:如果您只想将 URL 作为字符串列表,则可以使用列表推导或类似方法:

c = connection.execute('select links from table')
list_of_rows = c.fetchall()
list_of_strings = [row[0] for row in list_of_rows]
#                      ^ index of first element in
#                  ^^^ the tuple of values for each row

Note that you do have to provide a tuple or other sequence when you insert the data:请注意,插入数据时必须提供元组或其他序列:

For the qmark style, parameters must be a sequence .对于 qmark 样式, parameters必须是一个序列 For the named style, it can be either a sequence or dict instance.对于命名样式,它可以是序列字典实例。 The length of the sequence must match the number of placeholders, or a ProgrammingError is raised.序列的长度必须与占位符的数量匹配,否则会引发ProgrammingError If a dict is given, it must contain keys for all named parameters.如果给定一个dict ,它必须包含所有命名参数的键。

You might be thinking of the tuple part of it the wrong way.您可能以错误的方式思考它的元组部分。 You don't need to pass in a tuple of URLs, you need to pass in a tuple of parameters .您不需要传入 URL 元组,您需要传入参数元组。 You're not saying "the links column should contain this tuple" but rather "this tuple contains enough values to fill in the placeholders in this query".你不是说“链接列应该包含这个元组”,而是“这个元组包含足够的值来填充这个查询中的占位符”。

I'd rewrite that like so:我会这样重写:

for event in events:
    link_url = "https://www.website.com"+event+"#all"
    c.execute("INSERT INTO seriea (links) VALUES(?)", (link_url,))

This is so you can have multiple parameters, eg这样您就可以有多个参数,例如

c.execute(
    "INSERT INTO seriea (links, some, other) VALUES(?, ?, ?)",
    (link_url, foo, bar),
)

The current statement uses 1, and there are 80 supplied.当前语句使用 1,提供了 80 个。

I think that's because the string characters are counted我认为那是因为字符串字符被计算在内

Yes, that's most likely what's happening.是的,这很可能是正在发生的事情。 c.execute() expects to receive a sequence, and strings are a sequence of characters. c.execute()期望接收一个序列,字符串是一个字符序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM