python - 这段代码在数据库中只写了一行?

标签 python sqlite

这段代码只在数据库中写入一行

我发现这段代码没有错误。 . .

但为什么插入的内容不超过第一行?

def transremovechars():
    cur.execute('drop table transforms')
    char_cfg = config.get('Transform_Variables', 'Chars_to_be_removed')      #Reads all the special chars to be removed from specialchars.txt#
    cur.execute('select * from originallist')
    for row in cur:                                                         #Applies transformation to remove chars for each row in a loop#
        company = row[0]
        for specialchars in char_cfg:
            company =  company.replace(specialchars, '')
        cur.execute('Insert into transforms (Transresult1) values (\'' + company + '\')')
    con.commit() 

最佳答案

你忘记了 cur.fetchall():

def transremovechars():
    cur.execute('drop table transforms')
    char_cfg = config.get('Transform_Variables', 'Chars_to_be_removed')      #Reads all the special chars to be removed from specialchars.txt#
    cur.execute('select * from originallist')
    for row in cur.fetchall():                                                         #Applies transformation to remove chars for each row in a loop#
        company = row[0]
        for specialchars in char_cfg:
            company =  company.replace(specialchars, '')
        cur.execute('Insert into transforms (Transresult1) values (\'' + company + '\')')
    con.commit() 

关于python - 这段代码在数据库中只写了一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9261242/

相关文章:

Python 不清楚的正则表达式行为

android - 在 sqlite Android 中使用单个查询创建(如果表不存在)和插入表

sqlite - 如何在 SQLite 中获取外键约束的名称?

android - 使用游标避免 ANR

c# - 使用字节 [] 的 Nhibernate 3.2 sqlite blob 字段通过代码映射不起作用

java - 安卓 : Error in getting URI of an image

python - ScraperWiki 数据存储区需要什么编码?

python - 对 Pandas 数据集执行 SQL 查询

python - 计算列表项并存储在列表项对应的数据框列中

PYTHON:查找嵌套列表的平均值