好吧,我昨天和今天一直在为此奋斗,我得到了一个简单的sqlite插入,该插入不断返回-1作为返回值,我读了某处将主键名更改为_ID,但未进行任何更改.
所以这是代码的快照.
我的字段常量:
//BALLS tablepublic 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)上
//球表
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);
解决方法:
嗯…就像我没有那样,没有其他人在表创建的代码上指出,在字段BALL_USERID之间,我没有留空格,所以这是一个完整的词,有趣的是db.exec应该抛出异常但是在这种情况下,它没有接受一个奇怪的名称作为没有类型的字段,并且创建了没有字段“ userID”但没有类型“ userIDinteger”的表(如果您未设置默认类型,是否有默认类型? )
有时候,那些琐碎的事情会让你发疯.
总结以上是内存溢出为你收集整理的Java-SQLite插入失败全部内容,希望文章能够帮你解决Java-SQLite插入失败所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)