android - SQLite database errors with inputting data -
i've finished creating first database, think of syntax in oncreate
wrong. can't figure out however? i've tried looking @ tutorials , tried tailor code towards theirs, nothing seems working.
package com.example.bash1.sqlitediss; import android.content.contentvalues; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class databasehelper extends sqliteopenhelper { // database name public static final string database_stockdb = "stock.db"; // column names public static final string table_name = "stock_table"; public static final string col_1 = "id"; public static final string col_2 = "name"; public static final string col_3 = "datereceived"; public static final string col_4 = "expirydate"; public databasehelper(context context) { super(context, database_stockdb, null, 1); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table stock_table " + table_name + " (" + col_1 + "integer primary key,"+ col_2 + "integer,"+ col_3 + "integer," + col_4 +"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 datereceived, string expirydate){ sqlitedatabase db = this.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put(col_2, name); contentvalues.put(col_3, datereceived); contentvalues.put(col_4, expirydate); long result = db.insert(table_name,null,contentvalues); if (result == -1 ) return false; else return true; } }
the error follows:
error inserting name=sss datereceived=s expirydate=s`
android.database.sqlite.sqliteexception: table stock_table has no column named name (code 1): , while compiling: insert stock_table(name,datereceived,expirydate) values (?,?,?)
the problem create table command has no space between column name , type -
@override public void oncreate(sqlitedatabase db) { db.execsql("create table stock_table " + table_name + " (" + col_1 + "integer primary key,"+ col_2 + "integer,"+ col_3 + "integer," + col_4 +"integer" + ")"); }
that's why don't have column "name" "nameinteger". similar issue other columns too. also, inserting text values have integer type columns , change columns except primary key text type columns. change -
@override public void oncreate(sqlitedatabase db) { db.execsql("create table " + table_name + " (" + col_1 + " integer primary key,"+ col_2 + " text, "+ col_3 + " text, " + col_4 +" text" + ")"); }
Comments
Post a Comment