1

I'd like to build a table using SQLite which includes 4500 airports around the world and I'm not sure how can I insert them.

Airport.java

public class Airport {

int id;
String name, icao, iata, country, city, countryCode;

public Airport() {

}

public Airport(int id,String name,String icao,String iata, String country, String city, String countryCode)
{
    this.id = id;
    this.name = name;
    this.icao = icao;
    this.iata = iata;
    this.country = country;
    this.city = city;
    this.countryCode = countryCode;
}

public Airport(String name,String icao,String iata, String country, String city, String countryCode)
{
    this.name = name;
    this.icao = icao;
    this.iata = iata;
    this.country = country;
    this.city = city;
    this.countryCode = countryCode;
}

public String getName() {
    return this.name;
}

public String getICAO()
{
    return this.icao;
}

public String getIATA()
{
    return this.iata;
}

public String getCountry()
{
    return this.country;
}

public String getCity()
{
    return this.city;
}

public String getCountryCode()
{
    return this.countryCode;
}

}

DatabaseHandler.java

public class DatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 4;

private static final String DATABASE_NAME = "airportsDB";

private static final String TABLE_AIRPORTS = "airports";

private static final String KEY_ID = "id";
private static final String KEY_NAME = "airportname";
private static final String KEY_ICAO = "icao";
private static final String KEY_IATA = "iata";
private static final String KEY_COUNTRY = "country";
private static final String KEY_CITY = "city";
private static final String KEY_COUNTRYCODE = "countrycode";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
        String CREATE_AIRPORTS_TABLE = "CREATE TABLE " + TABLE_AIRPORTS+" ("
                +KEY_ID+" INTEGER PRIMARY KEY, "+KEY_NAME+" TEXT, "+KEY_ICAO+
                " TEXT, "+KEY_IATA+" TEXT, "+KEY_COUNTRY+" TEXT, "+KEY_CITY+
                " TEXT, "+KEY_COUNTRYCODE+" TEXT"+")";
        db.execSQL(CREATE_AIRPORTS_TABLE);


 }


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_AIRPORTS);

    // Create tables again
    onCreate(db);
}

public void addAirport(Airport airport){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, airport.getName());
    values.put(KEY_ICAO, airport.getICAO());
    values.put(KEY_IATA, airport.getIATA());
    values.put(KEY_COUNTRY, airport.getCountry());
    values.put(KEY_CITY, airport.getCity());
    values.put(KEY_COUNTRYCODE,airport.getCountryCode());

    db.insert(TABLE_AIRPORTS,null,values);
    db.close();
}


}

MainActivity.js

public class MainActivity extends Activity {


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);

    DatabaseHandler db = new DatabaseHandler(this);
    Log.d("INSERT: ", "Inserting...");
    db.addAirport(new Airport("Alfonso B. Aragon Airport","SKCL","CLO","Colombia","Cali","CO"));
    db.addAirport(new Airport("Kangding Airport","ZUKD","KGT","China","Kangding","CN"));
    db.addAirport(new Airport("Ramechhap Airport","VNRC","RHP","Nepal","Ramechhap","NP"));
    db.addAirport(new Airport("Bajura Airport","VNBR","BJU","Nepal","Bajura","NP"));
    db.addAirport(new Airport("Budapest Ferenc Liszt International Airport","LHBP","BUD","Hungary","Budapest","HU"));
    db.addAirport(new Airport("Awantipur","VIAW","AWT","India","Awantipur","IN"));
    db.addAirport(new Airport("Malvinas Argentinas International Airport","SAWH","USH","Argentina","Ushuaia","AR"));
    db.addAirport(new Airport("Rotterdam The Hague Airport","EHRD","RTM","Netherlands","Rotterdam","NL"));
    db.addAirport(new Airport("Rochester International Airport","KRST","RST","United States","Rochester","US"));
    db.addAirport(new Airport("Hatay Airport","LTDA","HTY","Turkey","Antakya","TR"));
    db.addAirport(new Airport("Alma Airport","CYTF","YTF","Canada","Alma","CA"));
    db.addAirport(new Airport("Karpathos Airport","LGKP","AOK","Greece","Karpathos","GR"));
    db.addAirport(new Airport("Zona da Mata Regional Airport","SBZM","IZA","Brazil","Juiz de Fora","BR"));
    db.addAirport(new Airport("Blue Mountain Airport","","VBM","United States","Blue Mountain","US"));
    db.addAirport(new Airport("Bogorodskoye Airport","UHNB","BQG","Russian Federation","Bogorodskoye","RU"));
    db.addAirport(new Airport("Elista Airport","URWI","ESL","Russian Federation","Elista","RU"));
    db.addAirport(new Airport("Vorkuta Airport","UUYW","VKT","Russian Federation","Vorkuta","RU"));
...
...
...
...
(4500 X db.addAirport...)
Button send = (Button)findViewById(R.id.sendButton);
        final EditText airportText = (EditText)findViewById(R.id.airport);
        final Switch status = (Switch)findViewById(R.id.statusSwitch);

        send.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                i.putExtra("AIRPORT", airportText.getText().toString());
                boolean isChecked = status.isChecked();

                if(isChecked)
                    i.putExtra("STATUS", "arr");

                 else
                    i.putExtra("STATUS", "dep");

                startActivity(i);
        }
    });
    }




}

Errorlog: Error:(18, 20) error: code too large

The problem is the code, which is too large, so it can't be compiled. How can I insert those records effectively ?

Avraham
  • 537
  • 7
  • 23
  • 1
    *"The problem is the code, which is too large, so it can't be compiled..."* - please provide the exact error message you are encountering. Otherwise, just make the code smaller by: (1) prebuilding the database offline and including it as a resource; or (2) by providing the airport information in a text file as a resource and populating it at start-up. – jww Feb 01 '15 at 23:05
  • I have a text file which includes all the JAVA lines code (4500 db.AddAirport() lines) Is there any way to execute those codes from a text file ? – Avraham Feb 01 '15 at 23:10

1 Answers1

3

You don't execute the text file, you read it and insert the rows into the database. You can store it as CSV or JSON, so you just need to look up (if you don't already know):

  • How to package a text file with your apk
  • How to open such a file and read it line by line

You seem to have a handle on inserting rows already, so that should do it.

This is all if you don't like jww's suggestion of creating the database outside of Android and packaging it with the app. That approach would mean your app doesn't have to spend time loading the db when it first starts. If you want to do it that way, maybe he can post it as an answer so you can accept it.

nasch
  • 5,330
  • 6
  • 31
  • 52