android - Unfortunately, <appname> has stopped. Why is it happening? The complete code is given below -
i have written complete code in android studio. have used sqlite backend. app stops click on "add data" button. please help.
//databasehelper.java (file name) code
package com.example.android.sqlite; import android.content.contentvalues; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; /** * created lenovo on 14-04-2017. */ public class databasehelper extends sqliteopenhelper { public static final string database_name = "user.db"; public static final string table_name = "user_info.db"; public static final string col_1 = "id"; public static final string col_2 = "name"; public static final string col_3 = "surname"; public static final string col_4 = "marks"; public databasehelper(context context) { super(context, database_name, null, 1); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table" + table_name + ("id integer primary key autoincrement,name text,surname text, marks integer")); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists" + table_name); oncreate(db); } public boolean insertdata(string name, string surname, string marks) { sqlitedatabase db = this.getwritabledatabase(); contentvalues cv = new contentvalues(); cv.put(col_2, name); cv.put(col_3, surname); cv.put(col_4, marks); long result = db.insert(table_name, null, cv); if (result == -1) return false; else return true; } }
// mainactvity.java(file name) //code
package com.example.android.sqlite; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class mainactivity extends appcompatactivity { databasehelper my_db; edittext editn,edits,editm; button butt; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); my_db = new databasehelper(this); editn= (edittext)findviewbyid(r.id.text1); edits=(edittext)findviewbyid(r.id.text2); editm=(edittext)findviewbyid(r.id.edittext); butt=(button)findviewbyid(r.id.button); adddata(); } public void adddata(){ butt.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v){ boolean isinserted = my_db.insertdata(editn.gettext().tostring(),edits.gettext().tostring(),editm.gettext().tostring()); if(isinserted==true) toast.maketext(mainactivity.this,"data inserted",toast.length_long).show(); else toast.maketext(mainactivity.this,"data insertion failed",toast.length_long).show(); } } ); } }
your table_name = "user_info.db"
contains .
. seems not allowed: https://stackoverflow.com/a/3694305/4322687
also, in following line: db.execsql("create table" + table_name + ("id integer primary key autoincrement,name text,surname text, marks integer"));
second pair of brackets looks it's supposed inside quotation marks.
Comments
Post a Comment