golkarm
golkarm

Reputation: 1014

Android Room: SQL error or missing database

I used Room library to create a database, but I could NOT build my app:

Task.java:8: error: [SQLITE_ERROR] SQL error or missing database (near "t": syntax error)
public class Task {
       ^

Here is my Task call:

@Entity(tableName = "task")
public class Task {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    private long mId;

    @ColumnInfo(
            name = "name",
            defaultValue = "Unknown Title"
    )
    private String mName;

    @ColumnInfo(
            name = "description",
            defaultValue = "Doesn't have any description!"
    )
    private String mDescription;

    @ColumnInfo(
            name = "isDone",
            defaultValue = "false"
    )
    private boolean mIsDone;


    public String getName() {
        return mName;
    }

    public void setName(String name) {
        if (name.length() > 0) mName = name;
    }

    public String getDescription() {
        return mDescription;
    }

    public void setDescription(String description) {
        if (description.length() > 0) mDescription = description;
    }

    public boolean isDone() {
        return mIsDone;
    }

    public void setIsDone(boolean isDone) {
        mIsDone = isDone;
    }

    public void changeIsDoneState() {
        mIsDone = !mIsDone;
    }

    public long getId() {
        return mId;
    }

    public void setId(long id) {
        this.mId = id;
    }
}

And my RoomDatabase:

@Database(entities = {Task.class}, version = 1)
public abstract class MainDB extends RoomDatabase {

    public abstract TackDao tackDao();

    private static volatile MainDB INSTANCE;
    public static MainDB getInstance(Context c) {

        if (INSTANCE == null) {
            synchronized (MainDB.class) {
                if (INSTANCE == null) INSTANCE = Room.databaseBuilder(
                        c.getApplicationContext(), MainDB.class, "main"
                ).allowMainThreadQueries().build();
            }
        }
        return INSTANCE;
    }
}

And my Dao:

@Dao
public interface TackDao {

    @Query("SELECT * FROM task")
    LiveData<List<Task>> getAllTasks();

    @Insert
    long insert(Task task);

    @Update
    int update(Task... tasks);

    @Delete
    int delete(Task... tasks);
}

Upvotes: 1

Views: 456

Answers (1)

a_local_nobody
a_local_nobody

Reputation: 8191

seems like there's an issue with you using:

 defaultValue = "Doesn't have any description!"

the apostrophe in Doesn't is causing this issue it seems

changing it to :

    @ColumnInfo(
            name = "description",
            defaultValue = "Does not have any description!"
    )

does work. not sure how you would properly escape this apostrophe value though, might not even be possible, what i've tried doing was:

    @ColumnInfo(
            name = "description",
            defaultValue = "Doesn''t have any description!"
    )

and this does seem to work correctly

Upvotes: 1

Related Questions