moirK
moirK

Reputation: 681

db.insert only stores auto incremented id in database

db.insert function inserts only an auto incremented ID in the table and nothing else though I get values from activity edit texts. Rest of the cells of table row remain empty

Here is my code:

public class SHelper extends SQLiteOpenHelper {

    static final String dbName = "School";
    static final String studentTable = "Student";
    static final String colName = "Name";
    static final String colPassword = "Password";
    public static final int dbVersion=1;

    public SHelper(Context context)
    {
        super(context, dbName, null, dbVersion);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "+studentTable+"("+colID+" INTEGER PRIMARY KEY, "+colName+" TEXT,"+colPassword+" TEXT);");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + studentTable);
        onCreate(db);
    }

    public void add(StudentData studentdata) 
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(colName, StudentData.getName()); 
        values.put(colPassword, StudentData.getPassword());
        db.insert(studentTable, null, values);
        db.close();
    }


}

public class StudentData {

    private static String name;
    private static String password;

    public StudentData() {
        super();
    }

    public StudentData(String name,String password) {
        super();

        StudentData.name = name;
        StudentData.password = password;
    }

    public static String getName() {
        return name;
    }

    public void setName(String name) {
        StudentData.name = name;
    }

        public static String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        StudentData.password = password;
    }
}

public class stoActivity extends Activity {

     SHelper db = new SHelper(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sto);

        final EditText fullname=(EditText)findViewById(R.id.fullName);
        final EditText password=(EditText)findViewById(R.id.password);

        final String fullname=fullname.getText().toString();
        final String password=password.getText().toString();

        Button registerbutton = (Button) findViewById(R.id.registerButton);      
        registerbutton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v){

            db.add(new StudentData(fullname,password));  

           Intent it=new Intent(stoActivity.this,otherActivity.class);
           stoActivity.this.startActivity(it);   
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_register, menu);
        return true;
    }
}

Upvotes: 3

Views: 288

Answers (4)

mango
mango

Reputation: 5636

There's your issue:

public void add(*StudentData studentdata) 
    {
        ...    
        values.put(colName, *StudentData.getName()); 
        values.put(colPassword, *StudentData.getPassword());
        ...
    }

Can you see it? Your statement is referring to the wrong part of the signature. you need to change the StudentData in the body of your method to lower case studentdata to match what is being passed to the method as a parameter. This illegal behavior went by compiled because you have the static modifier in places where i'm not sure is necessary, ex. the getName() and getPassword() methods.

Upvotes: 1

Opiatefuchs
Opiatefuchs

Reputation: 9870

It´s just an assumtion, but I had similar problems if I wanted to save data inside db. In your onCreate-Method at db.execSQL, You got a blank after comma --> "INTEGER PRIMARY KEY, ". I know that SQLite is very sensitive with such kind of input, You have to look exactly at blanks, commas or any other input.

Upvotes: 0

s.d
s.d

Reputation: 29436

You are not inserting any value for the column you have declared as primary Key.

Upvotes: 0

jeet
jeet

Reputation: 29199

create table with AutoIncrement key, so change method onCreate to the following:

@Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "+studentTable+"("+colID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+colName+" TEXT,"+colPassword+" TEXT);");

    }

Upvotes: 0

Related Questions