在Android sqlite数据库中插入html代码

时间:2011-09-27 06:02:41

标签: android html sqlite

我需要一些sqlite问题的帮助。我正在尝试保存从服务器获取的json数据,并且尝试将html代码插入sqlite数据库时出现异常。这是代码我我正在使用它和例外:

CODE:

public boolean executeInsert() {
        UserDatabaseHelper userDbHelper = new UserDatabaseHelper(context, null, 1);
        userDbHelper.initialize(context);
        ContentValues values = new ContentValues();
        try {
            values.put("objectId", objectId);
            values.put("objectOid", objectOid);

            String jsonData = new String(contentBuffer,"UTF-8");
            Log.w("JSONDATA","JSONDATA VALID OR NOT : "+jsonData);
            json = new JSONObject(jsonData);
            JSONObject jsonObj =(JSONObject) new JSONTokener(jsonData).nextValue();
            locale = jsonObj.getString("locale");
            Log.w("LOCALE","SHOW LOCALE  : "+locale);

            contentType = Integer.parseInt(jsonObj.getString("content_type"));
            Log.w("CONTENT TYPE","SHOW CONTENT TYPE : "+contentType);
                values.put("contentType", contentType);

            format = Integer.parseInt(jsonObj.getString("format"));
            Log.w("FORMAT","SHOW FORMAT : "+format);
                values.put("format", format);

            title = jsonObj.getString("title");
            Log.w("TITLE","SHOW TITLE : "+title);
                values.put("title", title);

            content = jsonObj.getString("content");
            Log.w("CONTENT","SHOW CONTENT : "+content);
                values.put("content", content);

            lastUpdate = jsonObj.getString("last_updated");
            Log.w("LAST UPDATED","LAST UPDATED : "+lastUpdate);
                values.put("dateUpdated", lastUpdate);

            collectionId = jsonObj.optInt("collection_id", 0);
                values.put("collectionId", collectionId);
            cardId = jsonObj.optInt("card_id", 0);
                values.put("cardId", cardId);

            userDbHelper.executeQuery("content", values);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            Log.w("Error","FUCKKKKKKKKKKKKKK ERROR : "+e);
        } catch (JSONException e) {
            e.printStackTrace();
            Log.w("Error","FUCKKKKKKKKKKKKKK ERROR : "+e);
        }
        return true;

    }

例外:

09-27 08:48:52.279: ERROR/Database(7862): Error inserting content=Welcome to Stampii<br />
09-27 08:48:52.279: ERROR/Database(7862): <br />
09-27 08:48:52.279: ERROR/Database(7862): Your favourite collections synchronised on your computer and your mobile in a new format: stampii Download them with photos, and constantly updated statistics. Swap and play with your friends and enjoy their contents even when you're offline!<br />
09-27 08:48:52.279: ERROR/Database(7862): <br />
09-27 08:48:52.279: ERROR/Database(7862): From theArea in the Kiosk, you'll be able to browse through all tha active collections, and find the one that you like best.<br />
09-27 08:48:52.279: ERROR/Database(7862): <br />
09-27 08:48:52.279: ERROR/Database(7862): Once you've selected the choosed collection, you just have to activate it to start enjoying your. You can make as many collections as you want at the same time.<br />
09-27 08:48:52.279: ERROR/Database(7862): <br />
09-27 08:48:52.279: ERROR/Database(7862): You can purchase yourfrom theStore, via sms or promotional vouchers.<br />
09-27 08:48:52.279: ERROR/Database(7862): <br />
09-27 08:48:52.279: ERROR/Database(7862): Allfollow a similar structure. They're all represented with a portrait with an image and all the data referred to the character that you've got in your.<br />
09-27 08:48:52.279: ERROR/Database(7862): <br />
09-27 08:48:52.279: ERROR/Database(7862): Your collection will be updated for free with the last info every time you select the sincronize button. Yourconstantly updated.<br />
09-27 08:48:52.279: ERROR/Database(7862): <br />
09-27 08:48:52.279: ERROR/Database(7862): Enjoy searching, negotiating and changing your repeated stampii with your friends, use your mobile and the social networks to get the you were looking for. title=Title Tutorial format=2 contentType=1 objectId=1 dateUpdated=2010-03-26 16:56:43
09-27 08:48:52.279: ERROR/Database(7862): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
09-27 08:48:52.279: ERROR/Database(7862):     at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
09-27 08:48:52.279: ERROR/Database(7862):     at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
09-27 08:48:52.279: ERROR/Database(7862):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1623)
09-27 08:48:52.279: ERROR/Database(7862):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1484)
09-27 08:48:52.279: ERROR/Database(7862):     at com.stampii.stampii.comm.rpc.UserDatabaseHelper.execQuery(UserDatabaseHelper.java:252)
09-27 08:48:52.279: ERROR/Database(7862):     at com.stampii.stampii.comm.rpc.UserDatabaseHelper.executeQuery(UserDatabaseHelper.java:247)
09-27 08:48:52.279: ERROR/Database(7862):     at com.stampii.stampii.comm.rpc.ContentRPCPacket.executeBefore(ContentRPCPacket.java:105)
09-27 08:48:52.279: ERROR/Database(7862):     at com.stampii.stampii.user.UserLogin$2$1.run(UserLogin.java:365)
09-27 08:48:52.279: ERROR/Database(7862):     at java.lang.Thread.run(Thread.java:1102)

修改

这是我用于在SQLite中插入数据的函数:

public boolean executeQuery(String tableName,ContentValues values){
    return execQuery(tableName,values);
}

private  boolean execQuery(String tableName,ContentValues values){
    sqliteDb = instance.getWritableDatabase();
    sqliteDb.insert(tableName, null, values);
    return true;
}

我正在使用这种方法:

userDbHelper.executeQuery("content", values);//content is the name of my table
//userDBHelper is an instance of my DatabaseHelper class.

我桌子上有什么类型的数据:

  1. id(整数)
  2. objectId(整数)
  3. 格式(整数)
  4. contentType(整数)
  5. dateUpdated(varchar)
  6. 内容(文字)
  7. objectOid(varchar)
  8. 任何想法如何修复,以便我可以在sqlite中保存html代码?

1 个答案:

答案 0 :(得分:1)

尝试简化代码,直到让它运行起来。首先,替换以下行:

content = jsonObj.getString("content");

使用以下行:

content = "somethingsimple";

如果有效,那么你就知道它是关于内容字符串的。

您知道该列的最大尺寸吗?