I have a CSV file and I want to bulk-import this file into my sqlite3 database using Python. the command is ".import .....". but it seems that it cannot work like this. Can anyone give me an example of how to do it in sqlite3? I am using windows just in case. Thanks
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You can do this using
blaze
&odo
efficientlyOdo will store the csv file to
data.db
(sqlite database) under the schemadata
Or you use
odo
directly, withoutblaze
. Either ways is fine. Read this documentationThe
.import
command is a feature of the sqlite3 command-line tool. To do it in Python, you should simply load the data using whatever facilities Python has, such as the csv module, and inserting the data as per usual.This way, you also have control over what types are inserted, rather than relying on sqlite3's seemingly undocumented behaviour.
My 2 cents (more generic):
Creating an sqlite connection to a file on disk is left as an exercise for the reader ... but there is now a two-liner made possible by the pandas library
Many thanks for bernie's answer! Had to tweak it a bit - here's what worked for me:
My text file (PC.txt) looks like this: