Cant find sql lite file on android - sqlite

I created a SQL Lite database and embbed in my app as a android asset the question is where the heck is the file stored on the device I used the following code to access the database
public Database()
{
var path = Path.Combine(System.Environment.
GetFolderPath(System.Environment.
SpecialFolder.Personal), "StockApp.db");
Console.Write("OPening Database dbPath" + path);
database = new SQLiteAsyncConnection(path);
}
When I search for the file i cannot find it but when I use the internal debugger it puts the file at the following location.
It finds it fine and it does place a row in the table
path "/data/user/0/com.companyname.StockApp/files/StockApp.db" string
But where is the actual file I have a STK7 DEVICE for testing so Do any ideas?

Related

How can I install a database in a titanium alloy project?

I have been trying to install a tiny simple database into my titanium project with the code from the documentation and trying different tweaks as suggested by a variety of sources. But I cannot get it to work, I get errors like "no such table" or resultset is null. Currenty my code looks like this:
var db = Ti.Database.install('../assets/exercises.db', 'exercisesDB');
Ti.API.info('installed '+ db.getName() );
db.close();
Ti.API.info('closed db' );
db = Ti.Database.open('exercisesDB');
Ti.API.info('reopened db' );
//Ti.API.info(db.getName() );
var exercisesDBRS = db.execute('SELECT id,name FROM exercise');
I have tried putting the database file exercises.db in the assets folder and in the Resources folder but I'm getting no where. I created the db file with "DB Browser for SQLite" ver 3.9.1 on OSX Sierra - is it compatible with appcelerator alloy projects? My current code produces the error "no such table" on the execute call. I assure you the db file has an exercise table.
PS, full code in new projects index.js file is as follow:
var db = Ti.Database.install('exercises.sqlite', 'exercisesDB');
Ti.API.debug('installed '+ db.getName() );
db.close();
Ti.API.debug('closed db' );
db = Ti.Database.open('exercisesDB');
Ti.API.debug('reopened db' );
var exercisesDBRS = db.execute('SELECT id, name FROM exercise');
Ti.API.debug('executed select');
Ti.API.debug(' rowcount== '+ exercisesDBRS.rowCount);
while (exercisesDBRS.isValidRow()) {
var exId = exercisesDBRS.fieldByName('id');
var exName = exercisesDBRS.fieldByName('name');
Ti.API.debug("Exercise: "+exId + ' ' + exName );
exercisesDBRS.next();
}
exercisesDBRS.close();
db.close();
function doClick(e) {
alert($.label.text);
}
$.index.open();
New project but same code and a copy of the same database file in the app/lib folder. Same error - no such table on the execute line when tested in android, works fine in iOS sim.
As per the docs, your external database should be located at the same place where you are running the below code:
var db = Ti.Database.install('../assets/exercises.db', 'exercisesDB');
There's no need to use ../assets/. You can simply refer the database without it because everything you put in assets folder is moved to respective Resources->iphone/android folder.
You can always use Ti.Database.install method for external database because after installing a database once, it will behave like Ti.Database.open('exercisesDB') method.
Coming to your primary concern, you can always safely put your database file in app -> lib folder (create lib folder if it's not present and put db file in there then).
After putting the db file there, you can always use below code:
var db = Ti.Database.install('exercises.db', 'exercisesDB');
Though, I have never used .db extension, instead I have always used .sqlite format (since docs also says SQLite database) and it has worked 100% correct every time.
So you can try this process by changing the database format to .sqlite and put it in app->lib folder. It will definitely work and after verifying you can use same code on using a .db file and see if it's supported or not.
On Android: Follow these steps:
1- In your PC, go to the project's root directory, delete build and Resources folders.
2- On device, clear app data from Settings and then delete the app.
3- Now install the app again with the same code and flow as I mentioned here.
If it's working fine for iOS sim, then it should also work for Android (only some cleaning issues are there or your previous database is still there).

How to use pre-designed SQLite in UWP project?

I have a SQLite database from another project and I want to use it in a UWP application. But I don't know how to import this database to the project and!
I can create a new Database and use it but I don't know how to copy database file from project. I use from SQLite.Net-PCL nuget package.
For how to access an exist file, there are two locations that all apps can access. Details please reference the file-access-permissions.
One is Application install directory. As #Henk Holterman said, you can import your existed database file into your project by right-click one folder and select Add->Existing item to add your database file to the project. Pay attention the file's Build action property need to be set to content. Details please see the following picture.
Suppose you already had a database file Sun.db added to the Assets folder, and now you can connect to it by the following code( use the SQLite.Net-PCL Nuget Package).
path = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, #"Assets\Sun.db");
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
{
}
But this folder is read only. Another location is Application data locations which can read/write. You can copy the database file from install directory to the application data directory for using. The following code example is for connecting a database file that in the local folder.
StorageFile file;
try
{
file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("Sun.db");
}
catch
{
StorageFile Importedfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Sun.db"));
file = await Importedfile.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder);
}
path = file.Path;
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
{
conn.CreateTable<User>();
}

Replace/overwrite local SQLite database file [WP8]

I' ve got a local pre-populated SQLite database file in my main project folder from which I get the needed data in my application. I establish a connection using:
SQLiteAsyncConnection conn = new SQLiteAsyncConnection("mydb.sqlite");
This file gets somehow updated (not locally & not by me or the user) so, after checking for new versions in my application, I have to overwrite my .sqlite file with the new one I download for that purpose.
(Also, there is a property called 'Build Action' which needs to be changed after adding a new db file.)
Is that possible? If so, have you got any suggestions?
edit:
I've managed to store the downloaded file in LocalStorage and then tried to copy and replace my database using this code:
StorageFile originalFile = await
ApplicationData.Current.LocalFolder.GetFileAsync("mydb.sqlite");
StorageFile downloadedFile = await ApplicationData.Current.LocalFolder.GetFileAsync("mydb2.sqlite");
await downloadedFile.CopyAndReplace(originalFile);
Now I'm getting this error:
'Windows.Storage.StorageFile' does not contain a definition for 'CopyAndReplace' and no extension method 'CopyAndReplace' accepting a first argument of type 'Windows.Storage.StorageFile' could be found
It's strange because it works fine in a new project I created. Could that be a conflict in the assembly references?

PhoneGap Local Storage WebSQL, IndexedDB, html5sql, lawnchair

I am trying to figure out the best way to store data in my cross platform app I am developing using PhoneGap.The API instructions suggest using WebSQL however this will no longer be supported, and IndexedDB is currently only for Windows / Blackberry.
There are many different questions on here many of the answers are really old and this, but I cannot seem to find the most popular js library for shipping an app with an existing database? (I.e. a good helper .js to simplify things).
I looked at HTML5SQL but the documentation is very sparse, lawnchair I am not sure about.
I don't know if this question will get flagged for being more preference bound, but I will give my 2 cents if it can help.
I have been able to ship an app with an existing database using SQLite with Phonegap/Cordova. Basically how I did it in Android was using the lite4cordova SQLite plugin:
https://github.com/lite4cordova/Cordova-SQLitePlugin
In the native code on load, I would check the default directory to see if a database with a specific name exists:
try {
File dbFile = getDatabasePath("data.db");
Log.v("info", "dbfiledir: " + dbFile.getAbsolutePath());
if (!dbFile.exists()) {
this.copy("data.db", dbFile.getAbsolutePath());
}
} catch (Exception e) {
e.printStackTrace();
}
And if it does not exist, copy the database file from the assets folder to the default database directory:
void copy(String file, String folder) throws IOException {
File CheckDirectory;
CheckDirectory = new File(folder);
String parentPath = CheckDirectory.getParent();
File filedir = new File(parentPath);
//File file2 = new File(file);
//Log.v("info", "filedir: " + file2.getAbsolutePath());
if (!filedir.exists()) {
if (!filedir.mkdirs()) {
return;
}
}
InputStream in = this.getApplicationContext().getAssets().open(file);
File newfile = new File(folder);
OutputStream out = new FileOutputStream(newfile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);
in.close();
out.close();
}
Now when the app loads, it will only copy if the database does not already exist. I can open the database using simply (Syntax may vary depending on plugin):
db = window.sqlitePlugin.openDatabase({
name : "data"
});
I will soon be performing the same logic on the iOS side, but I think it should be similarly straightforward.
That being said, there is a plugin listed on http://plugreg.com that provides a helper library for shipping prepopulated databases with WebSQL:
https://github.com/Smile-SA/cordova-plugin-websqldatabase-initializer
I wanted to use SQLite for my project so I opted for the copy on load method. Good luck!
Latest Update:
New Cross Platform Cordova WebSQL plugin by MS Open Tech
Microsoft Open Technologies is publishing the new open source WebSQL plugin for Apache Cordova and PhoneGap. This plugin allows developers to integrate a persistent SQL-based local storage solution in their Cordova apps using the exact same JavaScript code across Android, iOS, Windows Phone and Windows Store.

With MvvmCross what is the preferred way to copy a prefilled SQLite Database

I am modifying the N-10-KittensDb sample solution.
I See how to create a SQLite database, but I wish to use an existing database. I am guessing that I need to copy the database to the proper UI data folder. Maybe it is done within the Core project? And if so how is the correct path injected into the running Exec? As the Core can be used across many UI's? What method is called to see if the database exists or needs to be copied?
Sample from DataService:
public DataService(ISQLiteConnectionFactory factory)
{
const string file = "Cats.sldb";
var connection = factory.Create(file);
connection.CreateTable<Kitten>();
}
I believe the paths are different for Android vs Phone vs Touch vs Wpf?
Please direct me to a sample piece of code that uses the Cirrious.MvvmCross.Plugins.Sqlite for Phone or Wpf.
Thank you
Dan
Each platform by default creates a database in a folder location appropriate for the platform - e.g. Touch uses:
public ISQLiteConnection Create(string address)
{
var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
return new SQLiteConnection(Path.Combine(path, address));
}
from https://github.com/slodge/MvvmCross/blob/v3/Plugins/Cirrious/Sqlite/Cirrious.MvvmCross.Plugins.Sqlite.Touch/MvxTouchSQLiteConnectionFactory.cs#L18
To read/write files, MvvmCross does bundle a File plugin - this also operates by default in platform specific locations - but the two may not match perfectly - e.g. see:
protected override string FullPath(string path)
{
if (path.StartsWith(ResScheme))
return path.Substring(ResScheme.Length);
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), path);
}
from https://github.com/slodge/MvvmCross/blob/v3/Plugins/Cirrious/File/Cirrious.MvvmCross.Plugins.File.Touch/MvxTouchFileStore.cs#L22
Because of this mismatch, in order to share the same database-specific copy code across platforms you may find it easier to just inject your own platform specific copy on each platform - for more on injecting platform specific services, see http://slodge.blogspot.co.uk/2013/06/n31-injection-platform-specific.html

Resources