I am trying to get writable database using DBFlow using the FlowManager like this FlowManager.getDatabase(SampleDatabase.NAME).getWritableDatabase() using DBFlow version "4.0.0-beta1" but i get an error DatabaseWrapper cannot be converted to SQLiteDatabase. I found an issue on Github Can't use existing SQLite database but i cant understand what it means. I really need to do this will be grateful for any help.
This means that the FlowManager.getDatabase(SampleDatabase.NAME).getWritableDatabase() method does not return a SQLiteDatabase object like SQLiteOpenHelper.getWriteableDatabase(). Instead, it will return an instance of DatabaseWrapper class.
If you need direct access to the database, you should be able to get the file path to the database file using FlowManager.getDatabase(SampleDatabase.NAME).getDatabaseFileName(). Use this path to open your database using the Android API. However, this is definitely not the recommended way of using DBFlow and maybe leads to unexpected behaviour.
Related
I succeeded in importing a SQLite database using SQL/SQLite toolbox for my Xamarin.Android Project under Visual Studio 2019, However I can't seem to find a way to establish a connection between my database and the project... I looked everywhere for a connection string or any plugin that would help me do the task, but there wasn't enough documentation regarding the subject.
Does anyone have an idea that can help me make a progress?
You could refer to this document Using SQLite.NET with Android.
Once you have the SQLite.NET library available, follow these three steps to use it to access a database:
Add a using statement – Add the following statement to the C# files
where data access is required:
using SQLite;
Create a Blank Database – A database reference can be created by
passing the file path the SQLiteConnection class constructor. You do
not need to check if the file already exists – it will automatically
be created if required, otherwise the existing database file will be
opened. The dbPath variable should be determined according the rules
discussed earlier in this document:
var db = new SQLiteConnection (dbPath);
Save Data – Once you have created a SQLiteConnection object,
database commands are executed by calling its methods, such as
CreateTable and Insert like this:
db.CreateTable<Stock> ();
db.Insert (newStock); // after creating the newStock object
Retrieve Data – To retrieve an object (or a list of objects) use the
following syntax:
var stock = db.Get<Stock>(5); // primary key id of 5
var stockList = db.Table<Stock>();
When using Room from the Android Architecture Components, I received the following error when attempting to access the database using a Dagger component:
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: (database path)
I was using Dagger version 2.11 and Room version 1.0.0-alpha7. The error was reproducible on version 1.0.0-alpha5.
This error occurred on any attempt to access the database through a DAO after initialising the database and injecting it into my class.
It's because you are trying to modify the schema of the existing database without giving it any migration information. So basically it attempts to write the new database schema to the existing DB which doesn't work.
There are two ways around this. If you are in your development environment what you can do is fallback to a destructive migration, to do this your database creation code would look something like the following:
MyDatabase myDatabase = Room.databaseBuilder(context, MyDatabase.class, "my-db")
.fallbackToDestructiveMigration()
.build();
This means when you provide the database with an updated or new entity it will do what the answer from #huw said and just delete the database on the application's installation removing all the data from it and give you a fresh install.
The other method is to use a migration function. They are pretty long so unless someone wants me to write it up here I'll leave it for now but basically, the documentation can be found here:
Room DB Migration Documentation
This essentially causes the DB to run some SQL provided by yourself to update the database to the new version. This way you can ensure that none of your data is lost while doing the migration; or as little as possible depending on what you are doing. This is the preferred method for production apps as it means users won't lose their pre-existing data and you won't get a lot of angry reviews/lost customers.
Hope that makes helps!
One solution to this problem was to delete the database file and start again. This was not an issue since I was only testing and could repopulate the database using online data.
To do so either:
App info > Storage > Clear Data
Manually remove the file at /data/data/com.app.example/databases/database.db
I had this kind of exception after not so successful migration. Always double-check the SQL query you use for a migration. You can create a new column with a wrong type of data and the exception's description won't be helpful.
I create Database in DB Browser For SQlite.
simple database - with one table called Students with two row:
1.id - integer
2.name -text
I want to use this database in android studio app.For example I need an app,which will print the names of students from database Students;
I've two questions:
Where Should I put the Students.db file?
How to use/read the database in my app.
I'm searching for it for a while but cant find solution.
Can you give me a good tutorial or just answer the question.
Thanks
There is a well defined pattern of making a "DataBaseAdapter" class in Android.
http://android-er.blogspot.com/2011/06/simple-example-using-androids-sqlite.html
Has an example.
you create a class SQLiteHelper that extends SQLiteOpenHelper. Then you follow the general pattern that the SQLiteAdapter class follows. This approach handles creating the sqlite db for you within your app-private internal storage.
To read from the DB, you make an instance of your SQLiteAdapter class, and then call insert(...), delete(...), query(...), etc. to actually manipulate your db.
Basically you would like to use an existing sqlite database, I think this question rely on a same idea, that answer could help you too.
Or if you don't have to use an exiting database file, your starting point can be this tutorial.
I read some info from here:
Java - JDBC Driver SQLite 3.7.2 - Unable to open database test.db: file is encrypted or is not a database
and no idea about "version mismatch" sqlite3 binary and if it is same problem with Access, how can I solve it.
Anyway, I show a part of my code below, as I said before I don't know if it is the same problem:
I created an external database without encryption with Access 2013.
My file is less than 1MB and is copy well on DDMS on "/data/data/" + getPackageName() +"/databases/".
When I downloaded from DDMS to my computer I can see all my data (open with Access 2013) so I think is not corrupted.
My problem is when is called db.getBeers() on my activity with
public void GetBeers(){
db.openDataBase();
Cursor c= db.getBeers();
db.close();
On my class DBAdapter extends SQLiteOpenHelper
public Cursor getBeers(){
return myDataBase.query(DATABASE_TABLE, new String[]{KEY_ROWID, KEY_NAME, KEY_VOLUMENFERMENTACION,KEY_AZUCAR,KEY_AGUA, KEY_DINICIAL,KEY_AZUCARBOTELLA,KEY_AZUCARBARRIL,KEY_CATEGORIA},null,null,null,null,null);
}
First error on my logcat:
E/SQLiteLog(5852): (26) file is encrypted or is not a database.
I hope to see some light on this, it took me hours and i've no idea what's wrong, so any help is welcome.
I created an external database without encryption with Access 2013
SQLite has no ability to read Microsoft Access databases.
E/SQLiteLog(5852): (26) file is encrypted or is not a database.
From SQLite's standpoint, your file is not a database.
how can I solve it.
Do not attempt to use an Access 2013 database with SQLite. If you wish to package a database with your app, and you wish to use Android's SQLite classes with that database, you need to create a SQLite database.
I've never heard that Access is an option for android platform.
It is maybe a Java only but if you want really a good offline database.
Why you don't use SQLite? It's a built in database in Android, you can easily connect to it and do all you want. Since you are seeking to use an offline database it's the best choice, trust me.
Here is a reference :
https://developer.android.com/guide/topics/data/data-storage.html#db
There are many explainations how to open Android sqlite database. However how to open non android sqlite database? In particular when opeing database using SQLiteOpenHelper I have to give the expected database version. For me it's useless.
Can I use directly SQLiteDatabase class and its openDatabase method?
I want to open database and convert it to my program android database assuming some structure.
Can I use directly SQLiteDatabase class and its openDatabase method?
Yes.