Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

db.insert only stores auto incremented id in database

Tags:

android

sqlite

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;
    }
}
like image 260
moirK Avatar asked Aug 04 '26 12:08

moirK


1 Answers

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.

like image 149
mango Avatar answered Aug 07 '26 02:08

mango



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!