2016-09-02 3 views
1

Ich möchte 2 Tabellen erstellen: eine ist Registrierungsinformationen und die zweite ist Personal Info. Ich habe eine Datenbankoperation Datei getrennt von den beiden Tabellen erstellt, aber wenn ich es in der App laufen, bekomme ich diesen Fehler:Fehler beim Erstellen von Tabellen mit SQLite

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 

Dies ist die Tabelle Datenklasse. Ich habe hier alle Tabellenattribute erwähnt.

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"; 
} 

Hier habe ich die Datenbankbetrieb Klasse für 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; 
    } 
} 
erstellt

Hier Personal in Betrieb Klasse für PersonalInfoOp Datenbank

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"); 
    } 
} 

Und dies ist die Klasse Schüler Info. Wenn ich auf die nächste Schaltfläche klicke, erscheint ein Fehler 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(); 

    } 
}); 
+0

Und warum 'on: keine solche Tabelle: personal_info' reicht Ihnen nicht? – Antoniossss

+0

Wenn ich in meine erste Aktivität klicke, wird die Registrierungs-Infotabelle in der Datenbank erstellt, aber wenn ich auf die nächste Schaltfläche in der zweiten Aktivität klicke, wird die persönliche Infotabelle nicht erstellt. –

+0

Ich brauche diese Tabelle, um die persönlichen Informationen der Schüler in der Datenbank –

Antwort

0

Sowohl Ihre SQLiteOpenHelper s die gleiche Datenbank-Dateiname: student_database. SQLiteOpenHelper Versionen Datenbank-Dateien, nicht Tabellen, so in der zweiten Helfer nichts getan wird, da die Datenbank-Datei ist bereits in der richtigen Version. catch in onCreate() -

es zu beheben,

  • bewegen sich beide Tabellen auf den gleichen SQLiteOpenHelper
  • die try entfernen. Ausnahmen müssen bei einem Fehler rausgeworfen werden.
  • Deinstallieren Sie Ihre App, um die alte Datenbankdatei zu entfernen und onCreate() erneut auszuführen.

Weitere Informationen finden Sie unter When is SQLiteOpenHelper onCreate()/onUpgrade() run?.

Verwandte Themen