title: Won't use my Sql database
body: <p>Hey i have some trouble with my SQLite database, when i have closed my app and i then try to save my values it gives me an error in my logcat.</p>
  
  <p>The app was supposed to store the data when i close the app with sharedPreferences, when i reopen the app is supposed to send data from my main class and when i push the stop button the data should have been saved to my SQLite database.</p>
  
  <p>Why is this not possible?</p>
  
  <p>It is possible to do this without closing the app.</p>
  
  <p>Thanks for your help.</p>
  
  <p>My main class:</p>
  
  <pre><code>public class Main extends Activity{
  
  Button bStart, bStop;
  TextView tvView;
  Spinner spinner1;
  boolean stopValue;
  int startkilometer;
  String date;
  
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      bStart = (Button) findViewById(R.id.bStart);
      tvView = (TextView) findViewById(R.id.tvView);
      spinner1 = (Spinner) findViewById(R.id.spinner1);
  
  
      SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
  
  
  
      stopValue = pref.getBoolean("stopper", false);
      startkilometer = pref.getInt("startkm", 0);
      date = pref.getString("datoen", "");
  
  
  
      date = getIntent().getStringExtra("datoen");
      startkilometer = getIntent().getIntExtra("startkm", startkilometer);
      stopValue = getIntent().getBooleanExtra("stopper", stopValue);
  
  
      if(stopValue == false){
          bStart.setText("Start");
          bStart.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
                  // TODO Auto-generated method stub
                  Intent start = new Intent("com.uniqueapps.runner.START");
                  startActivity(start);
              }
          });
      }
  
      if(stopValue){
          bStart.setText("Stop");
          bStart.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  Intent stop = new Intent("com.uniqueapps.runner.STOP");
                  stop.putExtra("startkm", startkilometer);
                  stop.putExtra("datoen", date);
                  startActivity(stop);
              }
          });
      }
  
  @Override
  protected void onPause() {
      // TODO Auto-generated method stub
  
      SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean("stopper", stopValue);
        editor.putInt("startkm", startkilometer);
        editor.putString("datoen", date);
        editor.commit();
  
      super.onPause();
  }
  </code></pre>
  
  <p>My stop class:</p>
  
  <pre><code>public class Stop extends Main implements OnClickListener {
  
  Button bStop;
  EditText sqllocations, kilometer;
  int startkilometer, slutkilometer, kortekm;
  String locations;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.stop);
      bStop = (Button) findViewById(R.id.bstopTur);
      bStop.setOnClickListener(this);
      sqllocations = (EditText) findViewById(R.id.locations);
      kilometer = (EditText) findViewById(R.id.drovenKilometer);
  
  
      startkilometer = getIntent().getIntExtra("startkm", 0);
      date = getIntent().getStringExtra("datoen");
  
      sqllocations.setText("Unknown");
  }
  
  @Override
  public void onClick(View v) {
  
      // TODO Auto-generated method stub
  
      switch(v.getId()){
  
      case R.id.bstopTur:
  
          String locations = sqllocations.getText().toString();
          slutkilometer = Integer.valueOf(kilometer.getText().toString()); 
          int kortekm = (slutkilometer - startkilometer);
  
          try{
  
          KilometerSQL entry = new KilometerSQL(this);
          entry.open();
          entry.createEntry(date, kortekm, locations); ----&gt; Line 60
          entry.close();
          }catch(Exception e){
              e.printStackTrace();
          }
  
           Intent menu = new Intent("com.uniqueapps.runner.MENU");
              menu.putExtra("stopper", false);
              startActivity(menu);
              break;
      }
  }
  
  }
  </code></pre>
  
  <p>My KilometerSQL:</p>
  
  <pre><code>public class KilometerSQL {
  
  public static final String KEY_ROWID = "date";
  public static final String KEY_KILOMETER = "kilometer";
  public static final String KEY_LOCATIONS = "locations";
  
  private static final String DATABASE_NAME = "Kilometerdb";
  private static final String DATABASE_TABLE = "kilometertable";
  private static final int DATABASE_VERSION = 1;
  
  private DbHelper ourHelper;
  private final Context ourContext;
  private SQLiteDatabase ourDatabase;
  
  private static class DbHelper extends SQLiteOpenHelper{
  
      public DbHelper(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
          // TODO Auto-generated constructor stub
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          // TODO Auto-generated method stub
          db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                  KEY_ROWID + " TEXT NOT NULL, " +
                  KEY_KILOMETER + " INTEGER, " +
                  KEY_LOCATIONS + " TEXT NOT NULL);"
  
  
                  );
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          // TODO Auto-generated method stub
          db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
          onCreate(db);
      }
  
  
      }
  
  public KilometerSQL (Context c){
      ourContext = c;
  }
  
  public KilometerSQL open(){
      ourHelper = new DbHelper(ourContext);
      ourDatabase = ourHelper.getWritableDatabase();
      return this;
  }
  
  public void close(){
      ourHelper.close();
  }
  
  public long createEntry(String date, int kortekm, String locations) {
      // TODO Auto-generated method stub
      ContentValues cv = new ContentValues();
      cv.put(KEY_ROWID, date);
      cv.put(KEY_KILOMETER, kortekm);
      cv.put(KEY_LOCATIONS, locations);
      return ourDatabase.insert(DATABASE_TABLE, null, cv); ----&gt; Line 74
  }
  
  public String getData() {
      // TODO Auto-generated method stub
      String [] columns = new String []{ KEY_ROWID, KEY_KILOMETER, KEY_LOCATIONS};
      Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
      String result = "";
  
      int iDate = c.getColumnIndex(KEY_ROWID);
      int iKilometer = c.getColumnIndex(KEY_KILOMETER);
      int iLocations = c.getColumnIndex(KEY_LOCATIONS);
  
      for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
          result = result + c.getString(iDate) + " " + c.getString(iKilometer) + " " + c.getString(iLocations) + "\n";
      }
  
      return result;
  }
  
  
  }
  </code></pre>
  
  <p>My Logcat:</p>
  
  <pre><code>10-01 13:54:25.865: E/SQLiteDatabase(3199): Error inserting kilometer=30 date=null locations=Unknown
  10-01 13:54:25.865: E/SQLiteDatabase(3199): android.database.sqlite.SQLiteConstraintException: kilometertable.date may not be NULL (code 19)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at com.uniqueapps.runner.KilometerSQL.createEntry(KilometerSQL.java:74)  
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at com.uniqueapps.runner.Stop.onClick(Stop.java:60)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at android.view.View.performClick(View.java:4240)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at android.view.View$PerformClick.run(View.java:17721)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at android.os.Handler.handleCallback(Handler.java:730)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at android.os.Handler.dispatchMessage(Handler.java:92)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at android.os.Looper.loop(Looper.java:137)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at android.app.ActivityThread.main(ActivityThread.java:5103)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at java.lang.reflect.Method.invokeNative(Native Method)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at java.lang.reflect.Method.invoke(Method.java:525)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  10-01 13:54:25.865: E/SQLiteDatabase(3199):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>