Insert html code in Android sqlite database
I need a little help with an sqlite issue that I have.I'm trying to save json data which i get from the server and I get an exception trying to insert html code into the sqlite database.Here is the code I am usig for it and the exception :
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;
}
EXCEPTION:
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)
EDIT
Here is my function about which I am using for inserting data in 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;
}
I am using this method like this :
userDbHelper.executeQuery("content", values);//content is the name of my table
//userDBHelper is an instance of my DatabaseHelper class.
And what type of data I have on my table :
- id (integer)
- objectId (integer)
- format (integer)
- contentType (integer)
- dateUpdated (varchar)
- content (Text)
- objectOid (varchar)
Any ideas how to fix that so I can save the html code in sqlite?
Try to simplify your code until you get it working. Firstly, replace the line:
content = jsonObj.getString("content");
With the line:
content = "somethingsimple";
If that works, then you know it's something about the content string.
Do you know the max size of that column?
精彩评论