Priyabrata
Priyabrata

Reputation: 1232

Table has no column named "Column Name" In SQLite with android

I am writing an android application and trying to store my data in a db. How ever I am getting the error :

"Table Friend_Master has no column named Profile_Pic_Url"

I've gone through all the posts of this kind and tried "uninstalling and re-installing" and "clear data" but didn't work.

Here is my code :

package com.example.groupchat;

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

public class MyDatabase extends SQLiteOpenHelper{

    //Definition of the database
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "AppInformation";

    //Definition of the table
    private static final String TABLE_NAME = "Friend_Master" ;

    //Definition of the fields
    private static final String ID = "F_ID";
    static final String FirstName = "FIRST_NAME";
    private static final String LastName = "LAST_NAME";
    private static final String DisplayName = "DISPLAY_NAME";
    private static final String ProfilePicUrl = "Profile_Pic_Url";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); //Testing purpose ONLY
        String CreateTable = "CREATE TABLE " + TABLE_NAME + "(" + ID + " TEXT PRIMARY KEY , " +
                FirstName + " TEXT, " + LastName + " TEXT, " + DisplayName + " TEXT, "+ ProfilePicUrl + "TEXT" + ");" ;
        db.execSQL(CreateTable);
    }

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

    public void insertInformationInDatabase(FriendInformation info){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(ID, info.getID());
        cv.put(FirstName, info.getFirst_Name());
        cv.put(LastName, info.getLast_Name());
        cv.put(DisplayName, info.getDisplay_Name());
        cv.put(ProfilePicUrl, info.getProfile_Pic_Url());
        db.insert(TABLE_NAME, null, cv);
        db.close();
    }

    public FriendInformation getInformationFromDatabase(String _ID, String First_Name){
        SQLiteDatabase db=this.getReadableDatabase();
        String Query = "SELECT * FROM " + TABLE_NAME + " WHERE " + ID +  " =  \"" + _ID + "\" AND " + FirstName + " = \"" + First_Name + "\"";
        Cursor cursor = db.rawQuery(Query,  null);
        if (cursor.moveToFirst()){
            cursor.moveToFirst();
            FriendInformation info = new FriendInformation(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4));
            return info;
        }
        return null;
    }

    public FriendInformation getInformationFromDatabase(String Display_Name){
        SQLiteDatabase db=this.getReadableDatabase();
        String Query = "SELECT * FROM " + TABLE_NAME + " WHERE " + DisplayName +  " =  \"" + Display_Name + "\"";
        Cursor cursor = db.rawQuery(Query,  null);
        if (cursor.moveToFirst()){
            cursor.moveToFirst();
            FriendInformation info = new FriendInformation(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4));
            return info;
        }
        return null;
    }

}

Cannot understand where I am going wrong...

Upvotes: 1

Views: 823

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

You forget to add space between column name ProfilePicUrl and column type. add space between both as:

 String CreateTable = "CREATE TABLE " + TABLE_NAME + 
                        "(" + ID + " TEXT PRIMARY KEY , " +
                     FirstName + " TEXT, " + LastName + " TEXT, " 
                    + DisplayName + " TEXT, "+ ProfilePicUrl + " TEXT " + ");" ;

best option to avoid this types of mistakes use SQLiteQueryBuilder

Upvotes: 2

Related Questions