DatabaseHelperClass. java
Это вспомогательный класс базы данных для создания базы данных в android. Но это не работает, как я вижу в android logcat. Невозможно указать столбец местоположения в базе данных. Пожалуйста, сообщите, если я допустил ошибку в следующих классах.
public class DatabaseHelperClass extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "metrolines.db";
public DatabaseHelperClass(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
/* CREATE CLASS */
db.execSQL(Station.CREATE_TABLE);
Log.d("Create Table", "Passed this step");
Insert_Station(new Station("Uttam Nagar East","West Delhi"));
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
/* DROP TABLE IF EXIST */
db.execSQL("DROP TABLE IF EXISTS " + Station.TABLE_NAME);
/* CREATE TABLES AGAIN */
onCreate(db);
}
public void Insert_Station(Station station) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Station.STATION_NAME, station.getD_Station());
contentValues.put(Station.STATION_LOCATION, station.getD_Location());
db.insert(Station.TABLE_NAME, null, contentValues);
db.close();
}
public List<Station> getStationList() {
List<Station> stationList = new ArrayList<Station>();
//StationList
String SELECT_QUERY = "SELECT * FROM " + Station.TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(SELECT_QUERY, null);
if (cursor.moveToFirst()) {
do {
Station station = new Station();
station.setD_Station(cursor.getString(1));
station.setD_Location(cursor.getString(2));
stationList.add(station);
} while (cursor.moveToNext());
}
return stationList;
}}
Станция. java
public class Station {
public static final String TABLE_NAME = "STATION_TABLE";
public static final String STATION_NAME = "Station_name" ;
public static final String STATION_LOCATION = "Location_name" ;
private String D_Station;
private String D_Location;
//create table SQL Query
public static final String CREATE_TABLE =
"CREATE TABLE STATION_TABLE (ID INTEGER PRIMARY KEY AUTOINCREMENT, Station_name TEXT, Location_name TEXT)";
public Station(){
}
public Station(String d_Station, String d_Location) {
D_Station = d_Station;
D_Location = d_Location;
}
public String getD_Station() {
return D_Station;
}
public void setD_Station(String d_Station) {
D_Station = d_Station;
}
public String getD_Location() {
return D_Location;
}
public void setD_Location(String d_Location) {
D_Location = d_Location;
}
}
MainActiviy. java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHelperClass db = new DatabaseHelperClass(this);
Log.d("Insert", "Inserting");
db.Insert_Station(new Station("Uttam Nagar East", "West Delhi"));
db.Insert_Station(new Station("Dwarka Sec 9", "West Delhi"));
db.Insert_Station(new Station("Rajiv Chowk", "Central Delhi"));
db.Insert_Station(new Station("Hauz Khas", "South Delhi"));
Logcat
2020-03-20 09:51:54.330 29987-29987/com.gauravbhagat.meg D/Insert: Inserting
2020-03-20 09:51:54.389 29987-29987/com.gauravbhagat.meg E/SQLiteLog: (1) table STATION_TABLE has no column named Location_name
2020-03-20 09:51:54.436 29987-29987/com.gauravbhagat.meg E/SQLiteDatabase: Error inserting Location_name=West Delhi Station_name=Uttam Nagar East
android.database.sqlite.SQLiteException: table STATION_TABLE has no column named Location_name (code 1 SQLITE_ERROR[1]): , while compiling: INSERT INTO STATION_TABLE(Location_name,Station_name) VALUES (?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1229)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:703)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:2019)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1890)
at com.gauravbhagat.meg.SQliteDataBase.DatabaseHelperClass.Insert_Station(DatabaseHelperClass.java:48)
at com.gauravbhagat.meg.MainActivity.onCreate(MainActivity.java:64)
at android.app.Activity.performCreate(Activity.java:7340)
at android.app.Activity.performCreate(Activity.java:7331)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3106)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3269)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1960)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7094)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)