how to connect to database in android sqlite
  • package com.example.datainformation;

    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.os.Bundle;
    import android.content.Context;
    import android.util.Log;
    import android.view.View.OnClickListener;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.Toast;

    public class MainActivity extends Activity
        /*Right Code*/

        SQLiteDatabase db;
        protected void onCreate(Bundle savedInstanceState)
            //This Button object get button name
             Button button1 = (Button) findViewById(;
             button1.setOnClickListener(new OnClickListener()
              public void onClick(View v)
                          //This EditText Get Textboxs Values
                      EditText edittxt1 = (EditText)findViewById(;
                      EditText edittxt2 = (EditText)findViewById(;
                      EditText edittxt3 = (EditText)findViewById(;
                          //This is Create Database in you Data Folder
                          db = openOrCreateDatabase( "aahussain.db", SQLiteDatabase.CREATE_IF_NECESSARY , null );
                          // This String Create table in database
                        String CREATE_CONTACTS_TABLE = "CREATE TABLE if not exists info (name VARCHAR(255),Email VARCHAR(255),phone VARCHAR(255))";
                        //This is String Insert Data in your Table
                        String    Query =    "insert into info (name,Email,phone) values ('" + edittxt1.getText() + "','" + edittxt2.getText() + "','" + edittxt3.getText() + "')";
                      catch(Exception e)
                      {    // This is Toast Catch your Exceptions
                          Toast.makeText(MainActivity.this, "ERROR "+e.toString(), Toast.LENGTH_LONG).show();
                      }    // now DB Close

        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(, menu);
            return true;

    Thank You
    Software Engineer
  • 1 Comment sorted by
  • Nice sharing hussain ..Keep it up.. :)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook


Popular Posts of the Week