chennaione / sugar

Insanely easy way to work with Android Database.
http://satyan.github.com/sugar/
MIT License
2.62k stars 583 forks source link

E/SQLiteLog: (1) no such column: #738

Open pmrajani opened 7 years ago

pmrajani commented 7 years ago

SQLiteException: no such column: job_id (code 1): , while compiling: SELECT * FROM USER_JOB WHERE (job_id = ? )

` import android.os.Parcel; import android.os.Parcelable;

import com.orm.SugarRecord;

/**

public class UserJob extends SugarRecord implements Parcelable {

public int id,user_id,job_id,status,starred;
public String created_at;

//  Default constructor is necessary for SugarRecord
public UserJob(){

}

public UserJob(int id,int user_id,int job_id,int status,String created_at,int starred){
    super();
    this.id= id;
    this.user_id= user_id;
    this.job_id= job_id;
    this.status = status;
    this.created_at = created_at;
    this.starred=starred;
}

protected UserJob(Parcel in) {
    id = in.readInt();
    user_id = in.readInt();
    job_id = in.readInt();
    status = in.readInt();
    starred = in.readInt();
    created_at = in.readString();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(id);
    dest.writeInt(user_id);
    dest.writeInt(job_id);
    dest.writeInt(status);
    dest.writeInt(starred);
    dest.writeString(created_at);
}

@Override
public int describeContents() {
    return 0;
}

public static final Creator<UserJob> CREATOR = new Creator<UserJob>() {
    @Override
    public UserJob createFromParcel(Parcel in) {
        return new UserJob(in);
    }

    @Override
    public UserJob[] newArray(int size) {
        return new UserJob[size];
    }
};

}`

Insert Data from Activity code is bellow

`for(int i=0;i<jobs.resource.size();i++){ UserJob mJob = new UserJob(jobs.resource.get(i).id, jobs.resource.get(i).user_id, jobs.resource.get(i).job_id, jobs.resource.get(i).status,jobs.resource.get(i).created_at,jobs.resource.get(i).starred); mJob.save(); }

        List<UserJob> mJobs = UserJob.listAll(UserJob.class);
        Log.e("Size Applied JOB",""+mJobs.size());`

Log is showing size = 2 this is right and it's comes from tables record Now I want where query on my Fragment but it's show no such column: job_id

FRAGMENT CLASS

mJobs = Select.from(UserJob.class) .where(Condition.prop("job_id").eq(job.job_id)) .list();

Here App is crash and show error.

kigen commented 7 years ago

It's true, the column "job_id" doesn't exist as "jobid" anymore.. it should be "jobId" any field you name with `````` should be specified as camelCased e.g Example: if this is my class

class User extends SugarRecord{
    public String user_id;
    public String user_name;
    public String firstName;
    public User(){
    }
}

My query will be as follows:

List<User> users = Select.from(User.class)
.where(Condition.prop("userName").eq("kigen"))
.and(Condition.prop("first_name").eq("kigen"))
.list()

It's some kind of weird logic.. when a field name is camelCased the Condition.prop() value will be separated with _ eg camel_cased and vice versa. Try changing to:

mJobs = Select.from(UserJob.class) .where(Condition.prop("jobId").eq(job.job_id)) .list();

Let us know if it works.