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

    import android.app.Activity;
    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;
        
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //This Button object get button name
             Button button1 = (Button) findViewById(R.id.button1);
             button1.setOnClickListener(new OnClickListener()
             {
              public void onClick(View v)
              {
                      try
                      {
                          //This EditText Get Textboxs Values
                      EditText edittxt1 = (EditText)findViewById(R.id.txt1);
                      EditText edittxt2 = (EditText)findViewById(R.id.txt2);
                      EditText edittxt3 = (EditText)findViewById(R.id.txt3);
                          //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))";
                        db.execSQL(CREATE_CONTACTS_TABLE);
                        //This is String Insert Data in your Table
                        String    Query =    "insert into info (name,Email,phone) values ('" + edittxt1.getText() + "','" + edittxt2.getText() + "','" + edittxt3.getText() + "')";
                        db.execSQL(Query);
                      }
                      catch(Exception e)
                      {    // This is Toast Catch your Exceptions
                          Toast.makeText(MainActivity.this, "ERROR "+e.toString(), Toast.LENGTH_LONG).show();
                      }    // now DB Close
                          db.close();
                  }
                });
        }

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

    Thank You
    Regards,
    M.Hussain
    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

Tagged

Popular Posts of the Week

    Optimum Creative