jeudi 30 juillet 2015

Changed DB, updated DB version can't add data and can't not add data

I am working on an android application that uses two databases. Recently, I had to add a new column to one of the databases. Upon doing so, it broke my database. Installing and re-installing the application on my device did nothing, and I also updated the DB version.

Trying to insert data will net me this error:

E/SQLiteLog﹕ (1) table message_table has no column named msg_type

So, I tried taking out the "msg_type" column from the insert, and inserting data which gave me this error:

E/SQLiteLog﹕ (1299) abort at 8 in [INSERT INTO message_table(recipient,message) VALUES (?,?)]: NOT NULL constraint failed: message_table.msg_typeTEXT

Here is the oncreate:

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
            COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + //msg_id
            COL_2 + " TEXT NOT NULL, " + //recipient
            COL_3 + " TEXT, " + //message
            COL_4 + "TEXT NOT NULL);"); //message type
}

and the insert class:

public boolean addMessage(String recipient, String message, String type){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();

    //populate message object
    contentValues.put(COL_2, recipient);
    contentValues.put(COL_3, message);
    contentValues.put(COL_4, type);

    //insert new message
    long result = db.insert(TABLE_NAME, null, contentValues);

    //check if operation was successful
    if(result == -1)
        return false;
    else
        return true;
}

How can I be getting an error for either case? I thought that it didn't recognize the new column was added from the first error, but it also doesn't like not receiving the data for that column.

ANDROID: attempt to re-open an already-closed object?

I am trying a delete data from my table(s) however it seems that I am getting an error. So basically in my database contains two tables that are connected through an "exercise ID". So to delete data from my database, I would need to first get an exercise ID from the row I want deleted, then, use that as a reference against my second table and delete everything that is associated with it. So heres what I have so far that does not seem to want to work...

public void deleteRowFromDatabase(String exerciseName, String bodyPart) {

    SQLiteDatabase db = myDBHelper.getWritableDatabase();

    int exerciseID = getExerciseID(exerciseName, bodyPart);
    String query = "SELECT " + myDBHelper.COLUMN_EXERCISENAME_ID + " FROM " + myDBHelper.TABLE_EXERCISES_VALUE +
                    " WHERE " + myDBHelper.COLUMN_EXERCISENAME_ID + "=\"" + exerciseID + "\";";

    Cursor c = db.rawQuery(query, null);

    while (c.getCount()>0){
        c.moveToFirst();
        db.execSQL("DELETE FROM " + myDBHelper.TABLE_EXERCISES_VALUE + " WHERE " + myDBHelper.COLUMN_EXERCISENAME_ID +
                    "=\"" + exerciseID + "\";");
        c.moveToNext();
    }

    db.execSQL("DELETE FROM " + myDBHelper.TABLE_EXERCISES + " WHERE " + myDBHelper.COLUMN_BODYPARTNAME +
                "=\"" + bodyPart + "\"" + " AND " + myDBHelper.COLUMN_EXERCISENAME + "=\"" + exerciseName + "\";");

    c.close();
    db.close();
}

sqlite write error on attempt to read

I received this error when trying to SELECT from a sqlite database that I had opened with a read-only connection:

attempt to write a readonly database

What would cause a write error on a read attempt?

SQLite no such table, logic error

I had a database it worked fine, I changed the name of the table and it stopped working, if I change the name back, it works again. Of course I change query string after changies in the database. A feeling that SQLIte caches my database... What could be the problem? My query string:

"SELECT * FROM 'tb_Table_Name' ORDER BY 'Id';"

If i change tb_Table_Name to Table_Name it tell me no such table, logic error... And when I add new columns SQLIte not find them too, it tell me no such column, logic error It looks like it just working on my previous database structure. I can't understand why... Help me.

Where can I find the the SQLite binary SQLite.Interop.dll?

There are supposed to be an x86 and an x64 version of the library SQLite.Interop.dll so that you can work on either machine architecture. However I cannot find where this binary comes from. I've downloaded the .NET 4.0 precompiled binary packages from system.data.sqlite.org (both statically-linked and non), and they just aren't in the zip packages. I don't get it. They are referenced several places in the documentation yet they are not provided in the downloads.

Android DB Update near "WHERE": syntax error

I know there are already too many questions about this, and believe me I've done my part reading the answers to find a solution. Unfortunately, I can't find my mistake

Here's my code:

SQLiteDatabase db = getWritableDatabase();

ContentValues values = new ContentValues();
values.put("eat_it", 1);
values.put("expired", 1);
String[] args = {food.name, "0"};

db.update("foods", values, "name=? AND expired=?", args);
db.close();

What I want to do:

Find a record in the table foods WHERE the value of column name = food.name and the value of column expired = 0. If found, SET the record's eat_it column to 1 and expired column to 1. If no record found, don't do anything

Here's my create table syntax:

CREATE TABLE IF NOT EXISTS foods (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
    ...
    eat_it INTEGER DEFAULT 0,
    expired INTEGER DEFAULT 0
);

Here's the error message:

...
 Caused by: android.database.sqlite.SQLiteException: near "WHERE": syntax error (code 1): , while compiling: UPDATE foods SET expired=?,eat_it=? WHERE name ='Taco Salad'WHERE expired = 0
...

Thanks for your help

Check record in SqLite (ionic)

Does anybody know, that how can I check an inserted record? I would like to create an intro page. When the user visits once, I wanna store it and check it.

$rootScope.insert = function(visited) {
     var query = "INSERT INTO shoplist (visited) VALUES (?)";
     $cordovaSQLite.execute(db, query, [visited]).then(function(res) {
        console.log("INSERT ID -> " + res.insertId);
     }, function (err) {
        console.error(err);
     });
}

$rootScope.insert(1);

Basically I would like to check that visited record is 0 or 1. What should I do?