0

hii..i am new to android..i am developing an android application on platform 2.1..
There are two textbox: one for Name and other for entering job.I want the values should be taken from the 2 textboxes editText1 and editText2 and inserted in my database whenever a user clicks the button.Please help im stuck in this part.
my code is creating the table, but values are not being inserted into it.

here are my two classes:
DbHelper.java

package test.db.help;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;



public class DbHelper extends SQLiteOpenHelper {
    public static final String db_name = "mydb.db";
    public static final int db_version = 1;
    public final String TABLE = "test";

    public  final String C_Name = "Name";
    public final String C_job = "Job";

    public DbHelper(Context context, String db_name, CursorFactory factory,
            int version) {
        super(context, db_name, null, db_version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        String sql = String.format(
                "create table %s ( %s TEXT, %s TEXT )",
                TABLE, C_Name, C_job);

    db.execSQL(sql);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }


}


TestActivity.java

package test.db.help;

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

public class TestActivity extends Activity implements OnClickListener {
    EditText Name,Job;
    Button b;
    DbHelper obj;
    SQLiteDatabase db;
    public static final String db_name = "mydb.db";
    public static final int db_version = 1;
    ContentValues val; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(test.db.help.R.layout.dbtesting);

        Name=(EditText) findViewById(R.id.editText1);
        Job = (EditText) findViewById(R.id.editText2);

        obj=new DbHelper(TestActivity.this,db_name , null, db_version);
        db=obj.getWritableDatabase();
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        val=new ContentValues();
        val.clear();

        val.put(obj.C_Name,Name.getText().toString());
        val.put(obj.C_job,Job.getText().toString());
        db.insert(obj.TABLE, null, val);
    }
}

Edited by supreet.kaur26: title similar to old title already exists

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by peter_budo
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.