Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8400179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:29:49+00:00 2026-06-09T21:29:49+00:00

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

  • 0

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(?);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:29:50+00:00Added an answer on June 9, 2026 at 9:29 pm

    try this one….
    Change database version to 2.

    private
    static
    final
    int
    DATABASE_
    VERSION
    = 2;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code works really well for an initial insert into a table. But now
I have a problem with my code. Actually it works, but I want to
I am using the GCC compiler but I want the to code the actually
I created a Play! app and deployed it under TomCat. This works well. The
I am working with MvvmCross Monotouch. Here is the Binding Code that actually works
Is there any way I could run the following 'logical code' to actually work?
Actually my code is: LinearLayout ll = new LinearLayout(this); ll.setId(i); ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); ll.setPadding(0, 0,
Note: This code actually codes from a tutorial book I'm reading at the moment,
Kind of need help understanding what this code actually outputs. Does it out put
I'm trying to write a code to actually sort my array in an ascending

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.