0

I have made an android program which is database program. It only has to fetch data from EditText and store it into SQLite database. But, problem is this that it fails to store data and gives up an error which one can only see in LOGCAT when program is running. Here is my code of DBHelper which helps in creating table:

public class DbHelper extends SQLiteOpenHelper {
static String DATABASE_NAME = "userdata";
public static final String TABLE_NAME = "user";
public static final String KEY_FNAME = "fname";
public static final String KEY_LNAME = "lname";
public static final String KEY_PSIZE = "psize";
public static final String KEY_PPRICE = "pprice";
public static final String KEY_ID = "id";

public DbHelper(Context context) {
    super(context, DATABASE_NAME, null, 2);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_TABLE = "CREATE TABLE "
            + TABLE_NAME + "("
            + KEY_ID + " INTEGER PRIMARY KEY, "
            + KEY_FNAME + " TEXT, "
            + KEY_LNAME + " TEXT, "
            + KEY_PSIZE + " NUMBER, "
            + KEY_PPRICE + " NUMBER);";
    db.execSQL(CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}}

This code does not display the database data in activity(where it is suppose to) but gives error as:

"table user has no column named psize (code 1)"

I am sure problem exists here but i am unable to find out.

Rajbir Shienh
  • 2,965
  • 1
  • 14
  • 21
  • Probably, you changed your table structure. If so, you need to increase the value of the **DATABASE_VERSION** (`where is it hidden in your code?`) constant. – Phantômaxx Apr 19 '15 at 18:38
  • 1
    http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run – laalto Apr 20 '15 at 06:38
  • 1
    @DerGolem NUMERIC is the default affinity as described in the documentation linked in the answer, and it doesn't have anything to do with OP's problem. – laalto Apr 20 '15 at 11:25
  • @Iaalto Sure. I missed `Otherwise, the affinity is NUMERIC.` – Phantômaxx Apr 20 '15 at 11:29

1 Answers1

0

I think Sqlite doesn't has NUMBER type of field. These are all fields that are supported:

INTEGER:

  • INT

  • INTEGER

  • TINYINT

  • SMALLINT

  • MEDIUMINT

  • BIGINT

  • UNSIGNED BIG INT

  • INT2

  • INT8

TEXT

  • CHARACTER(20)

  • VARCHAR(255)

  • VARYING CHARACTER(255)

  • NCHAR(55)

  • NATIVE CHARACTER(70)

  • NVARCHAR(100)

  • TEXT

  • CLOB

NONE

  • BLOB

  • no datatype specified

REAL

  • REAL

  • DOUBLE

  • DOUBLE PRECISION

  • FLOAT

NUMERIC - NUMERIC

  • DECIMAL(10,5)

  • BOOLEAN

  • DATE

  • DATETIME

Check this for more: https://www.sqlite.org/datatype3.html Try with integer and see is it a problem.

MilanNz
  • 1,323
  • 3
  • 12
  • 29
  • Thanks everyone, i just figure it out. There were 3 problems, Firstly, Of course NUMBER, it should be INTEGER. Secondly, The version of database was set to 2, whereas it was supposed to be version 1. and, Finally the space problems in Query of CREATE_TABLE. I fixed those all. Thanks everyone, it is now working :) – Rajbir Shienh Apr 24 '15 at 18:21