In Android Studio, I need to create a SQLite table for a list of cities and population. It would have 1300 rows aprox. The list is in a 2 columns Excel file so I can copy it from there to my code.
I have a class to manage DB and a method to create the table, but I don´t know how to add the list of cities-population to the table.
This is the part of my code I have to create the table:
String crearTablaCities = "CREATE TABLE "+MI_TABLA_CITIES+" (ID_CITY INTEGER PRIMARY KEY AUTOINCREMENT, NAME_CITY TEXT, POPULATION_CITY INTEGER)";
db.execSQL(crearTablaCities);
I know how to add one row getting values from somewhere, but I don´t know how to add more than 1300 rows automatically so that I don´t have to write or modify more than 1300 lines by hand.
public void insertCity(ObjectCity newCity){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
//cv.put("ID_CITY", newCity.getCityID());
cv.put("NAME_CITY", newCity.getCityName());
cv.put("POPULATION_CITY", newCity.getCityPopulation());
db.insert(MI_TABLA_CITIES,null,cv);
//db.close();
}
I don´t know how to face the next step. Can someone please give me some hint to find the way to do it? Thanks a lot in advance
2
Answers
There are 3 relatively simple ways that you can accomplish this.
a) Create a formula that generates the code on your behalf and then copy and paste the generated code to a suitable place.
b) Save the data as a csv and copy the file as an asset you can then open and read the file extracting the data and inserting it perhaps in the onCreate method of the database helper.
c) Save the data as a csv and use an SQLite tool to import and load the data and to then copy that database as an asset and then use that database.
Option A is a very simple, assuming that you have something like :-
And your code shows that you have an
insertCity
method that takes aCity
object. Assuming that you construct a City usingCity("Tokyo",37339804)
then to insert a single city, your code could be something like :-Then you could enter a formula in cell C1 as
="insertCity(new ObjectCity("&CHAR(34)&A1&CHAR(34)&","&B1&"L));"
. You can then copy this cell to c2-c1300 (c2-c17 using the spreadsheet above). The resultant Speadsheet would be like:-You can then just drop copy and paste the generated code (cells c1-c1300) into a suitable place.
Here’s a working example:-
The ObjectCity class :-
The DatabaseHelper DBHelper class (based upon the code in the question) :-
Note the
loadData
method.Last an invoking activity MainActivity :-
Result
Running the above and using Android Studio’s Database Inspector reveals:-
Following on from the first answer re:-
Then a csv of the raw sheet was saved using Excel. This was imported into the SQLite Tool Navicat for SQLite (there are other such tools but I prefer Navicat):-
The database and connection were closed (saves the database).
In Android studio, the App was right clicked in Android View and New then Directory was selected(clicked) and from the dialog src/Android/assets was selected (aka created the assets folder in app/src/main (can be done outside of Android Studio)).
The database file (named thedatabase.db) was copied into the newly created assets folder.
The Database Helper class DBHelperB was written as per :-
MainActivity was then amended to be :-
The result, after running, as per the log includes :-