DreamsCoder.com

We Code your Dreams
home > android programs

Working with SQlite Database in Android





Working with SQlite Database in Android

Label - android





How to check values inserted in SqliteDatabase





How to check values inserted in SqliteDatabase

Label - android





How to retrieve values from SQlite Database





How to retrieve values from SQlite Database

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 {

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();
		
	}
		
		Cursor cs=dtb.query("addrecord", 
new String[]
{
dbh.num,dbh.name}, null, null, 
null, null, null);
		if(cs!=null)
		{
			cs.moveToFirst();
		}
		int no;
		String nm;
		do
		{
		no = Integer.parseInt(cs.getString(0));
		nm = cs.getString(1);
	
		Toast.makeText(this, 
"Number is : "+no+" Name is :"+nm, Toast.LENGTH_LONG).show();

		}while(cs.moveToNext()); 
//while last record is present
		
	} // Cursor
}//class

Label - android





Simple Android program to insert values into Database





Simple Android program to insert values into Database

/*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





Simple Text-to-Speech Android App





Simple Text-to-Speech Android App

package com.example.basic;
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	EditText e;
	Button b;
	String nam;
	TextToSpeech ttobj;
	

	@Override
	protected void onCreate(Bundle s) {
	super.onCreate(s);
	setContentView(R.layout.activity_main);
		
	e = (EditText)findViewById
(R.id.editText1);
findViewById(R.id.editText2);
    
    b = (Button)findViewById(R.id.button1);

        
        /* Creating Object of textToSpeech*/
        ttobj=new TextToSpeech
(getApplicationContext(), 			      new TextToSpeech.OnInitListener() 
{
	  @Override
	public void onInit(int status) 
        {
        if(status != TextToSpeech.ERROR)
         {
			                             ttobj.setLanguage(Locale.UK);

          }				
    }

		
});//TextToSpeech
        
        /*Button Listener*/
        b.setOnClickListener(new View.OnClickListener()
        {
        
        @Override
     public void onClick(View v)
     {
       	 nam = e.getText().toString();	
    	 speakText(); //call to method SpeechToText()
    	 
    
     }//onClick

        }); //onClickListener
    		}//onCreate Bundle

	 public void speakText(){
	   
	      Toast.makeText(getApplicationContext(),
nam, Toast.LENGTH_SHORT).show();
	      
	      ttobj.speak(nam,
TextToSpeech.QUEUE_FLUSH, null);
	      
	   }//onspeakText

}//classs

Label - android





next page >






Privacy Policy
Copyright © 2018 by DreamsCoder. All Rights Reserved.
DreamsCoder Google Plus DreamsCoder Facebook



Latest Technology,Tricks and Tips