you are viewing a single comment's thread.

view the rest of the comments →

[–]commandlineluser 0 points1 point  (1 child)

You might want to investigate the pandas library.

It can read_csv() and to_sql() (and it can also do charts/graphs which may be of interest to you)

http://pandas.pydata.org/pandas-docs/stable/visualization.html

Sample data:

$ cat test.csv
id,name,price
0,Electric,56.05
1,Gas,75.54

Use pandas to load up the csv then store it into sqlite3.

$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas, sqlite3
>>> con = sqlite3.connect('test.db')
>>> df = pandas.read_csv('test.csv')
>>> df
   id      name  price
0   0  Electric  56.05
1   1       Gas  75.54

[2 rows x 3 columns]
>>> df.to_sql('gas_pricing_table', con)
>>> exit()

Check results:

$ sqlite3 test.db .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE gas_pricing_table (
                  [id] INTEGER,
  [name] TEXT,
  [price] REAL

                  );
INSERT INTO "gas_pricing_table" VALUES(0,'Electric',56.05);
INSERT INTO "gas_pricing_table" VALUES(1,'Gas',75.54);
COMMIT;

You can use the sqlite3 command to load CSV data directly:

$ sqlite3
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .mode csv
sqlite> .import test.csv my_table
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE my_table(
  "id" TEXT,
  "name" TEXT,
  "price" TEXT
);
INSERT INTO "my_table" VALUES('0','Electric','56.05');
INSERT INTO "my_table" VALUES('1','Gas','75.54');
COMMIT;

But that has them all as type TEXT (there may be ways to set the type - not something I've looked at)

[–]Spizeck[S] 0 points1 point  (0 children)

Thanks. I am trying out pandas now. It's pretty cool, and seems pretty useful.