WebView Sqlite Problem?

paul3252

Member
Licensed User
Longtime User
I am developing a simple Q&A app. to get used to B4A. I have a small sqlite DB in my app, four fields, ID, Question, Answer, Category. ID is INT, all others are text.

I was hoping to store HTML in the answer field of the database, i.e. as tags

<HTML>
<body>
Answer here
</body>
</html>

Nothing fancy at all really. I am loading the answer into a webview using;

WebView1.LoadHtml(cursor2.getstring("answer"))

The correct answer is loaded but occasionally I run up against a cannot convert blob to text error. Any suggestions as to what the problem may be? Is it a length of field/string problem, or does htmlview not understand some of the tags?
 

warwound

Expert
Licensed User
Longtime User
I'd guess that some of the stored HTML contains special characters that need escaping or encoding.

Update you code to send the HTML to the log:

B4X:
Log(cursor2.getstring("answer"))
WebView1.LoadHtml(cursor2.getstring("answer"))

Look at the HTML when you get the error - are there any quote/apostophe or other special characters in the HTML?

Martin.
 
Upvote 0

paul3252

Member
Licensed User
Longtime User
It doesn't even get as far as loading the answer on those that cause the crash. When examining the contents in the HEX editor for the record that causes the 'crash' there is nothing that seems out of the ordinary.

I wonder if there is a 'codepage' issue. How do I specify for example that I want the records read as UTF8?

The HTML is <b>When should I commence my leave?</b><br /><br />


LogCat connected to: emulator-5554
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main_lstquestions_itemclick (B4A line: 149)
Log(cursor2.GetString("answer"))

android.database.sqlite.SQLiteException: unknown error: Unable to convert BLOB to string
at android.database.CursorWindow.getString_native(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:329)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
at anywheresoftware.b4a.sql.SQL$CursorWrapper.GetString(SQL.java:260)
at fedQ_A.aspolfed.main._lstquestions_itemclick(main.java:623)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:113)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:101)
at anywheresoftware.b4a.objects.ListViewWrapper$1.onItemClick(ListViewWrapper.java:55)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.ListView.performItemClick(ListView.java:3382)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
android.database.sqlite.SQLiteException: unknown error: Unable to convert BLOB to string
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
I wouldn't save the tags in the DB, just the answers and add the tags in the code, like this:

B4X:
Answer = cursor2.getstring("Answer")

WebView1.LoadHtml("<html><body><b>" & Answer & "</b></body></html>")
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I wouldn't save the tags in the DB, just the answers and add the tags in the code, like this:

B4X:
Answer = cursor2.getstring("Answer")

WebView1.LoadHtml("<html><body><b>" & Answer & "</b></body></html>")

+1

That makes the most sense to me.

If paul3252 wants to get or set the encoding of a database then he will find the PRAGMA statement to be what he needs.

I tested with this code and found that a newly created database has a default encoding of 'UTF-8':

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim MyCursor As Cursor
   Dim MyDatabase As SQL
   
   MyDatabase.Initialize(File.DirInternal, "new_database.db", True)
   
   MyCursor=MyDatabase.ExecQuery("PRAGMA encoding;")
   MyCursor.Position=0
   Log(MyCursor.GetString("encoding"))
   
End Sub

If you still have problems i suspect that you are creating the database with characters from a source that is not UTF-8 encoded.

Martin.
 
Upvote 0

paul3252

Member
Licensed User
Longtime User
Thanks all, cracked it. Got out the HEX editor and found and invisible rogue character that the webview did not like. I am now using Notpepad++ which is able to strip away any hidden junk.

The reason I am including the tags withing the answers is that the WebViewer then gives me an easy way to control layout and formatting using html codes, font size, italics, bold etc. A better option would be a 'rich text' aware control but as a beginner I am unsure whether such exists for B4A.
 
Upvote 0
Top