0

我正在使用 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)
4

5 回答 5

4

消除

values.put("bid",bid); 从插入语句值。

Auto Increment Primary Keybid一样。

然后再试一次..

更新:

从你的PlaceData Database class.

KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT 

private static final String KEY_ID="bid";

所以你不必插入bid主键。您的 Sqlite 表会自动生成(增量)它。对于每个新插入的记录。

于 2012-12-10T09:05:11.433 回答
1

不要尝试放置投标价值,因为它已经是自动增量值。

删除这个values.put("bid",bid);

不需要插入它,它违反了主键约束。

于 2012-12-10T09:03:56.877 回答
0

日志告诉你出了什么问题!您的插入终止您的主键约束。检查是否将重复值插入主键字段

 android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)
于 2012-12-10T09:05:00.590 回答
0

错误本身会告诉您到底出了什么问题:

android.database.sqlite.SQLiteConstraintException: PRIMARY KEY 必须是唯一的(代码 19)

你真的不需要提供KEY_ID价值。

于 2012-12-10T09:06:20.337 回答
0

您创建了一个表Primary Key。所以它的值一定是unique而不是NULL。所以当你第一次跑步时它工作得很好。但是第二次它采用相同的值,因此它不符合约束要求。

于 2012-12-10T09:27:17.107 回答