我昨天和今天一直在争取这个,我得到了一个简单的SQLite插件,它一直返回-1作为返回值,我读到某处将主键名更改为_id但没有做任何更改。
所以这里是代码的快照。 我的领域常数:
//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 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);
答案 0 :(得分:3)
嗯......因为我没有,没有人注意到表创建的代码,在字段BALL_USERID之间我没有留下空间所以它是一个完整的单词,有趣的事情,db.exec应该是抛出异常,但在这种情况下没有并接受一个奇怪的名称作为没有类型的字段,并创建没有字段“userId”但没有类型的“useridinteger”的表(如果你没有设置,是否有默认类型一个??)
有时只是那些可以让你疯狂的小事。
答案 1 :(得分:0)
我知道这是一个古老的示例,但是对于正在寻找Objective C示例的任何人来说,这都是很好的。但是,请确保在return语句之前放置sqlite3_reset(statement)行。否则,该代码仅在第一次保存或查找时有效。