Click here to Skip to main content
15,881,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I Want to create a login application..
Now in that application first time when i am login it will check username is empty ..if username is empty it will call databasebuild(),tablebuild(),and then insert into that table value..and username is already exists then it will directly call the next page i.e Galaxy_Main..From that device imei number if imei number is same as first
Now in my program i am create all this but still it has errors..

What I have tried:

public class MainActivity extends AppCompatActivity
{
    private static final String TAG = null;
    Button login;
    DatabaseHandler sqLiteHelper;

    SQLiteDatabase sqLiteDatabaseObj;
    String imeistr;

    private TextInputLayout userview ;
    private TextInputLayout passview ;
    private String SQLiteDataBaseQueryHolder;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sqLiteHelper = new DatabaseHandler(this);

        login = (Button) findViewById(R.id.button1);
        userview = (TextInputLayout) findViewById(R.id.Username);
        passview = (TextInputLayout) findViewById(R.id.Password);
        passview = (TextInputLayout) findViewById(R.id.Password);

        login.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                Cursor c = sqLiteDatabaseObj.rawQuery( "select * from LOGINUSER", null );
                c.moveToNext();

                String username = userview.getEditText().getText().toString();
                String password = passview.getEditText().getText().toString();

                if (username.isEmpty()) 
                {
                    SQLiteDataBaseBuild();
                    // Creating SQLite table if dose n't exists.
                    SQLiteTableBuild();
                    // SQLite query to insert data into table.
                    ContentValues cv = new ContentValues();
                    cv.put(sqLiteHelper.KEY_User, username);
                    cv.put(sqLiteHelper.KEY_PASS, password);

                    try 
                    {
                        // Executing query.
                        sqLiteDatabaseObj.beginTransaction();
                        sqLiteDatabaseObj.insert(sqLiteHelper.TABLE_NAME, null, cv);
                        sqLiteDatabaseObj.setTransactionSuccessful();
                        sqLiteDatabaseObj.endTransaction();

                        // Closing SQLite database object.
                        sqLiteDatabaseObj.close();
                    } 
                    catch (SQLException e) 
                    {
                        android.util.Log.e(TAG, e.getMessage());
                    }
                }

                Toast.makeText(MainActivity.this, "User Login Successfully", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(MainActivity.this, Galaxy_Main.class);
                startActivity(intent);
                }
            }
        });
    }        

    // SQLite database build method.
    public void SQLiteDataBaseBuild() 
    {
        sqLiteDatabaseObj = openOrCreateDatabase(DatabaseHandler.DATABASE_NAME, Context.MODE_PRIVATE, null);
    }

    // SQLite table build method.
    public void SQLiteTableBuild() 
    {
        sqLiteDatabaseObj.execSQL("CREATE TABLE IF NOT EXISTS " + DatabaseHandler.TABLE_NAME + "(" + DatabaseHandler.KEY_User + " PRIMARY KEY , " + DatabaseHandler.KEY_PASS + " STRING);");
    }
}

//DatabaseHandler.class

package com.example.mytrialapplication;

public class DatabaseHandler extends SQLiteOpenHelper 
{
    static final String DATABASE_NAME = "Galaxy";
    public static final String TABLE_NAME = "LOGINUSER";

    public static final String KEY_User = "username";
    public static final String KEY_PASS = "password";
    public static final String KEY_IMEI_NO = "imei_number";

    public DatabaseHandler(Context context)
    {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        db.execSQL(SQL_CREATE_ENTRIES);
    }

    public static final String SQL_CREATE_ENTRIES =
            "CREATE TABLE " + TABLE_NAME + " (" +KEY_User+ "STRING , "+
                    KEY_PASS +" STRING,"+ KEY_IMEI_NO + "STRING"+ " )";

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

        // Create tables again
        onCreate(db);
    }
}
Posted
Updated 10-Jan-21 8:32am
v3
Comments
David Crow 10-Jan-21 14:42pm    
"if username is empty it will call databasebuild(),tablebuild(),and then insert into that table value..."

Since sqLiteDatabaseObj.insert() is called if username is empty, you are adding empty fields/columns to the table. Is that intentional?

"...and username is already exists then it will directly call the next page i.e Galaxy_Main."

Regardless of whether username is empty or not, you can starting the Galaxy_Main activity.

You are calling rawQuery() but are not using it for anything? Should that code be removed?

"...but still it has errors."

And those errors would be what exactly? Have you stepped through the code using the debugger? If not, why?
Member 15038801 10-Jan-21 23:40pm    
sir i want to insert for first time username and password form user of that application and second time if the same user is there it will directly call the Galaxy_Main so what change i do in the application?
David Crow 11-Jan-21 9:08am    
Study this:
@Override
public void onClick(View v) 
{
    String username = userview.getEditText().getText().toString();
    Cursor c = sqLiteDatabaseObj.rawQuery("SELECT username FROM LOGINUSER WHERE username = ?", new String[] {username});
    if (c.getCount() == 0)
    {
        SQLiteDataBaseBuild();
        SQLiteTableBuild();

        String password = passview.getEditText().getText().toString();

        ContentValues cv = new ContentValues();
        cv.put(sqLiteHelper.KEY_User, username);
        cv.put(sqLiteHelper.KEY_PASS, password);

        try 
        {
            sqLiteDatabaseObj.beginTransaction();
            sqLiteDatabaseObj.insert(sqLiteHelper.TABLE_NAME, null, cv);
            sqLiteDatabaseObj.setTransactionSuccessful();
            sqLiteDatabaseObj.endTransaction();
            sqLiteDatabaseObj.close();
        } 
        catch (SQLException e) 
        {
            android.util.Log.e(TAG, e.getMessage());
        }              
    }
    else
    {
        Toast.makeText(MainActivity.this, "User Login Successfully", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(MainActivity.this, Galaxy_Main.class);
        startActivity(intent);
    }
}
Member 15038801 12-Jan-21 0:53am    
ok sir i will study this...
Member 15038801 11-Jan-21 0:03am    
It is not intentional also ...i am new in this ...but i am trying to this ....but what you think sir there is some updations?

1 solution

You already posted this question at Creation of android application[^] and received a solution. Please do not repost.
 
Share this answer
 
Comments
Member 15038801 9-Jan-21 5:38am    
sir in that problem i have different issue..and in this different so please help to sort out ..
Richard MacCutchan 9-Jan-21 5:44am    
Start by formatting your code properly by putting <pre> tags around it so it is readable. Then explain exactly what the problem is and where it occurs.
Member 15038801 9-Jan-21 6:59am    
I am giving pre tag please check it...
Richard MacCutchan 9-Jan-21 7:23am    
Why have you done it in the middle instead of all the code?
Member 15038801 10-Jan-21 23:56pm    
I think...sir that section has errors..my code is something wrong in that section..?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900