Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I understanding how to import my .csv file into sqlite?

I currently have a .csv file with several unlabeled columns of data, which to my knowledge translate to the following datatypes in sqlite:

  • datetime (in the format 7/19/2011 12:00:00 PM) -> numeric
  • double -> real
  • char(1) -> text
  • float -> real

I can create the database by doing the following:

sqlite> create table myTable (myVar1 numeric, myVar2 real, myVar3 text, myVar4 real);
sqlite> .separator ","
sqlite> .import myFile.csv myTable

Then I copy and paste the newly created myTable.db into the "assets" folder in my project in eclipse. I make a DatabaseHelper class that extends SQLiteOpenHelper, and then I can start using and reading from the database in my Android project.

Am I getting this right? I've never used a database before and I've seen so many vastly different instructions on doing this. Some of my questions are-- do I have to label the columns of my .csv file? Is my .csv file not "simple" enough to just use .import and I'll need to find a program to translate it? I've come across sites saying that I need to rename something (which I don't seem to have) to "_id", and I don't know what this is, where this is, or how to do this, or if it's even necessary, or what it's for. What else am I missing?

like image 439
Kalina Avatar asked Jan 18 '26 20:01

Kalina


1 Answers

I think you are getting it "right" except for that first datetime column. You should use the TEXT type, not a numeric type.

Also, you can inspect your data after the import to see if all is well, especially with that datetime field:

SELECT * FROM myTable ORDER BY RANDOM() LIMIT 10;

UPDATE

In response to the OP's last comment: my understanding of how you store date(time) is that it depends on your context. So if the date in the flat file is in the format "7/19/2011 12:00:00 PM", then without any transformation it'll be imported as TEXT anyway.