Save current and end time to sqlite database

Asked

Viewed 621 times

0

I am developing a project about home automation and at the moment when I click on the CONNECT button would realize the following functionalities: start device, start chronometer, save current time and send to the database.

By clicking the Disconnect button, if you would turn off the device, the timer would be reset, the current time of disconnection of the device would be saved and it would be sent to the bank.

It happens that while trying to save the current time when clicking on CONNECT shows the following error:

05-30 12:31:25.874    2192-2192/com.example.jhow.tecnumautomao E/SQLiteLog﹕ (1299) abort at 5 in [INSERT INTO contacts(tempoInicial) VALUES (?)]: NOT NULL constraint failed: contacts.name
05-30 12:31:25.895    2192-2192/com.example.jhow.tecnumautomao E/SQLiteDatabase﹕ Error inserting tempoInicial=12:31:25
    android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: contacts.name (code 1299)
            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:1471)
            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
            at com.example.jhow.tecnumautomao.Persistencia.PessoaDAO.dadosTimeInicial(PessoaDAO.java:158)
            at com.example.jhow.tecnumautomao.lampada_quarto$1.onClick(lampada_quarto.java:77)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Also follows method that saves time on the seat:

DBSQLITE CLASS TABLE:

 public void onCreate(SQLiteDatabase db) {

    String TABLE_CREATE =

            "CREATE TABLE " + Pessoa.TABLE_NAME  + "("
            + Pessoa.COLUNA_ID  + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
            + Pessoa.COLUNA_NOME + " TEXT, "
            + Pessoa.COLUNA_UNAME + " TEXT, "
            + Pessoa.COLUNA_EMAIL + " TEXT, "
            + Pessoa.COLUNA_PASS + " TEXT, "
            + Pessoa.COLUNA_TIME_INICIAL + " TEXT, "
            + Pessoa.COLUNA_TIME_FINAL + " TEXT "
            + Pessoa.COLUNA_SENHA1 + " TEXT, "
            + Pessoa.COLUNA_SENHA2 + " TEXT )";

    db.execSQL(TABLE_CREATE);
    this.db = db;


}

WAYS TO SAVE TIME

public void dadosTimeInicial(){

    SQLiteDatabase db = dbsqLite.getWritableDatabase();

    SimpleDateFormat dateFormat_hora = new SimpleDateFormat("HH:mm:ss");


    Date data = new Date();
    Calendar  cal = Calendar.getInstance();
    cal.setTime(data);
    Date data_atual = cal.getTime();

    String hora_atual = dateFormat_hora.format(data_atual);

    ContentValues values = new ContentValues();
    values.put(Pessoa.COLUNA_TIME_INICIAL, hora_atual);

    db.insert(Pessoa.TABLE_NAME, null , values );
    db.close();
}

 public void dadosTimeFinal (){


    SQLiteDatabase db = dbsqLite.getWritableDatabase();

    SimpleDateFormat dateFormat_hora = new SimpleDateFormat("HH:mm:ss");


    Date data = new Date();
    Calendar  cal = Calendar.getInstance();
    cal.setTime(data);
    Date data_atual = cal.getTime();

    String hora_atual = dateFormat_hora.format(data_atual);

    ContentValues values = new ContentValues();
    values.put(Pessoa.COLUNA_TIME_FINAL, hora_atual);

    db.insert(Pessoa.TABLE_NAME, null , values );
    db.close();
  • When you first created the "Contacts" table, by chance the "name" field was originally NOT NULL and then you modified it to NULL (as it is now in the code you posted)?

  • Hello did not realize this modification that you mentioned, the error that this giving is caused by the field "name "? but as this excerpt of code does not modify the same ?

  • It is because if this field was originally NOT NULL when creating the database, it would give the error as you are entering only date data and the other nulls. And sometimes a common mistake when implementing database on Android and we make a change in the structure of the database in the Helper onCreate, we forget to reinstall the app or else implement the changes in the onUpgrade routine, and with that we do not see the bank changes in the app, which in your case could be the name column still as NOT NULL complaining that one is trying to insert a null value in it (which is what the error is saying).

  • in case if this is the problem is possible I change the column "name" from NOT NULL to NULL ? as I would make this modification in android sqlite ?

  • By the code you posted it is already NULL, should not be giving this error if the bank was created like this from the beginning. Just out of curiosity, try to write some value in this column, even if an empty string, beyond the date, just to see if the error disappears or changes.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.