How to open non android sqlite database - android

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.

Related

DatabaseWrapper cannot be converted to SQLiteDatabase

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.

DB Browser && Android Studio

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.

Where is SQLite database located on android

I'm new to programing for android and i'm still learning. So I have a question about the location of the SQLite database. Is it stored in the same file system as the application ?
And also i'm not sure can the database be created before the app is installed(can it come with the app) or can the database only be created from inside the app ?
And what if i wanted my app to come with a database that already has tables and records which is local and not on a server. Would that be possible ?
SQLite is available on every Android device. Using an SQLite database in Android does not require any database setup or administration.
You only have to define the SQL statements for creating and updating the database. Afterwards the database is automatically managed for you by the Android platform.
Access to an SQLite database involves accessing the filesystem. This can be slow. Therefore it is recommended to perform database operations asynchronously, for example inside the AsyncTask class.
If your application creates a database, this database is by default saved in the directory DATA/data/APP_NAME/databases/FILENAME.
The parts of the above directory are constructed based on the following rules. DATA is the path which the Environment.getDataDirectory() method returns. APP_NAME is your application name. FILENAME is the name you specify in your application code for the database.
You can also follow this tutorial for further understanding.
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
Database is created after the app installation or after the db changes. The db is stored in /data/data/your_package/databases/

Import sql script in Android application

I'm developing an android application which use SQLite database.
The question is: can I import an external script (i.e. script.sql) which can upgrade the structure of SLQLite database in my application?
Why not, SQLiteDatabase class has "rawQuery" method which can perfrom any valid sql commands. So you only need to create the database, read your script from file and feed it to SQLiteDatabase instance

How to delete re-create tables in a SQLite database from Android when an application is started?

I am building an application which has a database with two tables created internally using SQLiteOpenHelper.When ever the application is running,it receives some data and saves it into the tables.What I want is to clear the data tables when ever the application is started?
I looked into this post How can I clear an SQLite database each time I start my application? which is not clear of how to use application.
SQLiteOpenHelper has the ability of creating in-memory databases if you pass the constructor a null name. Probably this is what you are looking for.
For example:
SQLiteOpenHelper sqloh = new SQLiteOpenHelper(context, null, null, 1);
SQLiteDatabase sqldb = sqloh.getWritableDatabase();
will create an in-memory db.
SqliteDatabase uses the method openOrCreate(...) which opens a database if it exist and creates and opens it when it doesn't exist. see docs based on that you could just delete the database file that is created before you do any database calls so that a new one is created each time.
This SO question gives the location of the file: Location of sqlite database on the device
There other route would be just to delete the data in the tables when the application starts by executing a sqlite query:
DELETE FROM your_table
My only thought would be do you really need a database if you are going to delete it every time the application starts. If you are not updating the data then why not just "cache" a json file with the data. The GSON library is awesome for taking json and converting it to java object with very little code, going to be less code than working with sqlite. But the recommendation comes from not having the big picture for what you are trying to accomplish. You then would just delete the json file(s) when the app starts instead of the db file.

Categories

Resources