Problem About SQLite Database, No Such Table

SQLite: No Such Table Error Android P problem

Arg finally solved it adding this.close(); after this.getReadableDatabase();

That openned connection was generating the original no such table exception

So I used Util.getDatabasePath(DB_NAME); instead of the complex propossed solution in Android P - 'SQLite: No Such Table Error' after copying database from assets and now the code is much more simpler

Thanks a lot to @LifeStyle who found the real problem.

Now the code is much more simpler:

public static String getDatabasePath(String fileNname){
return ApplicationContextProvider.getContext().getDatabasePath(fileNname).getAbsolutePath();
}

public class DbHelper extends SQLiteOpenHelper{
private String DB_NAME;
private String DB_COMPLETE_PATH;
private SQLiteDatabase mDataBase;
private static final int DATABASE_VERSION = 1;

public DbHelper(String name) throws IOException {
super(ApplicationContextProvider.getContext(), name+".db", null, DATABASE_VERSION);
this.DB_NAME = name+".db";
this.DB_COMPLETE_PATH = Util.getDatabasePath(DB_NAME);

if (checkIfDBExists()==false){
createDataBase();
}

openDataBase();
}

private void createDataBase() throws IOException {
this.getReadableDatabase();
this.close();
try {
copyDataBase();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private boolean checkIfDBExists() {
File dbFile = new File(DB_COMPLETE_PATH);
return dbFile.exists();
}
}

SQLiteException: no such table: database-notes (code 1 SQLITE_ERROR)

because my table is named database-notes

It would appear not, due to the failure, and is probably a misunderstanding of the difference between the database name and table name(s).

A Database can have multiple tables. The database name is the name of the file itself (the container of the components such as tables, indexes, views and triggers).

In your code database-notes, as per the 3rd parameter to the Room.databaseBuilder is the name of the database (the file).

With Room the table names are derived from the classes that are both annotated with @Entity and provided, via the entities parameter of the @Database annotation. In your case the Note class.

The name of the table will be Note unless you use the tableName = parameter of the @Entity annotation to provide another name.

Example

If the following were your Note class :-

@Entity // No tableName parameter so the table name is the class name
data class Note(
@PrimaryKey
var noteId: Long? = null,
var noteText: String,
var image: String
)

Then the table name would be Note (the name of the class)

If the Note class were :-

@Entity(tableName = "notes") //<<<<< specifically names the table
data class Note(
@PrimaryKey
var noteId: Long? = null,
var noteText: String,
var image: String
)

The the table name would be notes (as specified by the tableName = parameter of the @Entity annotation).

No Such Table error

There is no table Coffee in SQL.sqlite.

SQLite silently creates the database file if it does not exist. So if you've got the path wrong, you are opening an empty database file, which of course does not contain any tables. Make sure the database file exists there and it is not empty.

You can see what tables are there in the database by running SELECT * FROM sqlite_master; query.

problem about sqlite database, no such table:

If this is on the emulator, use DDMS File Explorer or adb pull to download a copy of the database file and confirm its contents.

And i crate a custom SQLiteOpenHelper
to copy this file to path :
/data/data/com.SGMalls/databases/mallMapv2.sqlite

If you are attempting to copy the file in SQLiteOpenHelper's onCreate(), you are too late. I would do it before then.

So the copy file code may be can not
copy the complete file.

It is more likely you would get an error about a corrupt file in that case.

R dbReadTable returns no such table error

Use

db <- dbConnect(SQLite(), "lookup_and_tracking.sqlite")

The problem is the file name parameter is not named conn=; It's dbname= and the default is "" which creates a new, empty data base.

sqlite database error no such table in android P

In your createDB function you must add this.close() after this.getReadableDatabase()

this.getReadableDatabase();
this.close();

This link can help you

Android P - 'SQLite: No Such Table Error' after copying database from assets in android version 9

This is caused because of the use of this.getReadableDatabase() before the copy.

This creates a database and since Android 9 the database is opened, by default, in WAL mode. This results in two files -wal and -shm (each preceded with the database file name) that are owned by the database which is overwritten, not by database that overwrites the former. This anomaly is detected and results in an empty database being returned and hence the table not found.

There are a few get-arounds but the suggested and most efficient way is to not open the database but to instead check if the file exists.

e.g. :-

private boolean checkDB() {

File cDB = new File(con.getDatabasePath(db_name).getPath());
if (cDB.exists()) return true;
if (!cDB.getParentFile().exists()) {
cDB.getParentFile().mkdirs();
}
return false;
}
  • Note this additionally gets around the issue why opening the database was likely introduced instead of the the file check; that is checking the file, without creating the parent directory (the databases folder) would subsequently fail when attempting the copy of the database file with an ENOENT cannot open file or directory exception.

  • databases are stored, unless otherwise specified, at data/data/the_package_name/databases/. However, when an App is installed only data/data/package_name/ exists, there is no databases folder/directory.

  • Opening the database as an SQLiteDatabase is quite expensive resource wise, a fair bit has to be done, the header checked, the schema generated and written to disk or read from disk into memory, the creation of the android_metadata table, the writing to the -wal and -shm files in addition to actually getting a File.

  • The only flaw as such with this method is that if the asset weren't a valid database in which case a Corrupt Database exception would occur. If this is an issue then you can easily check the first 16 bytes of the header as per Database File Format.

In addition to the above you should also remove/comment out this.getReadableDatabase(); in the createDB method. As this will also result in the same issue.

A more in-depth answer

SQLite database error: No such table

my problem Solved

https://github.com/jgilfelt/android-sqlite-asset-helper

its work for me



Related Topics



Leave a reply



Submit