DreamsCoder.com

We Code your Dreams


home > Simple Android program to insert values into Database




Simple Android program to insert values into Database







To create Database application in Android we have
created a separate activity for creation of database and
Queries DBhelper.java and for Accessing database
we have created an User Interface in MainActivity.java
We have also provided activity_main.xml file
which is user Interface.

How to retrieve values from SQlite Database - Click Here



/*DBhelper*/

package com.example.simpledatabase;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DBhelper extends SQLiteOpenHelper
{
	static final String dbname="demo";
	static final int version=1;
	static final String name="uname";
	static final String num="uno";
	
	static final String qry=
"create table addrecord (no integer 
primary key autoincrement,
" + "uno integer not null,
" + " uname text not null)"; 
	

public DBhelper(Context context) 
       {
	super(context, dbname, null, version);
	// TODO Auto-generated constructor stub
	/* it provides access to local files, databases,class loaders associated to the 
environment, services including system-level 
services, and more.*/ 
	}

	
	@Override
	public void onCreate(SQLiteDatabase db) 
        {
	// TODO Auto-generated method stub
		
	db.execSQL(qry);
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, 
int arg1, int arg2) 
        {
	// TODO Auto-generated method stub
		
	}
}//DBhelper class

_______________________________________________


/* Main Activity */
package com.example.simpledatabase;

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

public class MainActivity extends Activity {
EditText e1,e2;
Button b;
String name;
int num;
DBhelper dbh;
SQLiteDatabase dtb; //to fire Queries

	@Override
	protected void onCreate
(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
		
	DBhelper dbh = new DBhelper(this);
	dtb = dbh.getWritableDatabase();
		
	}
	public void insertDB(View view)
	{

		e1 = (EditText) findViewById
(R.id.editText1);
		e2 = (EditText)findViewById
(R.id.editText2);
		num = Integer.parseInt(e1.getText().toString());
	name = e2.getText().toString();
		
		ContentValues c = new ContentValues();
		c.put(dbh.name, name);
		c.put(dbh.num, num);
		
		dtb.insert("addrecord", null, c); /*Record Inserted*/
		Toast.makeText(this,
"Record Insereted Succesfully", 
		Toast.LENGTH_LONG).show();

	}
}//MainActivity class

________________________________________________

/*activity_main.xml*/



    


    


    

Label - android





by - Chinmay Mahajan





Are you looking for something else?


Suggested Programs




Working with SQlite Database in Android


How to check values inserted in SqliteDatabase


How to retrieve values from SQlite Database


Simple Text-to-Speech Android App


Error while Creating New Android Project in Eclipse