lundi 28 décembre 2015

SQLite database gives error in android

I'm creating an sqlite database in main_activity and then creating tables in another java class called dbSetup. I'm passing the Database obj to the dbSetup Class but when I run the application on my phone. Application does not run but shows an error saying "Unfortunately Application has stopped"

Following is my main_Activity code

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);
    SQLiteDatabase db=openOrCreateDatabase("BBMS",MODE_PRIVATE,null);
    dbSetup obj=new dbSetup(db);
    db.close();
}

Here is my dbSetup Class Code

public class dbSetup {
public dbSetup(SQLiteDatabase db){
    db.execSQL("CREATE TABLE BloodBank([Reg. No] INT PRIMARY KEY NOT NULL,Hospital VARCHAR(20),City VARCHAR(15));");
    db.execSQL("CREATE TABLE Blood([Blood Group] char(2) PRIMARY KEY NOT NULL,Cost int);");
    db.execSQL("CREATE TABLE Donor(Id INT PRIMARY KEY NOT NULL,Name VARCHAR(15),Age INT, Gender CHAR(1),[Blood Group] CHAR(2) FOREIGN KEY REFERENCES Blood.[Blood Group]);");
    db.execSQL("CREATE TABLE Recipient(Id INT PRIMARY KEY NOT NULL,Name VARCHAR(15),Age INT, Gender Char(1),[Blood Group] CHAR(2)) FOREIGN KEY REFERENCES Blood.[Blood Group];");
    db.execSQL("CREATE TABLE Employee(Id INT PRIMARY KEY NOT NULL,Name VARCHAR(15),Age INT,Gender Char(1),Salary INT,[Blood Group] CHAR(2)) FOREIGN KEY REFERENCES Blood.[Blood Group],[Reg. No] INT FOREIGN KEY REFERENCES BloodBank.[Reg. No]);");
    db.execSQL("CREATE TABLE Records([Blood Group] Char(2) FOREIGN KEY REFERENCES Blood.[Blood Group],[No of Bags] INT);");
    db.execSQL("CREATE TABLE DonationHistory([Donation No.] INT PRIMARY KEY NOT NULL,[Donor Id] FOREIGN KEY REFERENCES Donor.Id,[Blood Group] CHAR(2) FOREIGN KEY REFERENCES Blood.[Blood Group],[No of Bags] INT);");
    db.execSQL("CREATE TABLE SaleHistory([Sale No.] INT PRIMARY KEY NOT NULL, [Recipient Id] INT FOREIGN KEY REFERENCES Recipient.Id),[Blood Group] CHAR(2) FOREIGN KEY REFERENCES Blood.[Blood Group],[No of Bags] INT);");
}
}

Aucun commentaire:

Enregistrer un commentaire