Import data in Windows phone from SQLITE file - sqlite

Well, The problem is that I have a SQLLITE database file which has data and can be browsed in SQLITE browser. I want this file to be imported in my windows phone app so that I can bind my Lists.
I have been googling and trying for a solution but no luck yet.
public async void UpDatabase()
{
bool isDatabaseExisting = false;
try
{
StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("DTEMPCounselling");
isDatabaseExisting = true;
}
catch
{
isDatabaseExisting = false;
}
if (!isDatabaseExisting)
{
StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync("DTEMPCounselling");
await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
}
}
Problem with above code is it searches it in the emulator filesystem while my database file is in my C: drive.
Any suggestion how can I achieve this.
Thanks.

The code I used here was all right. But the real cause of the problem was the idea that the Emulator and the hosting machine work as two different systems. Means, to be able to read the database file in Emulator it should have been placed in Emulator's file system.
Putting the file in Emulator's file system solved the problem.

Related

Creating PDF document from dotnet Core 2 using node service

I need to create a pdf file from an HTML on the server-side (dotnet core 2) and send it as an attachment of an email. I have created a node service (createPdf.js) as follows and kept it in a local directory (NodeService) within the solution -
module.exports = function (callback, html, outputFilePath) {
var pdf = require('html-pdf');
var options = { format: 'A3', orientation: 'portrait' };
pdf.create(html, options)
.toFile(outputFilePath, function (err, res) {
if (err)
return callback(null, false);
callback(null, true);
});
}
And I am triggering this function as follows -
public static async Task<bool> GeneratePdf(INodeServices nodeService, string html, string outputFilePath)
{
string pdfGenerationNodeService = Path.Combine(Directory.GetCurrentDirectory(), "NodeService", "createPdf.js");
try
{
return await nodeService.InvokeAsync<bool>(pdfGenerationNodeService, html, outputFilePath);
}
catch (Exception ex)
{
throw;
}
}
For calling this method from the controller -
public async Task<IActionResult> SendQuotationToHospitalAsync([FromServices]INodeServices nodeService, int id)
{
...
bool isAdminPdfGenerationSuccess = await PdfHelperService.GeneratePdf(nodeService, htmlContent, filePath);
...
}
I have also registered the node service in StartUp.cs -
services.AddNodeServices();
When I am triggering the function in debug mode, it's working properly and the pdf file is getting generated. However, once I deploy the application on the server, the node function is not getting triggered.
Any help regarding the issue will be very helpful. Thanks.
P.S. As it is a home project, I can not afford any premium HTML-to-PDF converter
You may have a look at this as a working example of what you are trying to achieve with NodeServices. It runs on Docker, so you can get a hint from the Dockerfile of what you need to have installed on the server to get it working there as well.
Another approach is to use PuppeteerSharp, as follows:
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true,
Args = new string[] { "--no-sandbox" }
});
var page = await browser.NewPageAsync();
await page.SetContentAsync(htmlContent);
await page.PdfAsync(filePath); // page.PdfStreamAsync() to generate only in-memory
I'm using angular 7 for client-side and had npm packages installed accordingly, which included the html-pdf package as well. However, I had not installed it in the directory where I have kept the node script. I was hoping that the package would be taken from the vendor.js after deployment, which was clearly not the case. I had to create another package.json file in the directory and installed it separately after deployment and everything went smooth thereafter.
That's what I was missing when deploying the application - a little manual npm install for installing the package I'm using, in the local directory of the node script.
Thanks a lot for discussing the issue, it helped me a lot in understanding what other mistakes I might have done.

Flutter Firestore take long retrieving data while offline

I am using Firestore in flutter application. Each time user launch the application it retrieves some data from Firestore Cloud.
QuerySnapshot dataSnapshot = await Firestore.instance
.collection('/data')
.getDocuments();
When user opens the application on first time, it required from him to connect online, to get the data, and as Firebase documents say
For Android and iOS, offline persistence is enabled by default. To disable persistence, set the PersistenceEnabled option to false.
So, it should save the data that application have been read before to retrieve it while the device is offline; so user can access application at anytime with the same data that have been read.
The problem is: it takes too long time to retrieve the data while the device is offline, with the same codes and nothing changed!.
I tried to configure how much time it takes? On offline, it takes about 8 minutes and 40 seconds. But while on online, it takes just 10 seconds, maybe less.
So how can I solve this problem?
============
UPDATE
I manged to get more logs about this problem, which after take a lot of time, and will start application with the offline saved data, it prints this log
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
And then take 3 second for example (not much time) and continue with the next works.
I did open a new issue in GitHub too.
Is there a way to limit the time it takes?
And finally, with the help of diegoveloper comment in GitHub issue, I have reached the solution.
This comment
await Firestore.instance
.collection("Collection")
.getDocuments(source: source)
was a good solution if I decided to check source each time and then use it or I can use it in starting of a new Flutter project, but now I already have a lot of codes that need a better solution. So I decided to fork the cloud_firestore package and edit it.
You can find it here: https://github.com/ShadyBoshra2012/flutterfire/tree/master/packages/cloud_firestore
What I have edited:
firestore.dart
// The source of which the data will come from.
static Source _source = Source.serverAndCache;
static Source get source => _source;
Future<void> settings(
{bool persistenceEnabled,
String host,
bool sslEnabled,
bool timestampsInSnapshotsEnabled,
int cacheSizeBytes,
Source source}) async {
await channel.invokeMethod<void>('Firestore#settings', <String, dynamic>{
'app': app.name,
'persistenceEnabled': persistenceEnabled,
'host': host,
'sslEnabled': sslEnabled,
'timestampsInSnapshotsEnabled': timestampsInSnapshotsEnabled,
'cacheSizeBytes': cacheSizeBytes,
});
if (source != null) _source = source;
}
query.dart
source = Firestore.source; Line 92
document_reference.dart
source = Firestore.source; Line 83
How you can use it?
So you can use my forked repository in this way with using connectivity package from Google : https://pub.dev/packages/connectivity .
Add my forked repository in pubspec.yaml file
cloud_firestore:
git:
url: https://github.com/ShadyBoshra2012/flutterfire.git
path: packages/cloud_firestore
Then in your first screen or main
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.none) {
await Firestore.instance.settings(source: Source.cache);
} else {
await Firestore.instance.settings(source: Source.serverAndCache);
}
and if you want to refresh the source when change the connection state:
StreamSubscription subscription;
void initState() {
super.initState();
// Check the internet connection after each change
// of the connection.
subscription = Connectivity()
.onConnectivityChanged
.listen((ConnectivityResult result) async {
// Check the internet connection and then choose the appropriate
// source for it.
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.none) {
await Firestore.instance.settings(source: Source.cache);
} else {
await Firestore.instance.settings(source: Source.serverAndCache);
}
});
}
#override
void dispose() {
super.dispose();
subscription.cancel();
}
So I hope it works with everyone see it, and waiting for Flutter Team to code a better and better solution. Thanks for everyone has participated.
In addition to Shady Boshra's answer you can use FirebaseFirestore.instance.disableNetwork() functionality so your code will look like this:
StreamSubscription subscription;
void initState() {
super.initState();
// Check the internet connection after each change
// of the connection.
subscription = Connectivity()
.onConnectivityChanged
.listen((ConnectivityResult result) async {
// Check the internet connection and then choose the appropriate
// source for it.
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.none) {
await FirebaseFirestore.instance.disableNetwork();
} else {
await FirebaseFirestore.instance.enableNetwork();
}
});
}

Xamarin.Forms how to backup a SQLite database in SD card

I have a SQLite database in my project and I'd like to add a backup function to save that db in the SD card.
Is there any simple way to do this for both Android and IOS using Xamarin.Forms?
I searched among the old (some of them are veeery old) questions but I couldn't find a clear answer.
iOS:
There is no "sdcard" on iOS, but you can copy (File.Copy) the db to the App's Documents directory so it is accessible via the iTunes app so you can copy to your PC/Mac:
Use this directory to store user-generated content. The contents of this directory can be made available to the user through file sharing; therefore, his directory should only contain files that you may wish to expose to the user.
The contents of this directory are backed up by iTunes and iCloud.
So assuming you are using App Support directory for your DB, which you should be:
public string GetDBPath(string dbFileName)
{
// If you are not using App Support directory for your DBs you are doing it wrong on iOS ;-)
var supportDir = NSSearchPath.GetDirectories(NSSearchPathDirectory.ApplicationSupportDirectory, NSSearchPathDomain.User, true)[0];
var dbDir = Path.Combine(supportDir, "database");
if (!Directory.Exists(dbDir))
Directory.CreateDirectory(dbDir);
return Path.Combine(supportDir, dbDir, dbFileName);
}
Copying to your App's doc directory is as simple as:
public void CopyDBToDocs(string dbFileName)
{
var docDir = NSSearchPath.GetDirectories(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User)[0];
File.Copy(GetDBPath(dbFileName), Path.Combine(docDir, dbFileName));
}
Android:
On Android, it can be really simple, or a pain, depending upon the API of the device and what your Android API target of the app that you are developing.
As simple as:
public void CopyDBToSdCard(string dbName)
{
File.Copy(GetDBPath(dbName), Path.Combine(Android.OS.Environment.DirectoryDownloads, dbName);
}
GetDBPath in this case is actually using the "true" database directory which is the subdir from your app's sandboxed "FileDir" location:
public string GetDBPath(string dbFileName)
{
// FYI: GetDatabasePath("") fails on some APIs &| devices &|emulators so hack it... (Some API 23 devices, but not API 21, 27, 28, ...)
var dbPath = context.GetDatabasePath("ZZZ").AbsolutePath.Replace("/ZZZ", "");
if (!Directory.Exists(dbPath))
Directory.CreateDirectory(dbPath);
return context.GetDatabasePath(dbFileName).AbsolutePath;
}
Now to actually perform that file copy to the "external" location from the app, there is the app's manifest permission that is required:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Also if your app has "targeted" and is running on API-23 (Android 6.0) or above, you need to have the user consent to that permission at runtime.
Using the Android.Support.Compat is the easiest way. Basically you need to check if the permission has already been granted, if not show the user why you need permission and then let the OS ask the user to accept/deny the request. You will then be notified on those perm results.
Note: There is the Forms' Perm plugin from JamesM for the non-bold coders ;-) PermissionsPlugin
void GetExternalPerms()
{
const string writePermission = Manifest.Permission.WriteExternalStorage;
const string readPermission = Manifest.Permission.ReadExternalStorage;
string[] PermissionsLocation =
{
writePermission, readPermission
};
if ((ContextCompat.CheckSelfPermission(this, writePermission) == Permission.Granted) && (ContextCompat.CheckSelfPermission(this, readPermission) == Permission.Granted))
{
return;
}
if (ActivityCompat.ShouldShowRequestPermissionRationale(this, writePermission))
// Displaying a dialog would make sense at this point...
}
ActivityCompat.RequestPermissions(this, PermissionsLocation, 999);
}
Then the results of the user accepting or denying that request will to return via OnRequestPermissionsResult:
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
switch (requestCode)
{
case 999: // This is the code that we supply via RequestPermissions
if (grantResults[0] == Permission.Granted)
{
// you have permission, so defer to your DB copy routine now
}
break;
default:
break;
}
}

UWP PreLoaded SQLite

I am trying to copy a preloaded SQLite db into my UWP app. On the initial installation it copies the "test.db", but the size is 0 bytes and there are no tables or data. The original db is 1300 bytes with data and tables.
Another factoid...when I create the app Using Visual Studio 2017 and compile and run/debug the app it works fine, but when I sideload the appx file or download from the Windows Store the db is empty.
Here is the code that I am using:
Task task = CopyDatabase();
private async Task CopyDatabase()
{
bool isDatabaseExisting = false;
try
{
StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("Express.db");
isDatabaseExisting = true;
}
catch
{
isDatabaseExisting = false;
}
if (!isDatabaseExisting)
{
StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync("Express.db");
await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder, "Express.db", NameCollisionOption.ReplaceExisting);
}
}
I'm not getting any error messages.
Does the your database file deployed correctly to the target system?
To confirm it, see your deployed - "Package" - folder. Open command prompt with administrative previleges, and see the directory
c:\Program Files\WindowsApps\your-app-id
If your database file deployed successfully, you can see it in the directory. If not, you may need to change the deploy settings.
To deploy the file to target machine, you should set the property of the one as ...
'BuildAction=Contents'
'Copy to output directory'='Always Copy'
You can set it from solution explorer and right-click the your database file.
If you succeeded the deploying file, your code will copy your database file to app local folder.
c:\Users\YOUR-USER-ID\AppData\Local\Packages\YOUR-APP-ID\LocalState
First, you would need to use await for your CopyDatabase method.
Second, I suggest you call this method in MainPage_Loaded event handler instead of MainPage's Constructor.
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
}
private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
gui = this; InitializeComponent();
await CopyDatabase();
DataSetup();
CreateNewChartButton.Visibility = Visibility.Collapsed;
SignInButton_Click(null, null);
}

Predefined database in windows phone 8.1 app

Where predefined database (.db) should add and how to use it in windows phone 8.1 app?
I am not using Silverlight in my app.
I was trying to do something like this
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
CopyDatabase();
}
private void CopyDatabase()
{
IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication();
String DBFile = "myDB.sqlite";
if (!ISF.FileExists(DBFile)) CopyFromContentToStorage(ISF, "Assets/myDB.sqlite", DBFile);
}
It showing that the namespace name IsolatedStorageFile could not be found.
I found those codes in a sample database app for Windows-phone-8.0 and I was trying to do the same thing in Windows-phone-8.1 (without Silverlight).
As I see you try to copy the database from package to IsolatedStorage and you are targeting WinRT. The sample code can llok like this:
private async Task<bool> CopyDatabase()
{
StorageFolder packageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile file = await packageFolder.GetFileAsync("Assets/myDB.sqlite");
await file.CopyAsync(localFolder);
return true;
}
I've written this code from the top of my head, but should work or help you to find the solution. The above is also possible by Uri schemes:
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(#"ms-appx:///Assets/myDB.sqlite"));
More about Data and Files you will find at MSDN.

Resources