gpt4 book ai didi

java - 插入大量记录

转载 作者:行者123 更新时间:2023-11-29 05:11:27 25 4
gpt4 key购买 nike

<分区>

我想使用 SQLite 构建一个包含全局 4500 个机场的表,但我不确定如何插入它们。

机场.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;
}

}

数据库处理器.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();
}


}

主 Activity .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);
}
});
}




}

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

问题出在代码上,太大了,无法编译。我怎样才能有效地插入这些记录?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com