2

I am creating a login application in which data which is typed in EditText will get in a variable upon clicking on button in a java file and than that value will get stored into the sqlite database which I have created but when I typed value in EditText and click button I didn't get any database created into file explorer.

Here is my code:

import java.util.Locale;
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;
import android.widget.Toast;

public class AAAActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        EditText userid = (EditText)findViewById(R.id.et_un);
        EditText password = (EditText)findViewById(R.id.et_pw);
        final String uid;
        final String pwd;

        uid = userid.getText().toString();
        pwd = password.getText().toString();

        Button btn = (Button)findViewById(R.id.btn_login);
        btn.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            InsertUser(uid, pwd);
            show();                 
            }
           });
    }    

    public void InsertUser(String user_name, String password){
        //SQLiteDatabase db;
        SQLiteDatabase db;
        db = openOrCreateDatabase("TestData.db", SQLiteDatabase.CREATE_IF_NECESSARY,  
              null);

       try{

           db.setVersion(1);
           db.setLocale(Locale.getDefault());
           db.setLockingEnabled(true); 
           /*final String CREATE_TABLE_USERS = "CREATE TABLE users (" + "id INTEGER
                     PRIMARY KEY AUTOINCREMENT,"+" LoginName text not null, Password 
                                  text not null);"; */          
           String CREATE_TABLE_USERS = "CREATE TABLE users (" + "id INTEGER PRIMARY KEY
              AUTOINCREMENT,"+" LoginName text not null, Password text not null);";

        db.execSQL(CREATE_TABLE_USERS);

        ContentValues values = new ContentValues();
        values.put("LoginName", user_name);
        values.put("Password", password);
        db.insert("users", null, values);
          }
       catch(Exception e){
             }
        }
     public void show(){

    Toast toast=Toast.makeText(this, "your data is received", Toast.LENGTH_SHORT );
    toast.show();      
    }      
  }
CodeWithVikas
  • 1,105
  • 9
  • 33
Ashu
  • 21
  • 2

1 Answers1

0

I created a new DBAdapter class for the different function of the class like for open, close, update insert and retrieve data. Then I made another class to access the layout resources where I called the methods of DBAdapter class on button click events.

In the previous post of my code I found that every time when I insert the data into database I create a new database and that is I think create problem. Now I separate all the methods and call them individually.

Ashu
  • 21
  • 2