Import sql script in Android application - android

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

Related

How to import a .sql file to the android room database?

Does anyone know how to import a downloaded sql file to the android database via room?
I have a .sql file in android phone which is exported from mysql database. I want to import the .sql file to the android sql database.
I try to use the room of android JetPack. I check the official site:https://developer.android.google.cn/training/data-storage/room/creating-views
But dont find a way to do that.
The content in my .sql file is like:
DELETE FROM NewsInfo WHERE id = '20210524210624';
INSERT INTO NewsInfo VALUES ('20210524210624','2','aaa','bbb',3,'','','','en','push','2021-05-24 21:06:24');
So does anyone know how to import a downloaded sql file to the room on Android?
Thanks in advance
The best way I know of is to retrieve the actual db object from Room that has the execSQL() function.
And stream in the .sql file for each line db.execSQL(line);
(edit) If I knew this information wasn't readily available I would have posted the example, but I actually couldn't find it myself.
RoomDatabase database = ...
SupportSQLiteDatabase db = database.getOpenHelper().getWritableDatabase();
db.beginTransaction();
db.execSQL(line);
db.endTransaction();
You can simply call the query method on your Database instance.
appDatabase.query("YOUR INSERT QUERY")
So call the above method for each query you get in the .sql file.

How to open non android sqlite database

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.

Testing a SQLite database on Android

I am working on JUnit tests for my Android app which uses a SQLite database. I need to create a test database which can be manipulated by test cases. The database needs to start in a known configuration and reset for each test case. I figured out that I can use an in-memory database by passing the SQLiteOpenHelper a null value as the file name for the database (see this article for details). Now I also want to populate the database table for some of my tests. I have a CSV file on my local file system with some data that I can use for testing. How do I get this data into my SQLite database on an emulator or other device for testing?
There is no built-in CSV import function in the SQLite library code.
However, the sqlite3 command-line tool can import CSV files. Once imported, you have a database file that you can just copy to your device with the adb tool.
Alternatively, you can use sqlite3's .dump command to generate the SQL commands to recreate the database; you then put them into a string array, or put them into a text file read by your test program.

Android - Download SQL Database and Search

I've been working on testing trying to get an SQL database off of a website then take all the records and store them into arrays. Problem is that I've tried a ton of downloading methods and I keep getting hit with an error with opening the SQL database. I can't tell if this is actually downloading, if the database is corrupt (I checked it and it looked correct) or if my queries are messed up. Bear with me because I am just learning SQL and Android development.
The file at the link you provide is not a SQLite database. It's an ASCII file that contains SQL statements for creating a database and inserting content. There's no straightforward way to execute the content of that file in an Android app.
To make this work you need to create an actual SQLite database from your SQL file. To do that, install SQLite for whatever your desktop OS is and use this command:
sqlite> .read <filename of your SQL file>
Replace the file on your website with the SQLite file that creates. That should get you past the open error.
What are you doing here is download your database from url and stored it in sdcard.
Now you should open a empty database in your app and then copy your database in your app database and then try to open it.(you are not copying the database) the default location of database is
private String DB_PATH = "/data/data/your package name/databases/";

Exporting MySQL data into SQLite database format and store in SQLite database

I have to use remote MySQL database information in my Android application.
Is there a way that I can Translate MySQL data to SQLite format and store them in the SQLite DB to access by the mobile application.
Also I need to update the SQLite DB content when a modification happen to MySql data.
That I have decided to do when application loads, to check any modification happened and to change the SQLite DB Accordingly. Is this possible.
Any sample coding/ Idea/Link is highly appreciated.
Thanks in advance
Can you use mysqldump to export the information, for later import into SQLite? mysqldump will export data you specify, and save it into a file containing a bunch of SQL statements to rebuild the data.
So for example, if you run the dump on a schema with 3 tables and a bunch of rows in each, you end up with a .sql file with commands to build those 3 tables and then enter all the rows. The SQL can be used to import into another MySQL database or most other SQL databases.
mysqldump
I would think you could create a batch script of some sort to run the dump command, then use SQLite to pick up and run the .sql file.

Categories

Resources