which would be the steps to include a prepopulated sql database? [duplicate] - java

This question already has answers here:
Ship an application with a database
(15 answers)
Closed 2 years ago.
I am building a Quiz app to practice what I've learned so far. I want to retrieve data from the sql lite. Entering data in a non embedded RDMBS is trivial. With SQL lite I just don't know what to do. What steps you guys suggest? I am using Room database.

Room 2.2.0 and higher has built-in support for initializing your database from an existing database.
So, if you wanted to hand-populate your database, roughly speaking, the steps would be:
Set up your Room entities and RoomDatabase in your app
Run your app and have Room create an empty database for you
Copy that database off of your device or emulator (e.g., using Android Studio's Device File Explorer tool)
Use a SQLite client to add rows to your tables with the proper data
Put that database in assets/ within your project
Modify your RoomDatabase setup to use createFromAsset()
If it is more convenient for you to maintain your data in some other format (JSON, XML, CSV, whatever), you have two main choices:
Generate the Room database from that data on your development machine (e.g., via a custom Gradle task) and package the database, or
Package the raw data (e.g., JSON) in your app, parse that, and use that to populate your database
For example, in this sample app I wanted to demonstrate Room's full-text search (FTS) support. I wanted to use the contents of a book, and that was going to be easier to maintain as plain text files. So, I package the plain text files in the app and populate the Room database from there.
(I am hoping to have a createFromAsset() sample in the future)

Related

What data storage should I use for storing lyrics in my app?

I'm building a songbook Android application that displays songs' lyrics that I provide and some data like how many likes it's got (user is able to like a song), etc. Something like Spotify but only with lyrics. I need to store over 700 songs and I don't know what method to choose. It would be great if I could display the data on a website in the future too.
I've heard of JSON and XML but never used it. I am familiar with SQL databases like MySQL or SQLite, but I need to store almost pure text (including new lines) with some meta data and I think a database will not be efficient. I thoght of storing txt files in SQLite BLOB format. Also it would be great if the user would not have to update the app after adding one song to the database. BUT I also need it to work when user is offline.
To this day I've only build desktop apps with no networking and no API use. I am ready to learn this so if you have an idea what should I choose, could you provide any tips how to start?
You could use SQLite to store those lyrics. According to SQLite documentation you can store string or blob with a length up to 2147483647. You could start by learning how to use room to implement the database in Android.
To add new songs, you would probably need to implement an API client, and I recommend you learn about Retrofit. Your app would check an API to determine if there is a new song and insert it into the SQLite local database.

SQLite Databases

I am fairly new to programming java and I just started working with SQLite databases. A school assignment is requiring me to create a stand alone GUI program that can store data. After some research, I will be using a SQLite manager downloaded from Firefox. After completing my project, will it still able to run stand alone? Or will the SQLite manager be required to input data. Thank you
Yes, if you include the respective SQLite libraries. In fact there is little need for the SQLite Manager although the resultant file could be copied and used.
In short the SQLite database is a file that you open (connect to) using the respective library functions/API. Noting that some functionality may depend upon the version of SQLite (which could well be lower on the SQLite Manager).
You could also manage without the SQLite Manager, creating the database and tables therein within the program. Generally you'd use a SQLite Manager to provide a pre-populated database (noting that if using a pre-populated database that identifiers (table and column names) should match (case doesn't matter)).

Android local database

I'm creating an app that contains movie quotes,I would like to store somewhere a file (maybe an excel file) that contains both quotes and movie genre.
The file must be local (because the app works offline for now),shold I use an xls file with the quotes in the first column and the genre in the second or should I use some sort of database?
The file is needed only when the app starts because I will load the data inside arrays
You definitely should use database in this case.
Instead of writing database manager for SQLite from scratch I would recommend to use some existing library for this purpose e.g. http://satyan.github.io/sugar/
There are also alternative libraries based on a different model e.g. https://realm.io/docs/java/latest/
It is up to you which library to use or maybe to write your own from scratch, but you definitely should learn how to use databases in Android development.

How can I view webpages and save data to a database using Java?

I am collecting data from a website and trying to save it to a database (or something similar that is very accessible) rather than having a heap of files on my desktop or in a folder.
There are many pages that I need to look at (1900 to be exact). I want to save time in getting this data, and decided to make a Java program to do this.
This is basically what I am trying to do.
Visit the webpage: www.TestWebsite.com/items/0
Save the (Name, Description, Image(png)) into one array/class to a Database.
Repeat until I get up to: www.TestWebsite.com/items/1899
I want to be able to access this data offline without having to need to go online to view it.
Any ideas on how I should start. I have made a basic webpage viewer, I am just missing the step in between saving the strings and images to a database.
I appreciate any help!
Actually just did this the other day. I used jsoup to scrap the webpages I needed and wrote to my local database. awesomely easy framework for webpage parsing.
It's fairly straightforward, but you'll need to learn a little SQL if you haven't already.
You'll also have to pick a database platform - I'd suggest SQLite for such a purpose, since the data is for personal use and it's lightweight and easy to set up.
Here's a tutorial on using JDBC (Java Database Connectivity) to talk with a SQLite database: http://en.wikibooks.org/wiki/Java_JDBC_using_SQLite. It goes from setup to inserting data, so once you've completed that it should be straightforward to modify your webpage viewing code to grab the data you need and shove it into the DB.
Good luck!

Creating many small databases to be accessed by a webapp

I have this requirement for my business. We have a swing desktop application that works with a mysql database. At the end of each day the swing app exports the data that has changed and uploads it to a server. The set up is, a user working in an office, will have many companies that he is working with. If he changes any data for that company, then I export that company's data alone from the database. The data is exported in the form of java objects, serialised and stored into a file which gets uploaded.
The next day, if there are any changes made to that company again then I will replace the file in the server with the latest uploaded file.
Now on my server, I would like to work with this file. I would like to convert each of these files into mini databases that a webapp can read. It will not write to it. Everytime the user uploads, the database will be deleted and recreated.
So ultimately each of these files are a small subset of the data that a user has in his desktop application.
Now this issues are:
The objects that I have exported are "Apache Torque" objects. Torque is an ORM tool, basically the object represents the table. I need to convert this object into a database. Sqlite, HSQLDB, Derby...? The database should be small. If the object file is about 5KB, then the database that represents that file shouldnt be 3MB. Derby did that actually.
The java object classes could change. Since the underlying database could change. Hence I will need to deserialise these objects and create a database from it as soon as it is uploaded. Otherwise, I will not be able to deserialise these objects later on. Small changes to the database is fine for the web application. But if I dont deserialise it immediately, then I am stuck.
The conversion from the java object to the database should be fast. Since the user actually waits when his data is getting uploaded I would like to add a maximum of 5-10s additional for the conversion.
Is it ok to have thousands of these mini databases lying around? Is this design okay? Is there an alternate solution?
I wouldn't try to put each dataset into its own database. I would put all of them in one big database, along with a column in the key tables indicating the dataset that each row applies to (this sounds like it should just be a company identifier). This is a more normalised design than having many small databases.
You will then need to write the webapp so it makes queries for particular datasets, rather than connecting to a particular database.
if you adopt that approach, you can deserialize and store the datasets as soon as they arrive. The storage is simply inserting rows into an existing database, so it should be very fast.
In addition, i expect that one big database will be much easier to manage, maintain, report on, etc, than many small databases.
If you tell us more about the details of your schema, we could discuss how the database could be organised, if that would be useful.

Categories