Import an External SQLite database to my project - qt

I'm interested in importing an external SQLite database to my project.
When using the QT Quick Local Storage:
LocalStorage.openDatabaseSync("QQmlExampleDB", "1.0", "The Example QML SQL!", 1000000);
The problem is that, the program generates a NEW database, and if it's possible I'm interested in open an existing database.
Any idea?
Thank you very much!

Thanks all of you, with the information you have got me I could solve my problem, all the things I have had to do are:
First of all in main.cpp I set my offline Storage Path:
engine.setOfflineStoragePath(QString("./"));
Then I also add this code:
QDir dir("./Databases");
if (!dir.exists()) {
dir.mkpath(".");
}
QString new_name = QString(QCryptographicHash::hash(("nameofthecopiedDB"),QCryptographicHash::Md5).toHex());
QFile file(":/SQLite/nameofsourceDB.sqlite");
file.copy("./Databases/" + new_name + ".sqlite");
file.close();
Since I have the DB I would to use in my project in SQLite folder from my resources (:, indicates resources).
And then, in QML file, the openDatabaseSync() function:
basedades = Sql.LocalStorage.openDatabaseSync('nameofthecopiedDB',"1.0","Els meus entrenaments",1000000,"QSQLITE")
Thank you!

openDatabaseSync searches or creates dbs in the directory used for storing offline data.
That directory is identified by the data member offlineStoragePath of the QQmlEngine class.
To change it, you can use the the member method setOfflineStoragePath (see here for further details).
The first argument for openDatabaseSync is:
The name of the database passed to openDatabase()
See here for further details.

Related

Xamarin Open UWP Open file from "Documents" library

I have a Xamarin UWP app and am trying to load a file from my current users "Documents" library.
I understand that I need to add a File Type Association declaration first. I've done this and the file icon has changed to my application icon.
In the app I'm then using the FilePicker plugin from here...
https://github.com/ArtjomP/FilePicker-Plugin-for-Xamarin-and-Windows
FileData file = await CrossFilePicker.Current.PickFile();
Byte[] data = file.DataArray;
All I do is browse to the file and select it, but the application crashes with an access violation on the second line.
I've added the following capabilities to my UWP manifest, not sure if they are event relevant anymore.
<uap:Capability Name="documentsLibrary" />
<uap:Capability Name="removableStorage" />
How do I open my files? I ideally need to use a convenient location like the documents library.
Nick.
Edit:
I've followed this article, and still no joy.
https://www.pmichaels.net/2016/11/11/uwp-accessing-documents-library/
You are using a old package which most likely is no longer supported.
Try this one, https://github.com/jfversluis/FilePicker-Plugin-for-Xamarin-and-Windows
It is quite simple, and also supports UWP.
Sample usage
try
{
FileData fileData = await CrossFilePicker.Current.PickFile();
if (fileData == null)
return; // user canceled file picking
string fileName = fileData.FileName;
string contents = System.Text.Encoding.UTF8.GetString(fileData.DataArray);
System.Console.WriteLine("File name chosen: " + fileName);
System.Console.WriteLine("File data: " + contents);
}
catch (Exception ex)
{
System.Console.WriteLine("Exception choosing file: " + ex.ToString());
}
EDIT: I reworded the answer for clarity, and to avoid some implications that #MickyD rightly pointed in comments and I don't agree with those implications either:
This seems like a bug in the package as it doesn't work according to its documentation and one possible thing to do is to submit the issue to GitHub so that the bug is resolved by the developer of the package.
The other possible thing to do is to look for the alternatives. You may try to find other similar packages and see if they work, and the alternative for which I am sure that works is to use the native functions in System.Windows.Storage (as it is officially supported).

how can I change in memory db to file db In JAVA?

The sqlite configuration in spring boot is
sqlite.datasource.url=jdbc:sqlite::memory:
I confirmed that the data was inserted, but I want to get this sqlite as a real file.
Do you know what to do?
.properties setting
spring.datasource.url=jdbc:sqlite:file:memorydb.db?mode=memory&cache=shared
test code
Connection conn = DriverManager.getConnection("jdbc:sqlite:file:memorydb.db?mode=memory&cache=shared");
Statement stmt = conn.createStatement();
File tmpFile = File.createTempFile(name, ".db");
stmt.executeUpdate("backup to " + tmpFile.getAbsolutePath());
tmpFile.deleteOnExit();
stmt.close();
Not sure why you'd want to do that because normally one would say that they want to move from using an in-memory database to an actual persistence database. So if I'm getting you correctly, you want to know how to write data to a file please refer to this article https://www.journaldev.com/878/java-write-to-file. So being able to write to a file is not an alternative but a database and doing this can co-exist.
Hope this helps ;)

Can the Path assigned a SQLite DB be an arbitrary value?

In this blog post, some prerequisite code for getting started using SQLite in Windows Store Apps is given, for adding to the OnLaunched method of App.xaml.cs:
// Get a reference to the SQLite database
this.DBPath = Path.Combine(
Windows.Storage.ApplicationData.Current.LocalFolder.Path, "customers.sqlite");
My question is: Can I use any arbitrary value to replace the "customers.sqlite" part, or does it have to match something else in my code, such as the name of my table definition class (in my case "PhotraxCoreData.cs" which, according to Mr. Green's suggestion, I added below a newly-created "Models" folder)?
My understanding is that, once I've got those classes defined (I do), and the code above in App.xaml.cs, along with this there (adapted for my SQLite classes):
using (var db = new SQLite.SQLiteConnection(this.DBPath))
{
// Create the tables if they don't exist
db.CreateTable<PhotraxBaseData>();
db.CreateTable<PhotraxNames>();
db.CreateTable<PhotraxQueries>();
}
...SQLite tables based on those classes I specified will be created, and have the name "customers.sqlite" (provided I don't change it).
So, can I use:
this.DBPath = Path.Combine(
Windows.Storage.ApplicationData.Current.LocalFolder.Path, "platypus.sqlite");
...or must it be something like:
this.DBPath = Path.Combine(
Windows.Storage.ApplicationData.Current.LocalFolder.Path, "PhotraxCoreData.sqlite");
That database name is just a file name.
The directory must be accessible by your app, but the file name can be anything.
AS CL says, the file name can be anything the app has direct access to. Windows Store apps have limited access to the file system, so the sqlite database must be in either the apps install location (read only) or it's app data folder (read write). A common pattern is to ship a seed database in the app package and then copy it from the install location to app data on first use so it can be written to.

ASP.NET creating resources at runtime

I'm developing an ASP.NET webapp that has a multilanguage feature allowing the webmaster to create new languages at runtime.
The approach that I was thinking is the following:
The user selects one available (not created) language.
When the user confirms, the application automatically copies a set of existing resources, replacing the filename with the new culture. For example: default.aspx.en-us.resx to default.aspx.es-ar.resx.
The user edits the recently created resources.
Currently I'm having troubles with step number 2. I've achieved to copy the resources, but then these new resources are ignored. I think that this happens because the new resources are not included in the running assembly, and therefore are being ignored.
When I test the following code in my local project, I would have to manually add the new resources to the solution and then recompile to make it work.
Does anyone know how to make this work?
This is the code of the mentioned copy.
string _dir = path_ + "App_LocalResources\\\\";
DirectoryInfo _dirInfo = new DirectoryInfo(_dir);
foreach (FileInfo _file in _dirInfo.GetFiles("*en-us.resx")) {
_file.CopyTo(_dir + _file.Name.Replace("en-us", idioma_.Cultura));
}
string _dir2 = path_ + "App_GlobalResources\\\\";
_dirInfo = new DirectoryInfo(_dir2);
foreach (FileInfo _file in _dirInfo.GetFiles("*en-us.resx")) {
_file.CopyTo(_dir2 + _file.Name.Replace("en-us", idioma_.Cultura));
}
Thank you very much.
Creating or editing Resource files is not possible the same way as reading data.
In order to create or edit a resource file, you should do it the same way you create or edit XML files because resource files have with a specific structured XML elements.
Maybe this article will help you...

How do I rewrite a plist if its data types are immutable?

I am getting comfortable with using plists for initializing my app. I now want to save app state back to the plist used to initialize the app and I find myself stuck. At application startup I ingest the plist into an NSDictionary which is immutable. I now want to update the NSDictionary by replacing old values with new values for existing keys and write to the plist via [NSDictionary writeToFile:atomically]. How do I get around the immutability of NSDictionary?
Thanks,
Doug
UPDATE - Not quite there yet
I followed zneak's suggestion and ingested my settings file into an NSMutableDictionary. Works fine. Prior to writing the plist out I confirm that new values now replace old values. Cool. Good to go.
Problem: when I write the file thusly:
if ([self.settings writeToFile:plistPath atomically:YES] == NO) {
NSLog(#"Unable to write plist");
}
The method happily completes properly - the conditional is YES rather then NO - but I see no file. Where has the file gone?
Shouldn't I see the new file in my directory tree?
Make it a NSMutableDictionary by calling -[myDict mutableCopy], then use this one for writing the file; or simply load the plist using [NSMutableDictionary dictionaryWithContentsOfFile:(NSString*)] to get a mutable instance to start with instead of an immutable one.
Use an NSMutableDictionary (you can use -[NSDictionary mutableCopy] to create it). You can then use writeToFile just as with an NSDictionary.

Resources