1

I want to create 2 tables: one is Registration Info and the second is Personal Info. I have created a database operation file separate from both tables, but when I run it in the app, I get this error:

 Info Database Created successfully
09-02 19:19:03.724 12697-12697/com.example.user.myapplication E/SQLiteLog: (1) no such table: personal_info
09-02 19:19:03.724 12697-12697/com.example.user.myapplication E/SQLiteDatabase: Error inserting cont=8755664455 addr=Sham Nagar reg_no=CS20161802
    android.database.sqlite.SQLiteException: no such table: personal_info (code 1): , while compiling: INSERT INTO personal_info(cont,addr,reg_no) VALUES (?,?,?)
       at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
       at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
       at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
       at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
       at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
       at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
       at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
       at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
       at com.example.user.myapplication.PersonalInfoDataOp.personalInfoOp(PersonalInfoDataOp.java:51)
       at com.example.user.myapplication.student_info$2.onClick(student_info.java:99)
       at android.view.View.performClick(View.java:5198)
       at android.view.View$PerformClick.run(View.java:21147)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5417)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-02 19:19:03.724 12697-12697/com.example.user.myapplication D/Database operations: Info One row inserted 

This is the Table Data class. I have mentioned all the table attributes here.

public TableData(){
}

public static abstract class TableInfo implements BaseColumns{
    public static final String FIRST_NAME = "first_name" ;
    public static final String LAST_NAME = "last_name" ;
    public static final String SIT_NO = "sit_number" ;
    public static final String REG_NO = "reg_number" ;
    public static final String DATABASE_NAME = "student_database" ;
    public static final String TABLE_NAME = "registration_info" ;
}

public static abstract class PersonalInfo implements BaseColumns{
    public static final String REGISTRATION_NO = "reg_no";
    public static final String ADDRESS = "addr";
    public static final String CONTACT = "cont";
    public static final String DATABASE_NAME = "student_database";
    public static final String TABLE_NAME = "personal_info";
}

Here I have created the Database operation class for Registration Info

public class DatabaseOperations extends SQLiteOpenHelper {

    public static final int database_version = 1;

    public String CREATE_QUERY = "CREATE TABLE " + TableData.TableInfo.TABLE_NAME+ "("
            + TableData.TableInfo.FIRST_NAME+ " TEXT ," + TableData.TableInfo.LAST_NAME + " TEXT ,"
            + TableData.TableInfo.SIT_NO + " TEXT ," + TableData.TableInfo.REG_NO + " TEXT);";

    public DatabaseOperations(Context context) {
        super(context, TableData.TableInfo.DATABASE_NAME, null, database_version);
        Log.d("Database operations", "Reg Database Created successfully");
    }

    @Override
    public void onCreate(SQLiteDatabase sdb) {

        sdb.execSQL(CREATE_QUERY);
        Log.d("Database operations", "Reg Table Created successfully");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void regInfoOperation(DatabaseOperations dop, String First_Name, String Last_Name, String Sit_Number, String Reg_Number)
    {
        SQLiteDatabase RSQ = dop.getWritableDatabase();
        ContentValues rcv = new ContentValues();

        rcv.put(TableData.TableInfo.FIRST_NAME,First_Name);
        rcv.put(TableData.TableInfo.LAST_NAME,Last_Name);
        rcv.put(TableData.TableInfo.SIT_NO,Sit_Number);
        rcv.put(TableData.TableInfo.REG_NO,Reg_Number);

        long k = RSQ.insert(TableData.TableInfo.TABLE_NAME, null, rcv);
        Log.d("Database operations", "Reg One row inserted");
    }

    public Cursor getRegInformation(DatabaseOperations dop, String regNo)
    {
        SQLiteDatabase db = dop.getReadableDatabase();
        String[] coloumns = {TableData.TableInfo.FIRST_NAME, TableData.TableInfo.LAST_NAME, TableData.TableInfo.REG_NO};
        String where = TableData.TableInfo.REG_NO +" LIKE ?";
        String args[] = {regNo};
        Cursor CR = db.query(TableData.TableInfo.TABLE_NAME,coloumns, where, args, null, null, null);
        return CR;
    }
}

Here Personal in operation class for PersonalInfoOp database

public class PersonalInfoDataOp extends SQLiteOpenHelper {

    public static final int database_version = 1;

    public String CREATE_QUERY = "CREATE TABLE " + TableData.PersonalInfo.TABLE_NAME + "( "
            + TableData.PersonalInfo.REGISTRATION_NO + " TEXT,"
            + TableData.PersonalInfo.ADDRESS + " TEXT,"
            + TableData.PersonalInfo.CONTACT + " TEXT);";

    public PersonalInfoDataOp(Context context) {
        super(context, TableData.PersonalInfo.DATABASE_NAME, null, database_version);
        Log.d("Database operations", " Info Database Created successfully");
    }

    @Override
    public void onCreate(SQLiteDatabase dob) {
       try {
           dob.execSQL(CREATE_QUERY);
           Log.d("Database operations", "Personal info Table Created successfully");
       }
       catch (Exception e){
           Log.d("Database operations","Personal info Fail to create");
       }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void personalInfoOp(PersonalInfoDataOp dop, String Registration_no, String Addr, String Cont)
    {
        SQLiteDatabase PSQ = dop.getWritableDatabase();
        ContentValues pcv = new ContentValues();

        pcv.put(TableData.PersonalInfo.REGISTRATION_NO, Registration_no);
        pcv.put(TableData.PersonalInfo.ADDRESS,Addr);
        pcv.put(TableData.PersonalInfo.CONTACT, Cont);

        long k = PSQ.insert(TableData.PersonalInfo.TABLE_NAME, null, pcv);
        Log.d("Database operations", "Info One row inserted");
    }
}

And this one is the Student Info class. When I click on the next button, an error appears in logCaT.

Next.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent1 = new Intent(student_info.this,info.class);
        add = Address.getText().toString();
        con = Contact.getText().toString();

        PersonalInfoDataOp POP = new PersonalInfoDataOp(popc);
        POP.personalInfoOp(POP,RegistrationNum,add,con);

        startActivity(intent1);
        Toast.makeText(getBaseContext(),"Personal Info inserted......", Toast.LENGTH_LONG).show();

    }
});
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Rahul Waghmare
  • 68
  • 1
  • 10
  • And why `on: no such table: personal_info ` is not enought for you? – Antoniossss Sep 03 '16 at 01:32
  • when i click in my first activity the registration info table is created in database but when i click on next button in second activity the personal info table is not create it gives me error – Rahul Waghmare Sep 03 '16 at 04:15
  • I need that table to store students personal info in databse – Rahul Waghmare Sep 03 '16 at 04:16
  • Use android database manager helper class.. And read what's happening inside in Android...google it for that class you'll find it's enough debug what's happening – EngineSense Sep 04 '16 at 14:17

1 Answers1

0

Both your SQLiteOpenHelpers have the same database file name: student_database. SQLiteOpenHelper versions database files, not tables, so in the second helper nothing is done since the database file is already at its correct version.

To fix it,

  • Move both tables to the same SQLiteOpenHelper
  • Remove the try-catch in onCreate(). Exceptions need to get thrown out on error.
  • Uninstall your app to remove the old database file and to get onCreate() to execute again.

See When is SQLiteOpenHelper onCreate() / onUpgrade() run? for more.

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303