Android Question B4A SQLite error when inserting data into database (Can't Find Table Name)

The_killer01

New Member
Hello, Friends!

As I am new to B4A and I am just a student, I am working on an app/game that needs to connect to a database. In this case, I am currently using DB Browser for SQLite.

I created a table named "Questions" in DB Browser and has some attributes in it.

But when I try to insert data into it from my app in B4A, the B4A throws an exception that it can't find the table when in fact it is there.

My ExecNonQuery format is correct as I've tried it on DB Browser.

If you can help me, this will be a huge win for me. I've tried many different solutions but none of them are working.

I guess this is my last resort.

Thank you!

Error Message:
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Activity (category) Create (first time) **
** Activity (category) Resume **
** Activity (category) Pause, UserClosed = true **
** Activity (technology) Create (first time) **
** Activity (technology) Resume **
** Activity (technology) Pause, UserClosed = true **
** Activity (tech_1) Create (first time) **
** Activity (tech_1) Resume **
Error occurred on line: 329 (Tech_1)
android.database.sqlite.SQLiteException: no such table: Questions (code 1): , while compiling: INSERT INTO Questions (ID,category,question_number,score,complete) VALUES (1, 'Technology', 1, 100, '1')
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677)
    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
    at anywheresoftware.b4a.sql.SQL.ExecNonQuery(SQL.java:74)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:205)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at b4a.example.tech_1._insertdata(tech_1.java:981)
    at b4a.example.tech_1._btn_submit_click(tech_1.java:877)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:205)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:5638)
    at android.view.View$PerformClick.run(View.java:22430)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6176)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
** Activity (tech_1) Pause, UserClosed = true **
 

John Naylor

Active Member
Licensed User
Longtime User
Are you copying your database file from your assets folder to your default folder when your app starts?

If so, are you checking that your database file exists in your default folder and only copying your database file if it isn't there?

Did you then modify your database file in your assets folder by adding a table with DB Browser?

If so, your app is using an old version of your database file. Delete the old one and let your app copy the new one into the correct place.

Easy fix - paste this at the start of your app. Run it once then stop your app and comment out the line. Next time you run your app the new database file will be put in place.

B4X:
File.Delete (xui.DefaultFolder, "YOURDBNAME.sqlite")
 
Upvote 0

zed

Active Member
Licensed User
@Klaus is an expert, he is always rightšŸ˜‡
INSERT INTO Questions VALUES (1, 'Technology', 1, 100, '1')

It is not necessary to name the column names.
It seems that the last 1 is considered a string. Is it a mistake or not.

When you have a problem, the best is to post the code. It's the best way to see what's wrong.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I believe the case is likely as described by John Naylor in post #2. This is a very common mistake by many B4A beginners.
That's why I never attach my db in Files tab but to create my own tables by executing the CREATE TABLE query.

To enable members to help the OP, a sample project should be attached.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
There is a difference between "Questions" and "questions"
No. The table name is not case sensitive. The column name is. For instance if the column name is: id and you do: Log(rs.GetInt("Id")), you will get an error.
Dear Valued Members: Maybe we should not bombard this new OP with so many answers and posts. He only started one or two days ago. We need to give him a chance to absorb @John Naylor 's answer and give him a chance to respond with his code before confusing him with too many answers too fast.
 
Upvote 0

The_killer01

New Member
Are you copying your database file from your assets folder to your default folder when your app starts?

If so, are you checking that your database file exists in your default folder and only copying your database file if it isn't there?

Did you then modify your database file in your assets folder by adding a table with DB Browser?

If so, your app is using an old version of your database file. Delete the old one and let your app copy the new one into the correct place.

Easy fix - paste this at the start of your app. Run it once then stop your app and comment out the line. Next time you run your app the new database file will be put in place.

B4X:
File.Delete (xui.DefaultFolder, "YOURDBNAME.sqlite")
Good day, sorry for the late comment as I've been busy these past few days.

when I tried to copy and put your code into the Activity_Create part of my code, there is an error that says "Declared Variable "xui" is used before it was assigned any value"

Sorry if I can't figure it out how to properly fix it as I said that I am new to this and I am just a 2nd year College Student.

However, I did check my "default group folder" in the files tab and deleted the database file that is there and replace it with the new version that is on my "Files" folder. But even when I did that, it only return the same error. Hope you can continue to help me.
 
Upvote 0

Shelby

Well-Known Member
Licensed User
Hey Kill,
To upload your project for the experts here to help you, on your ide, click file, then export as zip. Save it somewhere and you can upload it here in the forum using the attach files button.
 
Upvote 0
Top