android - Emulator stops responding, tables not created -
the application starts , database created. application stops responding when table created. error message: table magazine has no column named key_m_name. here code helper:
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 " + table_magazine + " values('times now',12,030112,45,40.50)"); db.execsql("insert " + table_magazine + " values('times finance',3,220410,52,50.00)"); db.execsql("insert " + table_author + " values(007,'ashwin m',040594,m,15)"); db.execsql("insert " + table_author + " values(002,'shino aburame',221296,m,4)"); db.execsql("insert " + table_author + " values(004,'percy shelley',210684,f,50)"); db.execsql("insert " + table_editor + " values(001,'lady tsunade',140883,f,10)"); db.execsql("insert " + table_editor + " values(005,'akshay k',150980,m,2)"); db.execsql("insert " + table_editor + " values(101,'o henry',181165,m,25)"); db.execsql("insert " + table_article + " values(003,220414,'endangered insects',002,001)"); db.execsql("insert " + table_article + " values(104,280390,'ode west wind',004,101)"); db.execsql("insert " + table_article + " values(81,140210,'road stardom',007,005)"); db.execsql("insert " + table_article + " values(81,140210,'hidden leaf village',002,001)"); db.execsql("insert " + table_category + " values('lifestyle',12)"); db.execsql("insert " + table_category + " values('news',14)"); db.execsql("insert " + table_category + " values('entertainment',21)"); db.execsql("insert " + table_category + " values('education',4)"); db.execsql("insert " + table_advertisement + " values('washing machine','ifb',5)"); db.execsql("insert " + table_advertisement + " values('3 bhk condo','prestige',11)"); db.execsql("insert " + table_advertisement + " values('r8 lemans','audi',8)"); db.execsql("insert " + table_distributor + " values(11,'advaith','bangalore',40)"); db.execsql("insert " + table_distributor + " values(05,'kakashi','tokyo',85)"); db.execsql("insert " + 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); } }
you need pull variable names holding column names out of string literals in primary key
declaration.
change this:
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))";
to
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 + "))";
there similar problems in other tables, too, though code runs sql has been commented out now.
Comments
Post a Comment