2011-07-08 113 views
2

嗯,我一直爭取這一切的昨天和今天,我有一個簡單的SQLite插入即不斷返回-1作爲返回值,我讀的地方,改變主鍵名_id但沒有改變。SQLite的插入失敗

所以這裏有運算代碼的卡扣。 我的領域常量:

//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"; 

然後我createTables(SQLiteDatabase DB)我得到

// BALLS表

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" + 
");"); 

表的創建工作,我還有其他的表已被填充。

最後整個球Helper類

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(); 
    } 
} 

第二雙眼睛在這裏會來得心應手!所以如果任何人都可以在這裏發現任何東西,或者讓我知道如果我錯過了一些東西,我會非常感激。我已經檢查了B中的值(甚至通過我的內部驗證),但和插入這樣會失敗:

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

在日誌中的任何異常或其他信息? –

+0

插入失敗如何? Logcat中有沒有消息?所有球的ID –

+0

首先是自動增量(在創建表的查詢中定義),不知道這是好主意,插入與definded ID球,同時具有自動增量。二,我已經看到你正在使用布爾代替布爾值,因此你也可能使用Integer而不是int,Integer是對象。當您檢查是否ball.getId()== 0,球的ID可以不初始化,它可能會導致空指針異常。並請張貼例外,所以可能看到了什麼錯誤;) – Serhiy

回答

3

嗯......因爲我沒有,別人對創建表的代碼說明,現場BALL_USERID之間我沒有留下空間,所以這是一個整體的話,那db.exec應該有趣的事情拋出異常,但在這種情況下沒有並接受一個陌生的名字作爲現場無類型和創建沒有字段「userId」,但沒有類型的「useridinteger」的表(沒有設置默認類型?)

有時候只是那些可以讓你瘋狂的小事。