应用程序启动并创建数据库。但是,当要创建表时,应用程序停止响应。错误信息: table Magazine没有名为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 :(得分: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的代码暂时已被注释掉。