私はこのすべての昨日と今日戦っています。戻り値として-1を返すシンプルなSQLiteインサートがあります。主キー名を_idに変更しても変更はしませんでした。SQLiteの挿入に失敗しました
ここにコードのスナップがあります。フィールドの 私の定数は:私のcreateTablesで、その後
//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";
(SQLiteDatabaseデシベル)私が得た
// 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" +
");");
データが入力されました。
、最終的に全体のボールヘルパークラス
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);
ログに例外または追加情報がありますか? –
インサートはどのように失敗しますか? Logcatにメッセージがありますか? –
まず、ball idはautoincrementです(create table queryで定義されています)。自動インクリメント中にdefinded idを持つボールを挿入するのが良いのかどうかはわかりません...第二に、booleanの代わりにBooleanを使用しているのを見てきました。 intの代わりにIntegerを使用し、Integerをobjectにすることもできます。 ball.getId()== 0かどうかをチェックすると、ボールのIDは初期化されず、NullPointer例外が発生する可能性があります。例外をポストしてください。何が起こっているのかを見ることができます) – Serhiy