简体   繁体   中英

How save db file in document directory using Titanium

I want to save database file in Documents Directory instead of /Library/Private Documents folder using Titanium APIs.

var database = Titanium.Database.install('/db/Database.sqlite',DatabaseHandler.DB_NAME);

And it is getting saved into

iPhone Simulator/5.0/Applications/B750AD49-5879-4ABD-B903-C76A03833BE4/Library/Private Documents/Database.sql

whereas we are requiring it in

iPhone Simulator/5.0/Applications/B750AD49-5879-4ABD-B903-C76A03833BE4/Documents/dbDir/Database.sql

or

iPhone Simulator/5.0/Applications/B750AD49-5879-4ABD-B903-C76A03833BE4/Documents/Database.sql

The basic requirement is, we don't want the user's data lost on new app updates. On new app updates from App Store, only document directory data is saved, and rest all data is deleted.

And if I am manually moving db file with :

if(Ti.Platform.osname === 'iphone'){
  var dbFile = database.file;
  var filePath = String.format('%s/%s.sql',dbDir.resolve(),DatabaseHandler.DB_NAME);
  Ti.API.info(filePath);
  var result = dbFile.move(filePath);
  Ti.API.info(result);   
}

Then on running application, I am getting issue with message :

message = "invalid SQL statement. Error Domain=com.plausiblelabs.pldatabase Code=3 \\"An error occured parsing the provided SQL statement.\\" UserInfo=0x9c14ed0 {com.plausiblelabs.pldatabase.error.vendor.code=1, NSLocalizedDescription=An error occured parsing the provided SQL statement., com.plausiblelabs.pldatabase.error.query.string=SELECT * FROM UserProfile, com.plausiblelabs.pldatabase.error.vendor.string=no such table: UserProfile} in -[TiDatabaseProxy execute:] (TiDatabaseProxy.m:186)"

How are you opening the database?

If you are using Ti.Database.open('MyDatabase') since you moved it to another location, it is creating a blank database called 'MyDatabase' in the default location. The location your original install of the database occurred. From what I understand this is the default functionality of Ti.Database.open. If the database isn't there, then create it. Since this database is new and has no tables in it, it is going to say that the table UserProfile doesn't exist.

I don't know if there is a way for you to qualify the location of the database in the Ti.Database.open call. For example, I don't know if

Ti.Database.open('/iPhone Simulator/5.0/Applications/B750AD49-5879-4ABD-B903-C76A03833BE4/Documents/dbDir/MyDatabase')

will work or not. You need to tell the open call where the database is located, since you moved it from the location it expects to find it. Perhaps this isn't the case, but your example code doesn't show your usage of the database, rather it shows how you managed the moving of the file around.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM