So this is my first sqlite database and I'm running into an issue trying to insert rows into the database. All I want to do is insert integers into the sqlite database. Maybe theres a simpler method than what I'm Doing? Could I do it with the CheckedRoutes class?
Heres myDbHandler
package com.example.zach.listview;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.content.ContentValues;
public class MyDBHandler extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "checked_routes.db";
public static final String TABLE_CHECKED_ROUTES = "checked_routes";
public static final String COLUMN_ID = "_checkedRoutes";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_CHECKED_ROUTES +
"(" + COLUMN_ID + " INTEGER PRIMARY KEY " + ");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CHECKED_ROUTES);
onCreate(db);
}
/////Add row to table
public void addCheckedRoute(CheckedRoutes checkedRoute){
ContentValues values = new ContentValues();
values.put(COLUMN_ID, checkedRoute.get_checkedRoute());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_CHECKED_ROUTES, null, values);
db.close();
}
////Delete row from table
public void deleteCheckedRow(int checkedRoute){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_CHECKED_ROUTES + "WHERE " + COLUMN_ID + "=\"" + checkedRoute + "\";" );
}
}
heres my CheckedRoutes
package com.example.zach.listview;
public class CheckedRoutes {
int _checkedRoute;
public void set_checkedRoute(int _checkedRoute){
this._checkedRoute = _checkedRoute;
}
public int get_checkedRoute(){
return _checkedRoute;
}
}
And heres how I'm trying to insert the rows
MyDBHandler dbHandler;
int listViewItemPosition;
listViewItemPosition = getIntent().getIntExtra("listViewItemPosition", 0);
//add checked route position to database
CheckedRoutes checkedRoute = new CheckedRoutes();
checkedRoute.set_checkedRoute(listViewItemPosition);
dbHandler.addCheckedRoute(checkedRoute);
and heres my error
E/SQLiteDatabase: Error inserting _checkedRoutes=3
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: checked_routes._checkedRoutes (code 1555)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1474)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
at com.example.zach.listview.MyDBHandler.addCheckedRoute(MyDBHandler.java:39)
at com.example.zach.listview.RouteDetails.onBackPressed(RouteDetails.java:109)
at com.example.zach.listview.RouteDetails.onOptionsItemSelected(RouteDetails.java:88)
at android.app.Activity.onMenuItemSelected(Activity.java:3204)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:406)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:103)
at android.support.v7.widget.ToolbarWidgetWrapper$1.onClick(ToolbarWidgetWrapper.java:183)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
thank you for any help