我在main_activity中创建一个sqlite数据库,然后在另一个名为dbSetup的java类中创建表。我将Database obj传递给dbSetup类,但是当我在手机上运行应用程序时。应用程序没有运行但显示错误说"遗憾的是应用程序已停止"
以下是我的main_Activity代码
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();
}
这是我的dbSetup类代码
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);");
}
}
这是Logcat
12-28 16:36:12.526 20116-20116/? D/dalvikvm: Late-enabling CheckJNI
12-28 16:36:13.059 20116-20116/com.example.bilalrafique.bloodbankmanagementsystem I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
12-28 16:36:13.059 20116-20116/com.example.bilalrafique.bloodbankmanagementsystem W/dalvikvm: VFY: unable to resolve virtual method 408: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-28 16:36:13.060 20116-20116/com.example.bilalrafique.bloodbankmanagementsystem D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
12-28 16:36:13.061 20116-20116/com.example.bilalrafique.bloodbankmanagementsystem I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
12-28 16:36:13.061 20116-20116/com.example.bilalrafique.bloodbankmanagementsystem W/dalvikvm: VFY: unable to resolve virtual method 430: Landroid/content/res/TypedArray;.getType (I)I
12-28 16:36:13.061 20116-20116/com.example.bilalrafique.bloodbankmanagementsystem D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
12-28 16:36:13.126 20116-20116/com.example.bilalrafique.bloodbankmanagementsystem E/SQLiteLog: (1) table BloodBank already exists
12-28 16:36:13.131 20116-20116/com.example.bilalrafique.bloodbankmanagementsystem D/AndroidRuntime: Shutting down VM
12-28 16:36:13.131 20116-20116/com.example.bilalrafique.bloodbankmanagementsystem W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x416b4d40)
12-28 16:36:13.163 20116-20116/com.example.bilalrafique.bloodbankmanagementsystem E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bilalrafique.bloodbankmanagementsystem, PID: 20116
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bilalrafique.bloodbankmanagementsystem/com.example.bilalrafique.bloodbankmanagementsystem.MainActivity}: android.database.sqlite.SQLiteException: table BloodBank already exists (code 1): , while compiling: CREATE TABLE BloodBank([Reg. No] INT PRIMARY KEY NOT NULL,Hospital VARCHAR(20),City VARCHAR(15));
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: table BloodBank already exists (code 1): , while compiling: CREATE TABLE BloodBank([Reg. No] INT PRIMARY KEY NOT NULL,Hospital VARCHAR(20),City VARCHAR(15));
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
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.executeSql(SQLiteDatabase.java:1672)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
at com.example.bilalrafique.bloodbankmanagementsystem.dbSetup.run(dbSetup.java:9)
at com.example.bilalrafique.bloodbankmanagementsystem.MainActivity.onCreate(MainActivity.java:14)
at android.app.Activity.performCreate(Activity.java:5248) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269) at android.app.ActivityThread.access$800(ActivityThread.java:139) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5102) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method)
12-28 16:36:15.563 20116-20116/com.example.bilalrafique.bloodbankmanagementsystem I/Process: Sending signal. PID: 20116 SIG: 9
答案 0 :(得分:2)
引起:android.database.sqlite.SQLiteException:表BloodBank 已存在(代码1):,同时编译:CREATE TABLE BloodBank([Reg.No] INT PRIMARY KEY NOT NULL,Hospital VARCHAR(20),City VARCHAR(15));
重命名表名,因为PID
然后卸载&amp;运行应用
您可以使用
table BloodBank already exists