插入大量记录

时间:2015-02-01 23:01:02

标签: java android sqlite bulkinsert

我想使用SQLite构建一个表,其中包括全球4500个机场,我不知道如何插入它们。

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);
        }
    });
    }




}

错误日志: 错误:(18,20)错误:代码太大

问题是代码太大,因此无法编译。 如何有效地插入这些记录?

1 个答案:

答案 0 :(得分:3)

您不执行文本文件,读取它并将行插入数据库。您可以将其存储为CSV或JSON,因此您只需要查找(如果您还不知道):

  • 如何使用apk打包文本文件
  • 如何打开此类文件并逐行阅读

你似乎已经有了插入行的句柄,所以应该这样做。

如果您不喜欢jww建议在Android之外创建数据库并将其与应用程序打包,这就是全部。这种方法意味着您的应用程序在首次启动时不必花费时间加载数据库。如果你想这样做,也许他可以发布它作为答案,你可以接受它。