0

db.insert(table_name,null,contentValues); is always returning -1 and it is not inserting the values in the databases.

Can you please help me to figure out what I'm doing the wrong?

database_class.java:

public class database_class extends SQLiteOpenHelper {
    public static final String database_name = "signup_db";
    public static final String table_name = "signup";
    public static final String column_1 = "FIRST_NAME";
    public static final String column_2 = "SECOND_NAME";
    public static final String column_3 = "EMAIL";
    public static final String column_4 = "PASSWORD";
    public static final String column_5 = "c_password";

    public database_class(Context context){
        super(context,database_name , null, 1);
        //Toast.makeText(context, "Class called ", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table" + table_name + "(ID INTEGER PRIMARY KEY AUTOINCREMENT , FIRST_NAME TEXT , SECOND_NAME TEXT , EMAIL TEXT , PASSWORD TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS" + table_name);
        onCreate(db);
    }
    public boolean insertData(String first_name, String second_name, String email, String password){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(column_1,first_name);
        contentValues.put(column_2,second_name);
        contentValues.put(column_3,email);
        contentValues.put(column_4,password);
        long result = db.insert(table_name,null,contentValues);
        if (result == -1)
            return false;
        else
            return true;

    }
}

MainActivity Code:

fn = editText1.getText().toString();
ln = editText2.getText().toString();
em = editText3.getText().toString();
pass = editText4.getText().toString();
cpass = editText5.getText().toString();

if (1 == 1) {
    //boolean isInserted =  mydb.insertData(signup_values[0].toString(),signup_values[1].toString(),signup_values[2].toString(),signup_values[3].toString());
    boolean isInserted =  mydb.insertData(fn,ln,em,pass);
    if(isInserted == true) {
        Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_SHORT).show();
    }
    else
        Toast.makeText(MainActivity.this, "Data not Inserted", Toast.LENGTH_SHORT).show();
}
else
    Toast.makeText(MainActivity.this, "Password Mismatch", Toast.LENGTH_SHORT).show();
guipivoto
  • 18,327
  • 9
  • 60
  • 75
Mudasir Sharif
  • 733
  • 2
  • 15
  • 31
  • I am not an android developer, but given i can't see a way to get the error from the db, i would check that the db you are inserting to is available and open before trying to do your insert. – Gavin Jul 31 '16 at 17:02
  • Hint: read about java naming conventions. Classes start Uppercase, variables dont have _ in their names. Besides: your names are pretty much horrible! There is no need to save characters; like: what is ln supposed to mean? em? pass? cpass? Use names that say what the thingy they denote **is**. And either use an array for your text fields; or give those appropriate names too. Finally: consider not using static all over the place for your fields. You see - you can make the db name a field that you give to your class in the constructor for example. – GhostCat Jul 31 '16 at 17:11
  • @GhostCat Try to solve the problem, not nitpick the syntax ;) You're wasting your breath – OneCricketeer Jul 31 '16 at 17:18
  • @cricket_007 Everybody here downvotes bad questions or puts up close requests. That is a convention of this community. Java programmers have conventions too; and I don't see it as a waste of time to tell people about that. And: more than once people came back and said: "oh, i wasn't aware that my code isnt following conventions; i will look into that; thank you". So thanks, but I think I am perfectly able to manage my breath ;-) – GhostCat Jul 31 '16 at 17:27
  • Yeah its true that i am not following the conventions because i am new in java. With the passage of time i will take care of each and every convention. Thanks for your comments. – Mudasir Sharif Jul 31 '16 at 18:12

1 Answers1

4

I believe there's an error while creating your database. This way, create table will fail and then, insert will also fail (because table was not properly created).

Other parts of your code seems OK...

This line, will produce following string:

db.execSQL("create table" + table_name + "(ID INTEGER PRIMARY KEY AUTOINCREMENT , FIRST_NAME TEXT , SECOND_NAME TEXT , EMAIL TEXT , PASSWORD TEXT)");

Result:

create tablesignup(ID INTEGER PRIMARY KEY AUTOINCREMENT , FIRST_NAME TEXT , SECOND_NAME TEXT , EMAIL TEXT , PASSWORD TEXT)

This is not valid SQL Command. So, try to add some spaces like:

db.execSQL("create table " + table_name + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT, FIRST_NAME TEXT, SECOND_NAME TEXT, EMAIL TEXT, PASSWORD TEXT )");

Error 2

Your onUpgrade() also wont work due to same error:

db.execSQL("DROP TABLE IF EXISTS" + table_name);

Need to add some spaces like:

db.execSQL("DROP TABLE IF EXISTS " + table_name);

Finally

I tested your code and works fine. Maybe, some error happened before and it would be good to "reinstall" the database.

How you can do:

  • Unistall/Install your app

  • Update your database version to force onUpgrade(). Just do: super(context, database_name , null, 2); at onCreate()

guipivoto
  • 18,327
  • 9
  • 60
  • 75