当前位置:  开发笔记 > 编程语言 > 正文

使用Python将CSV文件导入sqlite3数据库表

如何解决《使用Python将CSV文件导入sqlite3数据库表》经验,为你挑选了6个好方法。

我有一个CSV文件,我想使用Python将此文件批量导入我的sqlite3数据库.命令是".import .....".但它似乎无法像这样工作.谁能给我一个如何在sqlite3中做到这一点的例子?我正在使用Windows以防万一.谢谢



1> bernie..:
import csv, sqlite3

con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("CREATE TABLE t (col1, col2);") # use your column names here

with open('data.csv','rb') as fin: # `with` statement available in 2.5+
    # csv.DictReader uses first line in file for column headings by default
    dr = csv.DictReader(fin) # comma is default delimiter
    to_db = [(i['col1'], i['col2']) for i in dr]

cur.executemany("INSERT INTO t (col1, col2) VALUES (?, ?);", to_db)
con.commit()
con.close()


如果你遇到了同样的问题:确保将col1和col2更改为csv文件中的列标题.并通过最后调用con.close()来关闭与数据库的连接.

2> Tennessee Le..:

创建一个sqlite连接到磁盘上的文件是留给读者的练习...但现在有一个双线程由熊猫库实现

df = pandas.read_csv(csvfile)
df.to_sql(table_name, conn, if_exists='append', index=False)


使用sep =';'.熊猫文档清楚地概述了如何处理这个问题.
有没有办法使用熊猫,但没有使用RAM?,我有一个巨大的.csv(7gb)我无法导入数据帧,然后附加到数据库.

3> Marcelo Cant..:

.import命令是sqlite3命令行工具的一项功能.要在Python中执行此操作,您只需使用Python具有的任何功能(例如csv模块)加载数据,并按常规方式插入数据.

这样,您还可以控制插入的类型,而不是依赖于sqlite3看似无证的行为.


@Marcelo:实际上它是在Python sqlite3包装器模块中完成的.http://docs.python.org/library/sqlite3.html#module-functions-and-constants说"""sqlite3模块内部使用语句缓存来避免SQL解析开销.如果要显式设置数量为连接缓存的语句,您可以设置cached_statements参数.当前实现的默认值是缓存100个语句."""

4> Guy L..:

我的2美分(更通用):

import csv, sqlite3
import logging

def _get_col_datatypes(fin):
    dr = csv.DictReader(fin) # comma is default delimiter
    fieldTypes = {}
    for entry in dr:
        feildslLeft = [f for f in dr.fieldnames if f not in fieldTypes.keys()]
        if not feildslLeft: break # We're done
        for field in feildslLeft:
            data = entry[field]

            # Need data to decide
            if len(data) == 0:
                continue

            if data.isdigit():
                fieldTypes[field] = "INTEGER"
            else:
                fieldTypes[field] = "TEXT"
        # TODO: Currently there's no support for DATE in sqllite

    if len(feildslLeft) > 0:
        raise Exception("Failed to find all the columns data types - Maybe some are empty?")

    return fieldTypes


def escapingGenerator(f):
    for line in f:
        yield line.encode("ascii", "xmlcharrefreplace").decode("ascii")


def csvToDb(csvFile, outputToFile = False):
    # TODO: implement output to file

    with open(csvFile,mode='r', encoding="ISO-8859-1") as fin:
        dt = _get_col_datatypes(fin)

        fin.seek(0)

        reader = csv.DictReader(fin)

        # Keep the order of the columns name just as in the CSV
        fields = reader.fieldnames
        cols = []

        # Set field and type
        for f in fields:
            cols.append("%s %s" % (f, dt[f]))

        # Generate create table statement:
        stmt = "CREATE TABLE ads (%s)" % ",".join(cols)

        con = sqlite3.connect(":memory:")
        cur = con.cursor()
        cur.execute(stmt)

        fin.seek(0)


        reader = csv.reader(escapingGenerator(fin))

        # Generate insert statement:
        stmt = "INSERT INTO ads VALUES(%s);" % ','.join('?' * len(cols))

        cur.executemany(stmt, reader)
        con.commit()

    return con



5> jiy..:

非常感谢bernie的回答!不得不稍微调整一下 - 这对我有用:

import csv, sqlite3
conn = sqlite3.connect("pcfc.sl3")
curs = conn.cursor()
curs.execute("CREATE TABLE PCFC (id INTEGER PRIMARY KEY, type INTEGER, term TEXT, definition TEXT);")
reader = csv.reader(open('PC.txt', 'r'), delimiter='|')
for row in reader:
    to_db = [unicode(row[0], "utf8"), unicode(row[1], "utf8"), unicode(row[2], "utf8")]
    curs.execute("INSERT INTO PCFC (type, term, definition) VALUES (?, ?, ?);", to_db)
conn.commit()

我的文本文件(PC.txt)如下所示:

1 | Term 1 | Definition 1
2 | Term 2 | Definition 2
3 | Term 3 | Definition 3



6> 小智..:
#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys, csv, sqlite3

def main():
    con = sqlite3.connect(sys.argv[1]) # database file input
    cur = con.cursor()
    cur.executescript("""
        DROP TABLE IF EXISTS t;
        CREATE TABLE t (COL1 TEXT, COL2 TEXT);
        """) # checks to see if table exists and makes a fresh table.

    with open(sys.argv[2], "rb") as f: # CSV file input
        reader = csv.reader(f, delimiter=',') # no header information with delimiter
        for row in reader:
            to_db = [unicode(row[0], "utf8"), unicode(row[1], "utf8")] # Appends data from CSV file representing and handling of text
            cur.execute("INSERT INTO neto (COL1, COL2) VALUES(?, ?);", to_db)
            con.commit()
    con.close() # closes connection to database

if __name__=='__main__':
    main()

推荐阅读
帆侮听我悄悄说星星
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有