2

Actually the code works well when I created the database, my first table(table_userprofile) and its related fields.

But as soon as I created another table(table_bloodgroup) under onCreate() method in my Helper Class.

It started to show 2nd table not created..and lots of exception popped up related to database being not created.

Following is the CODE:

package com.example.databaseexample;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {

    // All Static variables

    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "bloodDonation";

    // Contacts table name
    private static final String TABLE_USER="table_userprofile";
    private static final String TABLE_BLOODGROUP="table_bloodgroup";

    //UserProfile Table Columns names
    private static final String KEY_ID="id";
    private static final String KEY_NAME="name";
    private static final String KEY_PH_NO="phone_number";

    private static final String KEY_BGID="id";
    private static final String KEY_BNAME="bloodgroup_name";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        context.deleteDatabase(DATABASE_NAME);
        // TODO Auto-generated constructor stub
    }
    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String CREATE_UserProfile_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_USER + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_PH_NO + " TEXT" + ")";
        db.execSQL(CREATE_UserProfile_TABLE);

        String createTable_BLOODGROUP="CREATE TABLE IF NOT EXISTS " + TABLE_BLOODGROUP + "( " +
                KEY_BGID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
                KEY_BNAME + " VARCHAR(30)); ";
        db.execSQL(createTable_BLOODGROUP);

        Log.d("Created", "Created Both the Tables");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        //db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
        //db.execSQL("DROP TABLE IF EXISTS " + TABLE_BLOODGROUP);
        // Create tables again
        //onCreate(db);
    }


    // **************** Add New UserProfile Entry **********************

    void addUser(UserProfile user){
        SQLiteDatabase db=this.getWritableDatabase();

        ContentValues values=new ContentValues();
        values.put(KEY_NAME, user.get_name());
        values.put(KEY_PH_NO, user.get_phone_number());

        // Inserting Rows
        db.insertOrThrow(TABLE_USER, null, values);
        db.close();
    }



    // *****  Retrieve all UserProfile Entry *******

    public List<UserProfile> getAllUser(){
        List <UserProfile> userList=new ArrayList<UserProfile>();

        // Select All Query
        String selectQuery="SELECT * FROM " + TABLE_USER;

        SQLiteDatabase db=this.getReadableDatabase();
        Cursor c=db.rawQuery(selectQuery, null);

        // looping through all rows and adding to the list
        if(c.moveToFirst()){
            do{
                UserProfile user=new UserProfile();
                user.set_id(Integer.parseInt(c.getString(0)));
                user.set_name(c.getString(1));
                user.set_phone_number(c.getString(2));

                // Adding user to the list
                userList.add(user);
            }while(c.moveToNext());
        }
        c.close();
        return userList;
    }


    // **************** Add BloodGroup Entry **********************

        void addBloodGroup(BloodGroup group){
            try{
                    SQLiteDatabase db=this.getWritableDatabase();

                    ContentValues values=new ContentValues();
                    values.put(KEY_BNAME, group.get_name());

                    //  Inserting Rows
                    db.insertOrThrow(TABLE_BLOODGROUP, null, values);
                    db.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }   

        // *****  Retrieve all BloodGroups Entry *******

        public List<BloodGroup> getAllGroups(){
            List <BloodGroup> groupList=new ArrayList<BloodGroup>();
            try{


                //  Select All Query
                String selectQuery="SELECT * FROM " + TABLE_BLOODGROUP;

                SQLiteDatabase db=this.getReadableDatabase();
                Cursor c=db.rawQuery(selectQuery, null);

                //  looping through all rows and adding to the list
                if(c.moveToFirst()){
                    do{
                        BloodGroup grp=new BloodGroup();
                        grp.set_id(Integer.parseInt(c.getString(0)));
                        grp.set_name(c.getString(1));

                        // Adding user to the list
                        groupList.add(grp);
                    }while(c.moveToNext());
                }
                c.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            return groupList;
        }
}

LOG CAT ERROR:

 08-20 08:36:30.181: I/Database(736): sqlite returned: error code = 1, msg = table table_bloodgroup has no column named bgname
 08-20 08:36:30.200: E/Database(736): Error inserting bgname=O
 08-20 08:36:30.200: E/Database(736): android.database.sqlite.SQLiteException: table table_bloodgroup has no column named bgname: , while compiling: INSERT INTO table_bloodgroup(bgname) VALUES(?);
2
  • I have mentioned the log cat error below the code.. Kindly check it. Thanks. Commented Aug 20, 2012 at 3:17
  • Try checking the table's column name in the database. Commented Aug 20, 2012 at 11:48

2 Answers 2

16

try this one.... Change database version to 2.

private static final int DATABASE_ VERSION = 2;

Sign up to request clarification or add additional context in comments.

5 Comments

OK. you mean that I should remove DATABASE_VERSION=1 and now create newer VERSION.. OK Let me try it.
Bro it worked.. Instead of changing the DATABASE_VERSION.. i changed the database name and it ran successfully.. thanks for the effort. REGARDS :)
i had the same problem sometimes before, but it get solved when i changed the db vesion. Thats why i suggest you that. Ok if the info. Was helpful for you then vote me up
I can't believe such a simple change was the solution to the problems I was having. I was literally going INSANE with this ridiculous problem here.. I'm glad you managed to resolve it because now everything works for me! Thanks :)
Surely a strange solution here, but working. Does someone know why this is solving our issues?
0

Changing the version number create a complete new database schemas including your new table. However, you can override onUpgrade method of SqliteOpenHelper class and write a drop table statement to remove any existing table or tables and recreate another one with modified table columns.

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        //delete table if table is modified
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

        //create table again
        onCreate(db);

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.