How to import data to sembast flutter? - sql

I'm developing mobile app on flutter and now selecting a database. I'm thinking about sembast (https://pub.dev/packages/sembast#-readme-tab-), but there is no information about importing (preloading) data to the database.
Does anyone know that? Should I add csv or json files with data to asset and then somehow load it into the database? In comparison, there is sqflite package from the same author (https://pub.dev/packages/sqflite#-readme-tab-) and here I can add sqlite database to asset and then import it into the project.

Sembast is not flutter only so there is no specific mention about asset file.
One solution is to preload data using the basic versioning system:
https://github.com/tekartik/sembast.dart/blob/master/sembast/doc/open.md#preloading-data
Or to import data (reading an exported map from an asset file):
https://github.com/tekartik/sembast.dart/blob/master/sembast/doc/storage_format.md#importexport

Related

Is it different to store account in dotenv and other source code files?

Most of them keep tokens or accounts separately in the dotenv file.
is it different from just saving it as a variable in another source code file and import it for use?
I've been studying API these days, and I've been wondering how to store sensitive content.

Backup data with Async Storage - React Native

I am creating a note-taking app.
I want to enable users to backup their data manually by clicking 'backup' button, which will create a db backup file.
Upon reinstalling the app, users should be able to restore their data by importing the db backup file.
Would this be possible? I've already seen posts about using Gdrive and ICloud, but this is automatic. I wish to export and import the db file of AsyncStorage manually. For example, expecting API something like AsyncStorage.createDbFile or equivalent.
Alternative would be to JSON.stringify all the data in AsyncStorage and put that on a txt file and export that, but I don't think this is a good idea (or is it perfectly fine to do this?).
Thank you!

Best way to save json data and files in react native?

I am developing a react-native application. I want it to get a json file online and print it as a list, showing the image and by clicking, showing the pdf. Here is my json :
[
{
"id":"1",
"nb_edit":0,
"nom":"Catalog 1",
"description":"test",
"apercu":"img\/1.png",
"pdf":"pdf\/1.pdf"
},
{
"id":"2",
"nb_edit":"0",
"nom":"Hi",
"description":"yes",
"apercu":"img\/2.png",
"pdf":"pdf\/2.pdf"
}
]
It actually works fine. But now, I want to add an offline mode. For this, I want to basically set a version variable in the application to 0, and check if the version on the website (version.txt) is the same. If yes, just load the json file saved in the phone (the png and pdf are saved too). If not, download the json, update the json saved locally, update the version variable, and then load the files from the phone.
Do you have an idea of how could I do? I thought about using redux-persist for the version, but will it work for the json, the images and the pdfs, and how?
Thanks for your help.
You can use :
Option1:
AsyncStorage is an unencrypted, asynchronous, persistent, key-value
storage system that is global to the app. It should be used instead of
LocalStorage.
https://reactnative.dev/docs/asyncstorage.html
Option2:
Local database
SQLite is an open-source SQL database that stores data to a text file
on a device. It supports all the relational database features. In
order to access this database, we don’t need to establish any kind of
connections for it like JDBC, ODBC.
react-native-sqlite-storage
https://www.npmjs.com/package/react-native-sqlite-storage
I would not try to save the file to the device. Instead of trying to save the file, I would save data to the device's storage by using AsyncStorage (If the file is not that big). If you go in this way, do not forget to stringify the object while saving the object to the storage.
You may use react-native-fs for saving files such as pdf, images.

Objective C: How to store static data to Core Data when application launches

I am intending to use Core Data to store a static list of data which consist of objects with attributes (e.g. object = person, attributes = weight, height etc).
Any advise on what is the best way to approach this? What raw file format should I need to use and what is the best way to load the data from the raw file to core data?
Appreciate any advise and help on this
If the data set is large, you should include a prepopulated SQLite store in the app bundle.
Create a separate project in Xcode and import the data model and NSManagedObject subclass files (if any.) Write code to import or create the static data and write it all to the persistent store.
Copy/add the persistent store file to the release project. In the release project, set the store pathway to the location of the store file in the app bundle using [NSBundle pathForResourse:Type]. In the persistent store options, mark the store as readonly.
That will let you set up a prepopulated, readonly Core Data stack that will efficiently handle thousands of objects for you.
I'd suggest shipping binary .plist file with initial data and on first launch dump it to CoreData.
Also may I suggest you not use CoreData if your list is going to be immutable. Just read the plist on app startup and keep it in some AppDelegate's instance var.
Update (to reflect comment):
If it's a few thousand records - you SHOULD import it to core data as it would improve the performance of data retreval. Also - I would not suggest keeping ALL of them in the memory.
To get data into plist you could use some other programming language to select all the data from database and export it to xml plist (there are libraries for virtually any programming language). Then by using property list editor you would be able to export it to binary plist.
Then depending on what you need to do with your data - you can import it either in background or just show a progress indicator/bar to the user while using main thread for import. I believe plist would take few MB's. Also - try benchmarking a thousand records just to get approximate estimate of how long data import would take.
1, You could fill in core data on startup from external source. On startup, read data from external source(SQLite database or XML file), then insert the data into core data;
2, Provide pre-filled in SQLite database. For this we’ll let Core Data create the database structure for us based on the model, and then we populate the database with a utility app. The utility app could be a Mac or iPhone app that uses Core Data to populate the database via Core Data APIs, or some kind of program that fills in the SQLite database directly. Once the database is populated, just include it with the app and make the app use it as the default database if no database already exists.
Hope this tutorial will help.

Pre loaded database on iPhone?

I developing an app that is read-only. The data has relationships, so I cannot just use a plist or something similar.
Questions:
Should I use Core Data for such a requirement?
If so, how would I enter the data and then release the app with that data?
How would I make it so that the app doesn't need to re-populate a DB every time it loads?
Is there a way to create a Core Data model using sql commands with sqlite (i.e. insert into, etc)?
You may use an SQLite database to accomplish this.
Create the model in your iOS app.
Create and populate the database in a Mac OSX utility command-line app
Copy the sqlite file into your iOS app and link it with some code
Work through these two tutorials, line by line, and afterward you will have a good enough understanding (and code sample) to complete this task in your own app.
Core Data on iOS 5 Tutorial: Getting Started
Core Data on iOS 5 Tutorial: How To Preload and Import Existing Data
In my short experience with the iPhone, you have two options.
Write a data import function and run it on the first application launch.
Use solution 1, but build the initial sqllite file in the simulator, and then on first application launch, copy it into the app's documents directory.
From past experience, option 2 is much quicker for user experience, and the preferred solution.
You can write a utility project that imports the app's data model and use throwaway code n that project to populate the Core Data DB. Once the DB is populated, simply copy the actual file to the app project's resources folder and then when you set up your persistent store, use NSBundle to return the path to the DB file within the built app.
And you're done.