4

I checked other examples in SO and I searched a lot, nothing is working for me. The database file is this (after suggested edit).

Error

E/Database(274): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
E/Database(274):    at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
E/Database(274):    at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
E/Database(274):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549)
E/Database(274):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
E/Database(274):    at com.example.nycgasstationhunter.userRegister$2.onClick(userRegister.java:51)
E/Database(274):    at android.view.View.performClick(View.java:2408)
E/Database(274):    at android.view.View$PerformClick.run(View.java:8816)
E/Database(274):    at android.os.Handler.handleCallback(Handler.java:587)
E/Database(274):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/Database(274):    at android.os.Looper.loop(Looper.java:123)
E/Database(274):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/Database(274):    at java.lang.reflect.Method.invokeNative(Native Method)
E/Database(274):    at java.lang.reflect.Method.invoke(Method.java:521)
E/Database(274):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/Database(274):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/Database(274):    at dalvik.system.NativeStart.main(Native Method)

Code

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;

public class userRegister extends Activity{
//database
SQLiteDatabase db;
DBHelper dbhelper;
Context ourContext;
ContentValues cv;

 public void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_register);

//database  
 dbhelper=new DBHelper(this);
 db=dbhelper.getWritableDatabase();
 cv=new ContentValues();
//editText
final EditText userEdit=(EditText)  findViewById(R.id.userEdit);
final EditText emailEdit=(EditText) findViewById(R.id.emailEdit);
final EditText passwordEdit=(EditText)  findViewById(R.id.passwordEdit);
final EditText retypePassEdit=(EditText) findViewById(R.id.retypePassEdit);
//Register button
    Button regButton = (Button) findViewById(R.id.regButton);
      regButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
    //string
    final String userName=userEdit.getText().toString();
    final String emailAddress=emailEdit.getText().toString();
    final String password=passwordEdit.getText().toString();
    final String retypePassword=retypePassEdit.getText().toString();

         if (userName.length()!=0){
            if (emailAddress.length()!=0){
              if (password.length()!=0){
                 if (retypePassword.equals(password)){
                   //save in DB
                    cv.put(DBHelper.USER, userName);
                    cv.put(DBHelper.EMAIL,emailAddress);
                    cv.put(DBHelper.PASSWORD, retypePassword); 

                    db.insert(DBHelper.USER_TABLE, null, cv);

                    Intent intent = new Intent (userRegister.this,Profile.class);    
                     startActivity(intent);
               }
                 else 
                     Toast.makeText(userRegister.this,"Password mismatch", Toast.LENGTH_SHORT).show(); 
            }
              else 
                     Toast.makeText(userRegister.this,"Invalid password", Toast.LENGTH_SHORT).show();
        }
            else 
                 Toast.makeText(userRegister.this,"Invalid email", Toast.LENGTH_SHORT).show();
    }
         else 
             Toast.makeText(userRegister.this,"Invalid user name", Toast.LENGTH_SHORT).show();
 }
});
}
}

When I insert information and press regButton, it sends me to Profile activity. There is no information being saved in database. I don't understand the error in LogCat and how can I solve that? Why there is nothing in database? (I am using SQLite Database Browser to check data.) Thank you

3
  • Please add your DBHelper code / database schema. Commented Jan 1, 2014 at 21:04
  • @laalto --The database (with suggested edit):stackoverflow.com/questions/20444838/failure-1syntax-error Commented Jan 2, 2014 at 1:45
  • Please add the table creation part in this question. Commented Jan 2, 2014 at 7:09

3 Answers 3

6

The error SQLiteConstraintException indicates that an integrity constraint was violated.

From your Create Table SQL command I think the first problem is your primary key. It should be declare as autoincrement

Change String createDB:

public final String createDB="create table "+USER_TABLE+"("
                    + C_ID + " integer primary key autoincrement, "
                    + USER + " text not null,"  
                    + EMAIL + " text not null," 
                    + PASSWORD + " text not null," 
                    + TIME + " text not null);";  

Also, all fields are declared not null, you should test if retypePassword is not null and you must set a value to field TIME.

EDIT

In order the autoincrement take effect, the database version value need to be incremented.
In DBHelper class change DATABASE_VERSION=1; to DATABASE_VERSION=2;

Sign up to request clarification or add additional context in comments.

6 Comments

Same error.But if I change like this: code try { db.insertOrThrow(DBHelper.USER_TABLE, null, cv); } catch (SQLException ex) { // do something with ex or nothing } errror goes away but nothing in Database
I forgot to mention one thing! Please see the edit to my answer.
No luck. I got a force close. Problem with upgrade ---Public void onUpgrade(){} Error I/Database(275): sqlite returned: error code = 1, msg = near "existsuserInfoTable": syntax error E/AndroidRuntime(294): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myappp/com.example.myapp.userRegister}: android.database.sqlite.SQLiteException: near "existsuserInfoTable": syntax error: drop table if existsuserInfoTable ...so on
I fixed the problem for now. I changed the emulator. Now it works and I can see the data. I didn't change the DATABASE_VERSION and removed Log.w() from public void onUpgrade(){} in DBHelper class. Please let me know that if this is a right path or not. Any advice will be welcomed. Thank you
As you do when write any frase in english or in any language you should put spaces between words. A space is need between the sentence drop table if exists and table name, add a space after word "exist" in db.execSQL("drop table if exists " + USER_TABLE);. When you change the emulator a new database was created with the changed createDB string that's why now works.
|
2

If the database schema is as follows as you mention in comments (from this question)...

public final String createDB="create table "+USER_TABLE+"("
                     +C_ID+" integer primary key, "
                     +USER+" text not null,"  
                     +EMAIL+ " text not null," 
                     +PASSWORD+ " text not null," 
                     +TIME+ " text not null);";

... then you have not specified a value for the TIME column which has the not null constraint.

Some options:

  • Add a TIME value to the ContentValues before insert().

  • Change the schema and provide a suitable default such as

    TIME + " text not null default current_timestamp"
    

    As always, when changing the database schema, remove the old database so that onCreate() gets called with the new code (clear app data or just uninstall the app).

Comments

0

In my case I made two HTTP-requests and got two lists. Then wrote them to two tables. But first in a child table and then in parent. Strange, but a problem appeared on Android 4.4.2, but not on Android 5.0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.