0

I have made DB Helper class as follows and facing problem inserting data into my table.

This is my DB Helper:

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by Admin on 2/24/2017.
 */
public class CompanyDatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 5;

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

    // COMPANY table name
    private static final String TABLE_COMPANY = "detailsofcompany";
    // COMPANY Table Columns names
    private static final  String KEY_ID="_id";
    private static final String KEY_COMPANYNAME="_companyName";
    private static final String KEY_COMPANYURL="_companyUrl";
    private static final String KEY_COMPANYEMAIL="_companyEmail";
    private static final String KEY_COMPANYNUMMBER="_companyNumber";
    private static final String KEY_COMPANYADDRESS="_companyAddress";

    public CompanyDatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


/*
Creating new Table
 */
    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_COMPANY_TABLE = "CREATE TABLE "
                + TABLE_COMPANY + "("+ KEY_ID + " INTEGER PRIMARY KEY,"+ KEY_COMPANYNAME + " TEXT,"+ KEY_COMPANYURL + " TEXT,"+ KEY_COMPANYEMAIL + " TEXT"+ KEY_COMPANYNUMMBER+" TEXT"+ KEY_COMPANYADDRESS+" TEXT"+")";
        db.execSQL(CREATE_COMPANY_TABLE);
    }
/*
Upgrading Table
 */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMPANY);

        // Create tables again
        onCreate(db);
    }

    // Adding new company
    public void addCompany(Company company) {
        SQLiteDatabase db = CompanyDatabaseHandler.this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_COMPANYNAME, company.getCompanyName());
        values.put(KEY_COMPANYURL, company.getCompanyLink());
        values.put(KEY_COMPANYEMAIL, company.getCompanyEmail());
        values.put(KEY_COMPANYNUMMBER, company.getCompanyNumber());
        values.put(KEY_COMPANYADDRESS, company.getCompanyAddress());

        // Inserting Row
        db.insert(TABLE_COMPANY, null, values);
        // Closing database connection
        db.close();
    }

    // Getting single company
    public Company getCompany(int id) {SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_COMPANY,                                         //Table
                        new String[] { KEY_COMPANYNAME,KEY_COMPANYURL,KEY_COMPANYEMAIL ,KEY_COMPANYNUMMBER,KEY_COMPANYADDRESS}, //Colunms
                        KEY_ID + "=?",       //Selection KEY
                        new String[] { String.valueOf(id) },//Selection Args
                        null,                //Group By
                        null,                //Having
                        null);                //Order By


        if (cursor != null)
            cursor.moveToFirst();

        Company company=new Company(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5));
        // return company
        return company;
    }

    // Getting All Company
    public List<Company> getAllCompany() {
        List<Company> companyList = new ArrayList<Company>();
        // Select All Query
        String selectQuery = "SELECT * FROM " + TABLE_COMPANY;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Company company = new Company();
                company.setCompanyId(Integer.parseInt(cursor.getString(0)));
                company.setCompanyName(cursor.getString(1));
                company.setCompanyLink(cursor.getString(2));
                company.setCompanyEmail(cursor.getString(3));
                company.setCompanyNumber(cursor.getString(4));
                company.setCompanyAddress(cursor.getString(5));
                // Adding company to list
                companyList.add(company);
            } while (cursor.moveToNext());
        }

        // return companyList
        return companyList;
    }

    // Getting company Count
    public int getCompanyCount() {
        String countQuery = "SELECT * FROM " + TABLE_COMPANY;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        int count=cursor.getCount();
        cursor.close();

         return count;
//        return cursor.getCount();
    }

    // Updating single Company
    public int updateCompany(Company company) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_COMPANYNAME, company.getCompanyName());
        values.put(KEY_COMPANYURL, company.getCompanyLink());
        values.put(KEY_COMPANYEMAIL, company.getCompanyEmail());
        values.put(KEY_COMPANYNUMMBER, company.getCompanyNumber());
        values.put(KEY_COMPANYADDRESS, company.getCompanyAddress());
        // updating row
        return db.update(TABLE_COMPANY, values, KEY_ID + " = ?",
                new String[] { String.valueOf(company.getCompanyId()) });
    }

    // Deleting single company
    public void deleteCompany(Company company) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_COMPANY, KEY_ID + " = ?",
                new String[] { String.valueOf(company.getCompanyId()) });
        db.close();
    }
}

But while trying to add in table i am getting as follows :

    table detailsofcompany has no column named _companyAddress
02-25 20:12:29.781 19919-19919/############ E/SQLiteDatabase: Error inserting _companyAddress=Address here [email protected] _companyNumber=09876543210 _companyUrl=http://www.google.com _companyName=ABC COMPANY
                                                                                          android.database.sqlite.SQLiteException: table detailsofcompany has no column named _companyAddress (code 1): , while compiling: INSERT INTO detailsofcompany(_companyAddress,_companyEmail,_companyNumber,_companyUrl,_companyName) VALUES (?,?,?,?,?)

I am new on android SQLITE implementation. I have tried updating version and changing name of database as well as column name but it didnt helped.

I am stucked Your suggestions will be alot helpfull. I am not getting where am i going wrong.

1 Answer 1

2

Commas are missing from the CREATE_COMPANY_TABLE string.For the two columns you have missed the commas after TEXT Also uninstall the existing application before running the application with the changes done.

@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_COMPANY_TABLE = "CREATE TABLE "
            + TABLE_COMPANY + "("+ KEY_ID + " INTEGER PRIMARY KEY,"+ KEY_COMPANYNAME + " TEXT,"+ KEY_COMPANYURL + " TEXT,"+ KEY_COMPANYEMAIL + " TEXT,"+ KEY_COMPANYNUMMBER+" TEXT,"+ KEY_COMPANYADDRESS+" TEXT"+")";
    db.execSQL(CREATE_COMPANY_TABLE);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Tysm :-). I should have checked it first. I was looking for big mistake and left this. Tysm.

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.