SQLite cannot create database in OnCreate method. E/SQLiteLog: (1)

Question

For my application I am working with SQLite database.

I followed a tutorial for implementing the database and it worked. But then I noticed something.. I missed an extra table.

When I tried to add another table it gave me an error so I tried alot of different solutions, but nothing works here. The following that I tried is:

  • Increase version number for onUpgrade method
  • Drop database trough drop statement
  • Create new database file
  • Create new database table
  • Deleted application and files
  • Accessed the device monitor to locate .DB file

All these solutions did not work for me.

Now the fun part is when I deleted my application and tried all the other solutions above. I cannot even create my first working database table anymore. It is as if my whole database has broken.

I don't know what to do know. I don't value my data in my database. I would just like to start with a clean and fresh database.

The code that I am using to implement the database, that worked before troubleshooting, is:

    public class Database extends SQLiteOpenHelper{

        // Fields
        private final static int DATABASE_VERSION = 1;
        // Table products
        private final static String DATABASE_NAME = "product.db";
        public static final String TABLE_PRODUCTS = "products";
        public static final String ID = "_id";
        public static final String NAME = "name";
        public static final String CALORIES = "calories";
        public static final String PRODUCT_TYPE = "producttype";
        public static final String CATEGORY_TYPE = "categorytype";
        public static final String IMAGE = "image";

        // Table Calories
        /* Second table
        public static final String TABLE_CALORIES = "calories";
        public static final String CALID = "_id";
        public static final String AMOUNT_OF_CALORIES = "amountOfcalories";
        public static final String DAY_OF_THE_WEEK = "dayoftheweek";
        */
        // Table product fields
        private String prodID = null;
        private String prodname = null;
        private String prodcalories = null;
        private String prodproducttype = null;
        private String prodcategorytype = null;
        private String prodimage = null;
        private String totalCalories = null;
        private ProductType tempProductType = null;
        private ProductType newProductType = null;
        private CategoryType tempCatType = null;
        private CategoryType newCategoryType = null;
        private SQLiteDatabase database;

        // Table Calories fields
        private String calID = null;
        private String calTotalCalories = null;

        public Database(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, DATABASE_NAME, factory, DATABASE_VERSION);
            System.out.println("DATABASE PATH: " + context.getDatabasePath("product.db"));
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            System.out.println(db.isOpen());

            String create_table_products = "create table " + TABLE_PRODUCTS + "(" +
                    ID + " integer primary key auto increment " +
                    NAME + " text " +
                    CALORIES + " text " +
                    PRODUCT_TYPE + " text " +
                    CATEGORY_TYPE + " text " +
                    IMAGE+ " text " +
                    ");";
            db.execSQL(create_table_products);

            /* Second table
            String create_table_calories = "create table " + TABLE_CALORIES + "(" +
                    CALID + " integer primary key auto increment " +
                    AMOUNT_OF_CALORIES + " text " +
                    DAY_OF_THE_WEEK + " text " +
                    ");";
            db.execSQL(create_table_calories);
            */
        }

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

    }

Momentarily the code stops in the OnCreate() to create the first table. It gives me a vague E/SQLite: (1) error. So basically If I cannot create the table I would like to begin fresh, but I have no idea how as I tried everything to get rid of the old one.

Thanks in advance


Show source
| java   | android   | database   | sqlite   | android-sqlite   2017-01-01 13:01 2 Answers

Answers ( 2 )

  1. 2017-01-01 13:01

    You've forgotten commas ',' in the create table query.

    Syntax should be

    CREATE TABLE mytable (
    col1 int,
    col2 text);
    

    Edit: Would have commented but don't have enough rep.

  2. 2017-01-01 14:01

    Try to replace

    String create_table_products = "create table " + TABLE_PRODUCTS + "(" +
                    ID + " integer primary key auto increment " +
                    NAME + " text " +
                    CALORIES + " text " +
                    PRODUCT_TYPE + " text " +
                    CATEGORY_TYPE + " text " +
                    IMAGE+ " text " +
                    ");";
    

    to

     String create_table_products = "create table " + TABLE_PRODUCTS + "(" +
                    ID + " integer primary key autoincrement, " +
                    NAME + " text, " +
                    CALORIES + " text, " +
                    PRODUCT_TYPE + " text, " +
                    CATEGORY_TYPE + " text, " +
                    IMAGE+ " text " +
                    ");";
    
◀ Go back