python - 写入数据库时​​,Python 3 中的 SQLite3 出现磁盘 I/O 错误

标签 python sqlite relational-database disk-io

我是一名刚开始使用 Python 的学生,我的任务是创建一个关系数据库管理系统。我想我已经走得很远了,但我似乎碰壁了。这是我的代码:

import csv
import sqlite3

conn = sqlite3.connect('unfccc.db')
c = conn.cursor()

c.execute('''CREATE TABLE unfccc (
        Country TEXT, 
        CodeCountryFormat TEXT, 
        NamePollutant TEXT, 
        NameYearSector TEXT, 
        NameParent TEXT, 
        Sector TEXT, 
        CodeSector TEXT, 
        CNUEDSPD TEXT
        )''')

def insert_row(Country, CodeCountryFormat, NamePollutant, NameYearSector, NameParent, Sector, CodeSector, CNUEDSPD):
    c.execute("INSERT INTO unfccc VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (Country, CodeCountryFormat, NamePollutant, NameYearSector, NameParent, Sector, CodeSector, CNUEDSPD))
    conn.commit()

with open('UNFCCC_v20.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter='\t')

    counter = 0

    for row in readCSV:
        insert_row(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7])
        counter = counter+1
        print('%d down, more to go' % counter)
conn.close()

当我使用第 4 行将输入定向到 :memory: 运行它时,它工作得很好,我自己拥有我认为是关系数据库的东西。

但是,当我尝试运行这样的代码,将数据写入数据库文件时,出现了这个错误:

 File "<ipython-input-13-4c50216842bc>", line 19, in insert_row
    c.execute("INSERT INTO unfccc VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (Country, CodeCountryFormat, NamePollutant, NameYearSector, NameParent, Sector, CodeSector, CNUEDSPD))

OperationalError: disk I/O error

我搜索过 stackoverflow,也使用过 google,但我认为我发现的任何案例都不符合我在这里尝试做的事情(或者我没有知识来计算出什么事了)。关于我的代码,我注意到的另一件事是它以超快的速度将数据输入内存,但是当我写入 db 文件时它真的很慢,这不应该是硬件限制,因为我使用的是 SSD。任何帮助将不胜感激!

最佳答案

在处理存储在 Google Drive 上的项目时,将备份/同步设置为在系统托盘图标上暂停将防止磁盘 i/o 错误。

这是因为当文件被写入或更改时,备份与同步会尝试将新版本上传到您的 Google 云端硬盘,同时它正在执行此操作;该文件变为“只读”文件。

当同步暂停时,您的 Google 云端硬盘文件夹更像是一个普通目录。

(点击 -> 设置 -> 暂停/恢复)

关于python - 写入数据库时​​,Python 3 中的 SQLite3 出现磁盘 I/O 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47540607/

相关文章:

xcode - 如何在 XCode 4 的 i386 应用程序的 dylib 中使用 Sqlite?这种方法确实适用于 iPhone 应用程序

mysql - 数据库表中的行限制

php - 数据库关系

Python 后台脚本

python - 允许 "pip install"忽略本地版本

python - 在 Python 中从 sqlite3 查询并提取 Unicode 值

MySQL创建一对多表,下面的最佳选择是什么?

python - 避免将 DLL 放入 CWD

python - 删除除列表中的所有单词

sql - SQL查询更新具有相同列名的多个表