我正在使用webservices从另一个表中获取数据。我正在跟踪表的每个值,所以我确定我正在获取值,但我无法将它们插入到我的新表中。我无法到达我去了,任何人都会帮帮我..
我正在使用的代码是
private void insertData(String bbookId,String bdesc, byte[] image1,byte[] image2 ,String bid,String bname) {
Log.i("id",bid);
Log.i("BookId",bbookId);
Log.i("name",bname);
System.err.println("url1"+image1);
System.err.println("url2"+image2);
System.err.println("desc"+bdesc);
Log.e("testing","LOOK @ 1214353469TRJB UJIOY ");
SQLiteDatabase db = placeData.getWritableDatabase();
Log.i("Bookid",bbookId);
ContentValues values;
values = new ContentValues();
values.put("bbookid",bbookId);
values.put("desc",bdesc);
values.put("url1",image1);
values.put("url2",image2);
values.put("bid",bid);
values.put("name", bname);
Log.i("inserting","______________");
db.insert("pagess", null, values);
}
我成功获取所有日志值,并且还显示日志标记“插入” _
我的数据库类是::::::::
public class PlaceData extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "page.db";
private static final String DATABASE_TABLE = "pagess";
private static final int DATABASE_VERSION = 1;
private static final String KEY_ID="bid";
private static final String KEY_BBOOKID="bbookid";
private static final String KEY_NAME="name";
private static final String KEY_URL1="url1";
private static final String KEY_URL2="url2";
private static final String KEY_DESC="desc";
private SQLiteDatabase sqLiteDatabase;
private Context context;
public PlaceData(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i("table","table");
Log.i("bookid in table is ",KEY_BBOOKID);
db.execSQL("CREATE TABLE " + DATABASE_TABLE+"("+KEY_BBOOKID+" varchar(15),"+KEY_DESC+" text(1500),"+KEY_URL1+" BLOB,"+KEY_URL2+" BLOB,"+KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT ,"+KEY_NAME+" varchar(150))");
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase(DATABASE_NAME);
if(dbExist){
Log.i("g","ds");
}else{
CopyFiles();
}
}
private void CopyFiles()
{
try
{
InputStream is = context.getAssets().open(DATABASE_NAME);
File outfile = new File("data/data/com.books.bcukbook/databases/",DATABASE_NAME);
outfile.getParentFile().mkdirs();
outfile.createNewFile();
if (is == null)
throw new RuntimeException("stream is null");
else
{
FileOutputStream out = new FileOutputStream(outfile);
byte buf[] = new byte[128];
do {
int numread = is.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
is.close();
out.close();
}
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public long insert(String bbookid, String desc,String url1,String url2,String bid,String name){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_BBOOKID,bbookid);
contentValues.put(KEY_DESC,desc);
contentValues.put(KEY_URL1,url1);
contentValues.put(KEY_URL2,url2);
contentValues.put(KEY_ID,bid);
contentValues.put(KEY_NAME,name);
return sqLiteDatabase.insert(DATABASE_TABLE, null, contentValues);
}
public Cursor selectQuery(String query) throws SQLException
{
String myPath = "data/data/com.books.bcukbook/databases/" + DATABASE_NAME;
CursorFactory bcuk_pages = null;
SQLiteDatabase myData = SQLiteDatabase.openDatabase(myPath, bcuk_pages, SQLiteDatabase.OPEN_READWRITE);
Cursor mCursor =myData.rawQuery(query, null);
mCursor.moveToFirst();
myData.close();
return mCursor;
}
private void versionUpdation(SQLiteDatabase db) {
}
public boolean checkDataBase(String db) {
SQLiteDatabase checkDB = null;
File dbFile = new File( "data/data/com.books.bcukbook/databases/" + DATABASE_NAME);
return dbFile.exists();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion >= newVersion)
return;
if (oldVersion == 1) {
Log.d("New Version", "Datasadsggfdh can be upgraded");
}
Log.d("Sample Data", "onUpgrade : " + newVersion);
}
@Override
public synchronized void close() {
super.close();
}
}
我的日志显示为
12-10 14:13:41.285: I/inserting(30596): ______________
12-10 14:13:41.295: E/SQLiteDatabase(30596): Error inserting bbookid=1 desc=<p>Providing in-depth coverage of how to build mobile applications using the next major release of the Android SDK, this invaluable resource takes a hands-on approach to discussing Android with a series of projects, each of which introduces a new feature and highlights techniques and best practices to get the most out of Android.</p> url1=[B@42725768 bid=2 url2=[B@42636730 name=Hello World page1
12-10 14:13:41.295: E/SQLiteDatabase(30596): android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)
12-10 14:13:41.295: E/SQLiteDatabase(30596): at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
12-10 14:13:41.295: E/SQLiteDatabase(30596): at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
12-10 14:13:41.295: E/SQLiteDatabase(30596): at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
12-10 14:13:41.295: E/SQLiteDatabase(30596): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
12-10 14:13:41.295: E/SQLiteDatabase(30596): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
12-10 14:13:41.295: E/SQLiteDatabase(30596): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
12-10 14:13:41.295: E/SQLiteDatabase(30596): at com.books.bcukbook.DetailsActivity$LoadAllProducts.insertData(DetailsActivity.java:688)
12-10 14:13:41.295: E/SQLiteDatabase(30596): at com.books.bcukbook.DetailsActivity$LoadAllProducts.callInsertion(DetailsActivity.java:663)
12-10 14:13:41.295: E/SQLiteDatabase(30596): at com.books.bcukbook.DetailsActivity$LoadAllProducts.doInBackground(DetailsActivity.java:616)
12-10 14:13:41.295: E/SQLiteDatabase(30596): at com.books.bcukbook.DetailsActivity$LoadAllProducts.doInBackground(DetailsActivity.java:1)
12-10 14:13:41.295: E/SQLiteDatabase(30596): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-10 14:13:41.295: E/SQLiteDatabase(30596): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-10 14:13:41.295: E/SQLiteDatabase(30596): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-10 14:13:41.295: E/SQLiteDatabase(30596): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-10 14:13:41.295: E/SQLiteDatabase(30596): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-10 14:13:41.295: E/SQLiteDatabase(30596): at java.lang.Thread.run(Thread.java:856)
答案 0 :(得分:4)
删除
values.put("bid",bid);
来自插入语句值。
由于bid
自动递增主键。
再试一次..
<强>更新强>
来自PlaceData Database class
。
KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT
private static final String KEY_ID="bid";
因此您无需插入bid
主键。您的Sqlite表会自动生成(递增)它。对于每个新插入的记录。
答案 1 :(得分:1)
请勿尝试设置出价,因为它已经是自动增值。
删除此 values.put(“出价”,出价);
无需插入它,它违反了主键约束。
答案 2 :(得分:0)
日志告诉你什么错!您的插入终止主键约束.check用于将重复值插入主键字段
android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)
答案 3 :(得分:0)
错误本身告诉你错误的是什么:
android.database.sqlite.SQLiteConstraintException:PRIMARY KEY必须是 独特的(代码19)
您实际上不需要提供KEY_ID
值。
答案 4 :(得分:0)
您创建了一个Primary Key
的表格。因此,其值必须为unique
而不是NULL
。所以当你第一次跑步时它可以正常工作。但第二次它采用相同的值,因此它不符合约束要求。