In Xamarin IOS solution, the standard code for sqlite database path is:
var sqliteFilename = "MyDatabase.db3";
string libraryPath = Path.Combine (documentsPath, "..", "Library"); // Library folder instead
var path = Path.Combine (libraryPath, sqliteFilename);
The above code is fine if the database is created from scratch.
but my question is how should I include the existing sqlite database into the solution, so it can be copied over to "Library" folder?
Include your database as a file in your solution, marking it as "Build Action -> Content". In your code, you can access it relative to the root of your project (ie, "/mydb.db3") - then read it and copy it to your Library or wherever you want to place it.
Keep in mind that files bundled into your application are READ-ONLY. You will have to copy the db to a writable path before you can write to it.
Related
I started doing development with google scripts few days ago and recently joined stackoverflow. I have a problem with addToFolder() function. I have the following piece of code that copies my new spreadsheet into a folder (test/sheets) in my Google Drive:
var ss = SpreadsheetApp.create("test");
var ssID = ss.getId();
DocsList.getFileById(ssID).addToFolder(DocsList.getFolder("test/sheets"));
My problem is that now I have 2 versions of the same file (one in the root of my Google Drive folder and the other in test/sheets folder), whenever I try to delete either of the copies, the other copy is deleted as well. Is there a way to delete the old file and keep the new one OR is there a way to create the file in the desired folder in first place?
EDIT :
thanks for you quick response. I played with this for couple of hours but still have problem copying the file to the destination folder. The problem is that even when I use makeCopy Method of the file, still addToFolder is the only option to mention the folder. Again this ends up having the tagged filename in the destination folder.
I had the same problem with the copy method.
Here is my new Code:
var SetLocationFile = "icompare/sheets/stocks"
var FolderID = DocsList.getFolder(SetLocationFile);
var FileID = DocsList.getFileById(ssID);
FileID.makeCopy("test3").addToFolder(FolderID);
Folders in Google Docs\Google Drive are actually tags. When you "add" a file to the folder "test/sheets", you do not make a copy of your file, you just attach the tag "test/sheets" to it. Now the same file is shown both in the "test/sheets" folder (i.e. in the list of all files with the tag "test/sheets") and in the root. If you wish to make a copy of the file, you should use the copy method. (Please let me know if I just misunderstand your question.)
I realize this is an old questions but you can simply use .removeFromFolder(DocsList.getRootFolder()); to remove the file from the root folder.
I would also like to know the answer to this question.. seems rather "weird" that the API does not even provide a way to create spreadsheets and place them in a certain map? And no, I do not want a Copy of the file, I want the file to be in a specific map and in no other map...
I have 'inherited' a test harness application which uses Enterprise Library for its SQL data access. In the app.config file (enterpriselibrary.configurationSettings), it references a "configurationSection" with a path to "dataConfig.config", which is encrypted. I would like to change the database connection properties, but EntLibConfig.exe will not open the dataConfig.config or app.config (I have the FileKeyAlgorithmPairStorageProviderData file).
The test harness application runs, so its configured ok.
I can, in code, using (Microsoft.Practices.EnterpriseLibrary.Data.Configuration.ConfigurationManager.GetConfiguration("dataConfiguration")) read the data configuration, and can navigate all the instances and connection strings (security isn't an issue for this test harness). I can dump everything I need to a hand-crafted XML file (using GetType().AssemblyQualifiedName to get the full name for the classes which read the config file) and then change the app.config to read my new, unencrypted, xml dataConfig file.
All is fine, I can now change my database config settings.
However... given that ConfigurationManager.GetConfiguration("dataConfiguration") returns a fully populated instance of a DatabaseSettings object, is there not a method I can call which will write the XML file (dataConfig.config) for me ?
I appreciate that this is probably a really big hammer way to edit the data configuration, but after half a day of trying, I fell back on the old coding maxim... if you can't find the tool to do what you want, write your own !
Thanks
Well... turns out that its not that hard.
I added a new "configurationSection" to my app.config (dataConfiguration2), with encrypt set to false, with a path pointing to an new empty text file (dataConfiguration.config2). I then copied my encrypted dataConfiguration details using the following code:
using Entlib = Microsoft.Practices.EnterpriseLibrary.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
:
DatabaseSettings settings = (DatabaseSettings)Entlib.ConfigurationManager.GetConfiguration("dataConfiguration");
Entlib.ConfigurationManager.WriteConfiguration("dataConfiguration2", settings);
...and it filled the empty file with the (unencrypted) configuration details.
I created a project without checking "use CoreData". The Project name is "glossary"
Now I changed my mind and so for I added an data model usinng Add->New File->Resource->Data Model->gave it the name Glossary->didn`t select any class->Finish.
The next step was to design my Data Model.
Then I added the needed Code to my AppDelegate.
For all Steps i was following this Tutorial:
https://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html%23//apple_ref/doc/uid/TP40008305-CH1-SW1
My problem now is located in this line:
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"glossary" withExtension:#"momd"]
modelURL is always nill
Creating an extra NSBundle Ref and calling mainBundle shows me that this is working, however i don`t know if it is really the right path.
Doing the same Steps on an project with selected "use CoreData" while creating works great.
Has anybody an idea?
It has to do with model versioning. You want to add a versioned model (item in Xcode project tree will have .xcdatamodeld extension) and then your code will run smoothly.
In Xcode 4 when you add a new Core Data file it is versioned by default. Even if you added Core Data support after project creation.
If you don't plan to have model versions, just use .mom in your code.
I'm not 100 % sure, but URLForResource does work for files only and momd is directory.
URLForResource documentation: The method first looks for a matching resource file in the nonlocalized resource directory of the specified bundle. (In Mac OS X, this directory is typically called Resources but in iOS, it is the main bundle directory.) If a matching resource file is not found, it then looks in the top level of any available language-specific “.lproj” directories. (The search order for the language-specific directories corresponds to the user’s preferences.) It does not recurse through other subdirectories at any of these locations. For more details see Bundles and Localization.
How do i get the tmp directory on the iPhone or iPad?
if i use NSTemporaryDirectory() to create a file it does not save to the simulator temp directory.
As drawnonward said correctly in a comment
"NSTemporaryDirectory() will do the right thing on a device."
and thats true...
EDIT: Apparently there may be a problem with this approach on later Os's (>6.1.3), maybe try the NSCachesDirectory instead of the tmp directory.
As an alternative, from the documentation on NSFileManager URLForDirectory:inDomain:appropriateForURL:create:error:
You can also use this method to create a new temporary directory for storing things like autosave files; to do so, specify NSItemReplacementDirectory for the directory parameter, NSUserDomainMask for the domain parameter, and a valid parent directory for the url parameter. After locating (or creating) the desired directory, this method returns the URL for that directory. If more than one appropriate directory exists in the specified domain, this method returns only the first one it finds."
A simple question for everyone I think. I just begin to work with database for the Iphone.
And so I create my database "external.sql" and I import it into Xcode.
I create a simple Navigation Base application and I try to display all from module_category but its not working, told me that the table does not exist.
So I try to display in the console all tables inside my database, but it return me nothing
I have this:
2009-11-30 18:52:00.845 CategoryList[18458:20b] Display all tables in the database : ()
2009-11-30 18:52:00.859 CategoryList[18458:20b] *** Terminating app due to uncaught exception, reason: 'failed to execute statement: 'SELECT * FROM module_category' with message: no such table: module_category'
Is there something special to do when importing a database into the project? Did I have to create an Object for all my tables before being able to display them?
Thank you,
P.S: I have possibility to send the test project if you don't understand.
I'm assuming here that you're using sqlite3. If so, it looks like the database file is not present on the iPhone at the path you're using to open it.
Check that the database file is being copied to the bundle by looking inside the .app bundle. To put the file in the bundle, add it to the Copy Bundle Resources build phase in Xcode.
Check that the database file is not corrupt. Use sqlite3 [path to the file inside the bundle] to test your queries from the command line.
Check that you are using the correct path to open the database. Put a breakpoint on the line where you call sqlite3_open and check that the file really is at that path.