2014-04-23 47 views
0

應用程序啓動並創建數據庫。但是,當創建表時,應用程序停止響應。錯誤消息: 表格雜誌沒有名爲KEY_M_NAME的列。這裏是我的助手代碼:模擬器停止響應,表格未創建

import com.example.projectdbms.model.Magazine; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class Helper extends SQLiteOpenHelper 
{ 

    // LogCat tag 
    private static final String LOG = "Helper"; 

    // Database Version 
    private static final int DATABASE_VERSION = 1; 

    // Database Name 
    private static final String DATABASE_NAME = "Magazine Publishers Db"; 

    // Table Names 
    private static final String TABLE_MAGAZINE = "Magazine"; 
    private static final String TABLE_ARTICLE = "Article"; 
    private static final String TABLE_EDITOR = "Editor"; 
    private static final String TABLE_AUTHOR = "Author"; 
    private static final String TABLE_CATEGORY = "Category"; 
    private static final String TABLE_DISTRIBUTOR = "Distributor"; 
    private static final String TABLE_ADVERTISEMENT = "Advertisement"; 

    // Common column names 


    //Distributor Table - column names 
    private static final String KEY_D_NO = "D_no"; 
    private static final String KEY_DNAME = "Name"; 
    private static final String KEY_CITY = "City"; 
    private static final String KEY_SALES = "Sales"; 

    //Author Table - column names 
    private static final String KEY_A_ID = "A_id"; 
    private static final String KEY_A_NAME = "Name"; 
    private static final String KEY_A_DOB = "D.O.B."; 
    private static final String KEY_A_SEX = "Sex"; 
    private static final String KEY_A_ARTICLES = "No of articles"; 

    //Editor Table - column names 
    private static final String KEY_E_ID = "E_id"; 
    private static final String KEY_E_NAME = "Name"; 
    private static final String KEY_E_DOB = "D.O.B."; 
    private static final String KEY_E_SEX = "Sex"; 
    private static final String KEY_E_ARTICLES = "No of articles"; 

    //Article Table - column names 
    private static final String KEY_AR_NO = "Ar_id"; 
    private static final String KEY_AR_DATE = "Date"; 
    private static final String KEY_AR_TITLE = "Title"; 

    //Magazine Table - column names 
    private static final String KEY_M_NAME = "Name"; 
    private static final String KEY_EDITION = "Edition"; 
    private static final String KEY_M_DATE = "Date"; 
    private static final String KEY_PAGES = "Pages"; 
    private static final String KEY_PRICE = "Price"; 

    //Advertisement Table - column names 
    private static final String KEY_PNAME = "Product/Service"; 
    private static final String KEY_BRAND = "Brand"; 
    private static final String KEY_SIZE = "Size"; 

    //Category Table - column names 
    private static final String KEY_CNAME = "Category"; 
    private static final String KEY_C_ARTICLES = "No of articles"; 

    //Table Create Statements 

    //Magazine table create statement 
    private static final String CREATE_TABLE_MAGAZINE = "CREATE TABLE " 
    + TABLE_MAGAZINE + "(" + KEY_M_NAME + " TEXT NOT NULL, " + KEY_EDITION 
    + " INTEGER, " + KEY_M_DATE + " INTEGER," + KEY_PAGES + " INTEGER NOT NULL, " 
    + KEY_PRICE + " REAL, PRIMARY KEY(KEY_M_NAME, KEY_M_DATE))"; 

    //Editor table  
    private static final String CREATE_TABLE_EDITOR = "CREATE TABLE " 
    + TABLE_EDITOR + "(" + KEY_E_ID + " INTEGER NOT NULL, " + KEY_E_NAME + " TEXT NOT NULL," 
    + KEY_E_DOB + " INTEGER, " + KEY_E_SEX + " TEXT," + KEY_E_ARTICLES 
    + " INTEGER, PRIMARY KEY(KEY_E_ID))"; 

    //Author table 
    private static final String CREATE_TABLE_AUTHOR = "CREATE TABLE " 
    + TABLE_AUTHOR + "(" + KEY_A_ID + " INTEGER NOT NULL, " + KEY_A_NAME + " TEXT NOT NULL, " 
    + KEY_A_DOB + " INTEGER, " + KEY_A_SEX + " TEXT," + KEY_A_ARTICLES 
    + " INTEGER, PRIMARY KEY(KEY_A_ID))"; 

    //Articles table 
    private static final String CREATE_TABLE_ARTICLE = "CREATE TABLE " 
    + TABLE_ARTICLE + "(" + KEY_AR_NO + " INTEGER NOT NULL, " + KEY_AR_DATE 
    + " INTEGER," + KEY_AR_TITLE + " TEXT NOT NULL, " + KEY_A_ID + " INTEGER, " 
    + KEY_E_ID + "INTEGER, PRIMARY KEY(KEY_AR_NO)" 
    + "FOREIGN KEY (A_ID) REFERENCES AUTHOR(A_ID) ON DELETE CASCADE ON UPDATE CASCADE," + 
    "FOREIGN KEY (E_ID) REFERENCES EDITOR(E_ID) ON DELETE CASCADE ON UPDATE CASCADE)"; 

    //Category table        
    private static final String CREATE_TABLE_CATEGORY = "CREATE TABLE " 
    + TABLE_CATEGORY + "(" + KEY_CNAME + " TEXT NOT NULL," + KEY_C_ARTICLES 
    + " INTEGER, PRIMARY KEY(KEY_CNAME))"; 

    //Advertisement table 
    private static final String CREATE_TABLE_ADVERTISEMENT = "CREATE TABLE " 
    + TABLE_ADVERTISEMENT + "(" + KEY_PNAME + " TEXT NOT NULL, " + KEY_BRAND 
    + " TEXT NOT NULL, " + KEY_SIZE + " INTEGER, PRIMARY KEY(KEY_PNAME,KEY_BRAND))"; 

    //Distributor table 
    private static final String CREATE_TABLE_DISTIBUTOR = "CREATE TABLE " 
    + TABLE_DISTRIBUTOR + "(" + KEY_D_NO + " INTEGER NOT NULL, " + KEY_DNAME 
    + " TEXT NOT NULL, " + KEY_CITY + " TEXT, " + KEY_SALES + " INTEGER, PRIMARY KEY(KEY_D_NO))"; 


    public Helper(Context context) 
    { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     //(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) 
    { 
     // creating required tables 
     db.execSQL(CREATE_TABLE_MAGAZINE); 
     /*db.execSQL(CREATE_TABLE_EDITOR); 
     db.execSQL(CREATE_TABLE_AUTHOR); 
     db.execSQL(CREATE_TABLE_ARTICLE); 
     db.execSQL(CREATE_TABLE_CATEGORY); 
     db.execSQL(CREATE_TABLE_ADVERTISEMENT); 
     db.execSQL(CREATE_TABLE_DISTIBUTOR);*/ 
     //ins(db); 
    } 

    public void addMagazine(Magazine magazine) { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(KEY_M_NAME, magazine.getTitle()); 
     values.put(KEY_EDITION, magazine.getEdition()); 
     values.put(KEY_M_DATE, magazine.getDate()); 
     values.put(KEY_PAGES, magazine.getPages()); 
     values.put(KEY_PRICE, magazine.getPrice()); 
     // Inserting Row 
     db.insert(TABLE_MAGAZINE, null, values); 
     db.close(); // Closing database connection 
    } 

    public void ins(SQLiteDatabase db) 
    { 
     db.execSQL("INSERT INTO " + TABLE_MAGAZINE + " VALUES('Times Now',12,030112,45,40.50)"); 
     db.execSQL("INSERT INTO " + TABLE_MAGAZINE + " VALUES('Times Finance',3,220410,52,50.00)"); 
     db.execSQL("INSERT INTO " + TABLE_AUTHOR + " VALUES(007,'Ashwin M',040594,M,15)"); 
     db.execSQL("INSERT INTO " + TABLE_AUTHOR + " VALUES(002,'Shino Aburame',221296,M,4)"); 
     db.execSQL("INSERT INTO " + TABLE_AUTHOR + " VALUES(004,'Percy Shelley',210684,F,50)"); 
     db.execSQL("INSERT INTO " + TABLE_EDITOR + " VALUES(001,'Lady Tsunade',140883,F,10)"); 
     db.execSQL("INSERT INTO " + TABLE_EDITOR + " VALUES(005,'Akshay K',150980,M,2)"); 
     db.execSQL("INSERT INTO " + TABLE_EDITOR + " VALUES(101,'O Henry',181165,M,25)"); 
     db.execSQL("INSERT INTO " + TABLE_ARTICLE + " VALUES(003,220414,'Endangered Insects',002,001)"); 
     db.execSQL("INSERT INTO " + TABLE_ARTICLE + " VALUES(104,280390,'Ode To The West Wind',004,101)"); 
     db.execSQL("INSERT INTO " + TABLE_ARTICLE + " VALUES(81,140210,'Road To Stardom',007,005)"); 
     db.execSQL("INSERT INTO " + TABLE_ARTICLE + " VALUES(81,140210,'Hidden Leaf Village',002,001)"); 
     db.execSQL("INSERT INTO " + TABLE_CATEGORY + " VALUES('Lifestyle',12)"); 
     db.execSQL("INSERT INTO " + TABLE_CATEGORY + " VALUES('News',14)"); 
     db.execSQL("INSERT INTO " + TABLE_CATEGORY + " VALUES('Entertainment',21)"); 
     db.execSQL("INSERT INTO " + TABLE_CATEGORY + " VALUES('Education',4)"); 
     db.execSQL("INSERT INTO " + TABLE_ADVERTISEMENT + " VALUES('Washing Machine','IFB',5)"); 
     db.execSQL("INSERT INTO " + TABLE_ADVERTISEMENT + " VALUES('3 BHK Condo','Prestige',11)"); 
     db.execSQL("INSERT INTO " + TABLE_ADVERTISEMENT + " VALUES('R8 LeMans','Audi',8)"); 
     db.execSQL("INSERT INTO " + TABLE_DISTRIBUTOR + " VALUES(11,'Advaith','Bangalore',40)"); 
     db.execSQL("INSERT INTO " + TABLE_DISTRIBUTOR + " VALUES(05,'Kakashi','Tokyo',85)"); 
     db.execSQL("INSERT INTO " + TABLE_DISTRIBUTOR + " VALUES(03,'Penguin','New York',120)"); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    { 
     //on upgrade drop older tables 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_MAGAZINE); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_ARTICLE); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_EDITOR); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_AUTHOR); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_CATEGORY); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_DISTRIBUTOR); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_ADVERTISEMENT); 

     //create new tables 
     onCreate(db); 
    } 
} 
+0

一個小建議,把你的查詢放在一個原始文本文件中,並將它們讀入一個字符串數組中。管理和關閉android更容易。 – Olayinka

回答

0

你需要拉牽着你列名出字符串字面量在PRIMARY KEY聲明變量名。

更改此:

private static final String CREATE_TABLE_MAGAZINE = "CREATE TABLE " 
+ TABLE_MAGAZINE + "(" + KEY_M_NAME + " TEXT NOT NULL, " + KEY_EDITION 
+ " INTEGER, " + KEY_M_DATE + " INTEGER," + KEY_PAGES + " INTEGER NOT NULL, " 
+ KEY_PRICE + " REAL, PRIMARY KEY(KEY_M_NAME, KEY_M_DATE))"; 

private static final String CREATE_TABLE_MAGAZINE = "CREATE TABLE " 
+ TABLE_MAGAZINE + "(" + KEY_M_NAME + " TEXT NOT NULL, " + KEY_EDITION 
+ " INTEGER, " + KEY_M_DATE + " INTEGER," + KEY_PAGES + " INTEGER NOT NULL, " 
+ KEY_PRICE + " REAL, PRIMARY KEY(" + KEY_M_NAME + "," + KEY_M_DATE + "))"; 

有在其他表類似的問題也一樣,雖然在運行SQL代碼已被註釋掉現在。