user3324329
user3324329

Reputation: 59

Inserting data in database in android

I am working in android , and trying to insert values in database, here is the code of my database class

public class DataHandler {
    public static final String fname="fname";
    public static final String lname="lname";
    public static final String age="21";
    public static final String height="180";
    public static final String weight="50";
    public static final String TABLE_NAME="user";

    public static final int  DATABASE_VERSION=1;
    public static final String DATA_BASE_NAME="SLIMART_DATABASE";
    public static final String TABLE_CREATE ="create table user(fname text not null, lname text not null, age text not null, height text not null,weight text not null);";

    DataBaseHelper dbhelper;
    Context ctx;
    SQLiteDatabase db;
    public DataHandler(Context ctx)
    {
        this.ctx=ctx;
        dbhelper=new DataBaseHelper(ctx);
    }
    private static class DataBaseHelper extends SQLiteOpenHelper
    {
        public DataBaseHelper(Context ctx)
        {
            super(ctx,DATA_BASE_NAME,null,DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            try{
            db.execSQL(TABLE_CREATE);
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS user");
            onCreate(db);

        }

    }
    public DataHandler open()
    {
        db=dbhelper.getWritableDatabase();
        return this;
    }
    public void close()
    {
        dbhelper.close();
    }

    public long insertData(String afname,String alname,String aage,String aheight,String aweight)
    {
        ContentValues content =new ContentValues();
        content.put(fname,afname);
        content.put(lname,alname);
        content.put(age,aage);
        content.put(height,aheight);
        content.put(weight,aweight);
        return db.insertOrThrow(TABLE_NAME, null, content);

    }

    public Cursor returnData()
    {
        return db.query(TABLE_NAME,new String[] {fname,lname,age,height,weight},null,null,null,null,null);
    }



}

and here is the activity code from where I am accessing the database class

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);

        save=(Button) findViewById(R.id.button1);
        fname=(EditText) findViewById(R.id.fname);
        lname=(EditText) findViewById(R.id.lname);
        age=(EditText) findViewById(R.id.age);
        height=(EditText) findViewById(R.id.height);
        weight=(EditText) findViewById(R.id.weight);

        save.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) 
            {
                String sfname=fname.getText().toString();
                String slname=lname.getText().toString();
                String sage=age.getText().toString();
                String sheight=height.getText().toString();
                String sweight=weight.getText().toString();

                handler = new DataHandler(getBaseContext());
                handler.open();
                long id=handler.insertData(sfname, slname, sage, sheight, sweight);
                Toast.makeText(getBaseContext(),"Data Successfully Saved",Toast.LENGTH_LONG).show();
                handler.close();

            }


        });

    }

but I recieve this error in logcat

03-22 03:16:37.060: E/AndroidRuntime(1115): android.database.sqlite.SQLiteException: near "0": syntax error (code 1): , while compiling: INSERT INTO user(lname,0,fname) VALUES (?,?,?)

Upvotes: 0

Views: 91

Answers (2)

tauitdnmd
tauitdnmd

Reputation: 368

I checked your code & i saw the problem, this is your code to insert data.

public long insertData(String afname,String alname,String aage,String aheight,String aweight)
    {
        ContentValues content =new ContentValues();
        content.put(fname,afname);
        content.put(lname,alname);
        content.put(age,aage);
        content.put(height,aheight);
        content.put(weight,aweight);
        return db.insertOrThrow(TABLE_NAME, null, content);

    }

-> Where: fname , lname , age, weight , height is field's name in database table. But you also define :

public static final String fname="fname";
    public static final String lname="lname";
    public static final String age="21";
    public static final String height="180";
    public static final String weight="50";

--> age,height , weight is not the name of field when you created database table.
SO: Please changed like this:

public static final String fname="fname";
    public static final String lname="lname";
    public static final String age="age";
    public static final String height="height";
    public static final String weight="weight";

Upvotes: 0

laalto
laalto

Reputation: 152797

content.put(age,aage);
content.put(height,aheight);
content.put(weight,aweight);

 

public static final String age="21";
public static final String height="180";
public static final String weight="50";

age, height and weight are not column names in your table but just numeric literals. Use actual column names as content keys. Also, you cannot have column names that are just numbers.

For example, change to:

content.put("age",aage);
content.put("height",aheight);
content.put("weight",aweight);

Upvotes: 1

Related Questions