SQLite: SQL error or missing database when inserting data into a table

I am trying to insert new data into an SQLite table.

  • I know for sure that the insert code is correct because I use it for different tables and it works great
  • I know for sure that the database and table are created and exist because I see them in SQLite browser.

But something in the syntax for creating or inserting SQLite is wrong, because I get this error:

SQL error or missing database

Here is my code:

CREATE TABLE IF NOT EXISTS Theory_answers ("questionID" INTEGER,"answerID" INTEGER,"answerText" TEXT, "isTrue" INTEGER) INSERT INTO Theory_answers (questionID, answerID, answerText,isTrue) VALUES ("1","1",text,"0") 
+4
source share
4 answers

if it will be ...

 VALUES ("1","1",text,"0") -> VALUES (1,1,'text',0) 

or

 VALUES ("1","1","text","0") <-- if you need to double quote all values? 

Looking at the link, she suggests that the insertion be done with a single quote around the text and without quotes around the numbers ...

sqlite> INSERT INTO Books (Id, Title, Author, ISBN) ...> VALUES (1, "War and Peace", "Leo Tolstoy", "978-0345472403");

+8
source

There are two possibilities for the error that I see if it is actually displayed in the SQL that you are showing;

  • You do not complete each line with ; , which causes SQLite to not realize that these are two separate statements.

  • You forgot to include β€œtext” in your insert statement.

Usually the error you are reporting is due to SQLite not finding the database you are asking to open, something very difficult to debug without seeing any code.

+3
source

If you get this error from adding a new column to / or to modify your SQLite table, this is most likely because the table is still in the application cache. The easiest fix if you are under development is to go to Settings-> Applications-> Application-> Storage-> Delete Data. And that will clear all cached memories.

Otherwise, programmatically, you can DELETE YOUR_TABLE TABLE and recreate a new one.

Hope this helps others as well.

+1
source

This problem may occur when the cursor is not closed. in android and in several languages, all cursors must be closed before running another request.

you need to find the entire "select" request before the new request and close all curser

  final Cursor cur = mDb.rawQuery( "select * from mytable;", . . . cur.close(); 

if the entire cursor is closed and your request is correct, your code will work correctly.

0
source

Source: https://habr.com/ru/post/1496415/


All Articles