Ale K.
Ale K.

Reputation: 316

SQLite Insert fails

Well i been fighting with this all yesterday and today, i got a simple SQLite insert that keeps returning a -1 as a return value, i read somewhere to change the primary key name to _id but made no change.

so here are the snaps op the code. My constants for the fields:

//BALLS TABLE
public  static final String BALLS_TABLE  = "balls";
public  static final String BALL_ID = "id";
public  static final String BALL_USERID = "userId";
public  static final String BALL_MODEL = "model";
public  static final String BALL_BRAND = "brand";
public  static final String BALL_SERIAL = "serial";
public  static final String BALL_NOTES = "notes";
public  static final String BALL_IMAGE = "image";

then on my createTables(SQLiteDatabase db) i got

//BALLS TABLE

db.execSQL(
"create table " + BALLS_TABLE +" (" +
BALL_ID + " integer primary key autoincrement not null," +
BALL_USERID + "integer not null," +
BALL_MODEL + " text not null," +
BALL_BRAND + " text not null," +
BALL_SERIAL + " text not null," +
BALL_NOTES + " text not null," +
BALL_IMAGE + " blob" +
");");

the creation of tables is working as i got other tables that are been populated.

and finally the whole ball Helper class

package com.kegel.android.bowlermanager.data;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class BallHelper {

    private SQLiteDatabase database;
    private ArrayList<Ball> currentBalls;
    private Context context;

    public BallHelper(Context context,SQLiteDatabase database)
    {
        this.context = context;
        this.database = database;
        loadBalls();
    }

    public ArrayList<Ball> getCurrentBalls() {
        return currentBalls;
    }

    public Boolean BallExists(long id)
    {
        for (Ball ball : currentBalls)
        {
            if (ball.getId() == id)
                return true;
        }   
        return false;
    }

    public Boolean BallExists(Ball u)
    {
        for (Ball ball : currentBalls)
        {
            if (ball.getId() == u.getId())
                return true;
        }   
        return false;
    }

    public void updateBall(Ball b) {
        assert(null != b);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] by = null;
        if (b.getImage() != null )
        {
            Bitmap bmp = b.getImage();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);   
            by = baos.toByteArray();  

        }
        ContentValues values = new ContentValues();
        values.put(SQLiteOpenDataHelper.BALL_USERID, 1);
        values.put(SQLiteOpenDataHelper.BALL_MODEL, b.getModel());
        values.put(SQLiteOpenDataHelper.BALL_BRAND , b.getBrand());
        values.put(SQLiteOpenDataHelper.BALL_SERIAL , b.getSerial());
        values.put(SQLiteOpenDataHelper.BALL_NOTES, b.getNotes());
        values.put(SQLiteOpenDataHelper.BALL_IMAGE , by);
        if (b.isValid())
        {
            if (b.getId() == 0)
            {
                b.setId(database.insert(SQLiteOpenDataHelper.BALLS_TABLE, null, values));
            }
            else
            {
                long id = b.getId();
                String where = String.format("%s = %d", SQLiteOpenDataHelper.BALL_ID, id);
                database.update(SQLiteOpenDataHelper.BALLS_TABLE, values, where, null);
            }
            loadBalls();    
        }
    }
    public void deleteBalls(Long id) {
        String where = String.format("%s in (%s)", SQLiteOpenDataHelper.BALL_ID, id);
        database.delete(SQLiteOpenDataHelper.BALLS_TABLE, where, null);
        loadBalls();
    }
    public void deleteBalls(Ball u) {
        String where = String.format("%s in (%s)", SQLiteOpenDataHelper.BALL_ID, u.getId());
        database.delete(SQLiteOpenDataHelper.BALLS_TABLE, where, null);
        loadBalls();
    }
        private void loadBalls() {
        byte[] img = null;
        currentBalls = new ArrayList<Ball>();
        //try
        //{
        Cursor ballsCursor = database.query(SQLiteOpenDataHelper.BALLS_TABLE, new String[] {SQLiteOpenDataHelper.BALL_ID,SQLiteOpenDataHelper.USER_ID, SQLiteOpenDataHelper.BALL_MODEL, SQLiteOpenDataHelper.BALL_BRAND, SQLiteOpenDataHelper.BALL_SERIAL, SQLiteOpenDataHelper.BALL_NOTES, SQLiteOpenDataHelper.BALL_IMAGE}, null, null, null, null, null);
        ballsCursor.moveToFirst();
        Ball b;
        if (! ballsCursor.isAfterLast()) {
            do {
                long id = ballsCursor.getInt(0);
                long bowlerId  = ballsCursor.getLong(1);
                String model = ballsCursor.getString(2);
                String brand = ballsCursor.getString(3);
                String serial = ballsCursor.getString(4);
                String notes = ballsCursor.getString(5);
                img = ballsCursor.getBlob(6);
                Bitmap bmp=BitmapFactory.decodeByteArray(img,0,img.length);
                b = new Ball(context,bowlerId,model,brand,serial,notes);
                b.setId(id);
                b.setImage(bmp);
                currentBalls.add(b);
            } while (ballsCursor.moveToNext());
        }
        ballsCursor.close();
    }
}

A second pair of eyes here will come handy! so if anyone can spot anything here, or let me know if I'm missing something i will really appreciate it. I already checked the values on b ( is even passing my internal validation ) but and insertion like this will fail:

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.fake_pofile);
Ball ball = new Ball(this,1, "Test", "Test", "", "");
ball.setImage(bm);
mHelper.updateBall(ball);

Upvotes: 3

Views: 5031

Answers (2)

Dave
Dave

Reputation: 91

I know this is an old example, but for anyone looking for an Objective C sample, this is good. However, be sure to put the sqlite3_reset(statement) lines BEFORE the return statements. Otherwise, the code only works on the first save or find.

Upvotes: 0

Ale K.
Ale K.

Reputation: 316

Well... as i didn't, nobody else noted on the code for the table creation, between the field BALL_USERID i wasn't leaving a space so it was a whole word, funny thing that db.exec is supposed to throw exceptions but in this case didn't and accepted a strange name as a field with no type and created the table with no field "userId" but "useridinteger" with no type ( is there a default type if you don't set one?? )

Sometimes are just those litte things that can make you go nuts.

Upvotes: 3

Related Questions