-1

I have created one table in SQLite database by android for login for tracking and displaying I want to create another table in same database in SQLite by android studio

The register and login activity by user table are successfully run without any error or exception.But while creating second table staff it shows exception no such table found

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper
{
    public DatabaseHelper(Context context)
    {
        super(context, "Slab.db", null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL("Create table IF NOT EXISTS user(userid integer Primary Key AutoIncrement,username varchar,password varchar)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
            db.execSQL("drop table if exists user");
            onCreate(db);
    }


    public boolean insert(String username, String password)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put("username",username);
        contentValues.put("password",password);
        long ins=db.insert("user",null,contentValues);
        if (ins==-1)
            return false;
        else
            return true;
    }
    public boolean chkusername(String username)
    {
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.rawQuery("Select * from user where username=?",new String[]{username});
        if(cursor.getCount()>0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    public boolean chkusernamepassword(String username,String password)
    {
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.rawQuery("Select * from user where username=? and password=?",new String[]{username,password});
        if (cursor.getCount()>0)
        {
            return true;

        }
        else
        {
            return false;
        }



    }


Register.java

    b2.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            String s1=e1.getText().toString();
            String s2=e2.getText().toString();
            String s3=e3.getText().toString();
            if(s1.isEmpty()||s2.isEmpty())
            {
                Toast.makeText(getApplicationContext(), "Plese Enter All Details", Toast.LENGTH_SHORT).show();
            }
            else
            {
                if(s2.equals(s3))
                {
                    Boolean chkusername=db.chkusername(s1);
                    if(chkusername==true)
                    {
                        Boolean insert=db.insert(s1,s2);
                        if(insert==true)
                        {
                            Toast.makeText(getApplicationContext(),"Registration Successful", Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(RegisterActivity.this,MainActivity.class);
                            startActivity(intent);
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(),"Please Register Again",Toast.LENGTH_SHORT).show();
                        }
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(),"Username Already Exists",Toast.LENGTH_SHORT).show();
                    }
                }
                else
                {
                    Toast.makeText(getApplicationContext(),"Please Confirm Your Password", Toast.LENGTH_SHORT).show();
                }
            }
        }
    });

StaffdatabaseHelper.java

public StaffDatabaseHelper(Context context)
    {
        super(context,"Slab.db",null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL("Create table IF NOT EXISTS staff( staffid integer PRIMARY KEY AUTOINCREMENT,Name text,Email varchar,Phoneno integer,Companyname text,Salary integer,WorkingHours integer)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("drop table if exists staff");
        onCreate(db);
    }
    public boolean insert(String name,String email,String phoneno,String companyname,String salary,String wh)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put("Name",name);
        contentValues.put("Email",email);
        contentValues.put("Phoneno",phoneno);
        contentValues.put("Companyname",companyname);
        contentValues.put("Salary",salary);
        contentValues.put("WorkingHours",wh);
        long ins=db.insert("staff",null,contentValues);
        if (ins==-1)
            return  false;
        else
            return true;
    }
    public boolean chkemailandchkphoneno(String email,String phoneno)
    {
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.rawQuery("Select * from staff where Email=? and Phoneno=?",new String[]{email,phoneno});
       if(cursor.getCount()>0)
           return false;
       else
           return true;
    }

Staffform.java

staffDatabaseHelper=new StaffDatabaseHelper(this);
        b1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                String s1=e1.getText().toString();
                String s2=e2.getText().toString();
                String s3=e3.getText().toString();
                String s4=e4.getText().toString();
                String s5=e5.getText().toString();
                String s6=e6.getText().toString();
                if(s1.isEmpty()||s2.isEmpty()||s3.isEmpty()||s4.isEmpty()||s5.isEmpty()||s6.isEmpty())
                {
                    Toast.makeText(getApplicationContext(),"Please enter all details",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Boolean chkemailphone=staffDatabaseHelper.chkemailandchkphoneno(s2,s3);
                    if(chkemailphone==true)
                    {
                        Boolean insert=staffDatabaseHelper.insert(s1,s2,s3,s4,s5,s6);
                        if(insert==true)
                        {
                            Toast.makeText(getApplicationContext(),"Successfully added",Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(staffform.this,staffdetails.class);
                            startActivity(intent);
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(),"Ooops!Please Try Again",Toast.LENGTH_SHORT).show();
                        }
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(),"Staff member already exists",Toast.LENGTH_SHORT).show();

                    }
                }

            }
        });

I except the output of staffform is successfully added, but the the output is

E/SQLiteLog: (1) no such table: staff
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.slab, PID: 7639
    android.database.sqlite.SQLiteException: no such table: staff (code 1): , while compiling: Select * from staff where Email=? and Phoneno=?
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:501)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1392)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1331)
        at com.example.slab.StaffDatabaseHelper.chkemailandchkphoneno(StaffDatabaseHelper.java:46)
        at com.example.slab.staffform$1.onClick(staffform.java:39)
        at android.view.View.performClick(View.java:6304)
        at android.view.View$PerformClick.run(View.java:24803)
        at android.os.Handler.handleCallback(Handler.java:794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6635)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • You most likely renamed (or added) the table after a first run of your app. Uninstall and reinstall your app. – Phantômaxx Mar 24 '19 at 10:00
  • 1
    You are trying to create two databases with the same database name **Slab.db** once one is created that will be the one that is opened. Hence if **DatabaseHelper** is used and the database is accessed then when and attempt is made to use **StaffdatabaseHelper** the only tables that exist will be those from **DatabaseHelper** as **Slab.db** exists. You need to either use distinct databases names or combine the tables into one database helper. Typically the latter would be used. – MikeT Mar 24 '19 at 10:31
  • You may find [Should I create a class that inherits SQLiteOpenHelper for each table in my database?](https://stackoverflow.com/questions/55011476/should-i-create-a-class-that-inherits-sqliteopenhelper-for-each-table-in-my-data/55012700#55012700) useful. – MikeT Mar 24 '19 at 10:44

1 Answers1

1

Use DatabaseHelper calss to create and store every table throughout your app,android will allow only one db of a name you create e.g("my_db") for your app if you use SQLiteOpenHelper , thus remove the StaffDatabaseHelper class and do the things in DatabaseHelper class Add the table creation query in the OnCreate method of the DatabaseHelper class.

himel
  • 500
  • 5
  • 14
  • 1
    *android will allow only one db for your app if you use SQLiteOpenHelper* That's not the case. You can have multiple databases and multiple database helpers. The real issue is that the same database name and thus file has been used so the 2nd database helper opens the first used database and thus onCreate doesn't run and create the staff table, only the user table exists. – MikeT Mar 24 '19 at 10:38
  • 1
    P.S. [Should I create a class that inherits SQLiteOpenHelper for each table in my database?](https://stackoverflow.com/questions/55011476/should-i-create-a-class-that-inherits-sqliteopenhelper-for-each-table-in-my-data/55012700#55012700) demonstrates both multiple databases and helpers. – MikeT Mar 24 '19 at 10:50
  • yes , I've mistakenly said so , it's the same name, thanks for the info – himel Mar 24 '19 at 11:34