I'm experimenting a weird issue detecting if a file exists or not.
I'm working with SQLite, so I have a DB file in my App, which is Member of my Project in Target Membership, it's only the DB structure.
In my AppDelegate I'm checking if the file exists:
let fm = NSFileManager.defaultManager()
var docDir = ""
docDir = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let dbName:String = String.fromCString("db22.sqlite3")!
let path = docDir.stringByAppendingPathComponent(dbName)
In that point the file is created (not the idea), when I print the path, the file exist with size 0.
println("Database path: \(path)")
-rw-r--r-- 1 LLL staff 0 Jan 24 22:49 /Users/LLL/Library/Developer/CoreSimulator/Devices/F18F613D-B032-4B0A-87FC-3E484FC0C024/data/Containers/Data/Application/74FE784B-F131-4C60-9846-21D115E08594/Documents/db22.sqlite3
Just before run the app:
$ find /Users/LLL/Library/Developer/ -name db22.sqlite3
Nothing there.
So my next if in order to check if the file exists or not, is always returning TRUE:
if !(fm.fileExistsAtPath(path)) {
...
...
fm.copyItemAtPath(from, toPath: path, error: &error)
...
...
I tried with Reset Content and Settings in the Simulator, and also closing the project and deleting the all the files and directories in Library/Developer//Xcode/DerivedData/
But the results are the same (Product -> Clean also in XCode).
I was using this file before, working just fine, copying it to my DocumentDirectory in order to modify my DB, I deleted because I changed something in the tables, and suddenly was not working anymore.
If I tried changing the name dbName variable to something that I've never used before, then the file is not created, and the fileExistsAtPath is returning FALSE, as it should be doing also with the previous one.
I also deleted the db22.sqlite3 from my XCode Project, I cleaned and Cleared all the settings, deleting all the files in DerivedData...but the same result.
Any ideas?
Thanks in advance guys!
Related
I've used:-
$db = new \IP2Location\Database('./database/IP2LOCATION-LITE-DB3.IPV6.BIN', \IP2Location\Database::FILE_IO);
it seems to work OK.
IP2 docs. says "Class Database, Expects 2 input parameters"
Full path of IP2Location BIN data file. -- OK
File Open Mode. -- Why does it also need "\IP2Location\Database::" ?
Tried not using "\IP2Location\Database::FILE_IO" just using "FILE_IO".
this is my first question. if i make a mistake, sorry from the beginning. i am newbie at firebase and react native. i searched and i couldent find my answers anywhere.
i made a to do list. i searched about structure of database and understood that the structure must be flatten and i made a structure like that:
data structure screenshot
data structure:
database {
folders: {
folder_name: folder1,
messages: {
message1: true, (uid of this message)
message2: true (uid of this message)
}
}
messages: {
message1 {
message_content: this is a to do item1,
time: 1212221321,
folder: folder1 (uid of this folder)
}
}
message2 {
message_content: this is a to do item2,
time: 1212221321,
folder: folder1 (uid of this folder)
}
message3 {
message_content: this is a to do item3,
time: 1212221321,
folder: folder2 (this message is in another folder)
}
......
}
}
as you see, when i add a message in a folder, the folder contains message uid numbers in it. and each message contains an object property as folder uid (which folder this message in).
questions:
1: is this structure type right way? or should starters use another structure?
2: well, i m planning to turn the message id to false in folders (klasorler) when user delete a message. but i dont know what should i do when user delete a folder.(in this case, deleting folder 1..if folder one i must delete only message1 and message2) should i get all of message id's in folder and start a loop and delete messages one by one? (i mean loop in react native side)... or is there any shorter and smarter way in firebase with firebase methods ?
i hope i explained myself well :)
This question already has answers here:
Delphi Get file location
(2 answers)
Closed 6 years ago.
I am using SQLite, rather than MySQL, because it is simpler and requires no extra running software, just my executable.
For simplicity's sake I would prefer to have the database file in the same directory as my executable, and to be able to copy or move that folder elsewhere and still have the program work. Meaning that it can find the SQLite database file, which was copied along with it, in its new location.
However, it looks like I have to specify an absolute path to the database file.
Is there any way to change this at run time?
For those who don't quite follow that, I will try to explain:
let us say that my app's .exe is in C:\A\A.exe
so, I want to put the database file in the same directory. Lets call it C:\A\A.db
Delphi seems to require an absolute path. I.e, C:A\A.db and not .\A.db
I want to be able to copy both the .exe and its database to C:\B and still have the .exe be able to find its database file, which is now in C:\B\A.db
How can I do that programmatically, at run-time?
Answer
The path to the database is stored in the database connection component, so something like myConnection.parameters.database := 'C:\my_database.db;
Just compute the SQlite3 database file name on the fly, using:
myDbFileName := ExtractFilePath(paramstr(0)) + 'mysqlite3file.db3';
This will point e.g. to c:\my\app\folder\mysqlite3file.db3 file, when you run c:\my\app\folder\myprogam.exe.
First you want to copy the db to the EXE folder :
if FileExists(FDConnection1.Params.Database) then
CopyFile(Pchar(FDConnection1.Params.Database),Pchar('EXE Folder\A.db'),True);
then you can change the connection to the new db (copy) :
if FileExists('EXE Folder\A.db') then
begin
FDConnection1.Params.Database := 'EXE Folder\A.db';
FDConnection1.Connected := True;
end;
You always have to specify absolute path to the database file. Alternative would be preforming a full search for the database file which is stupid.
Here is a little piece of code I use to create backup of the database so you get the idea:
procedure TForm1.dxBarButton1Click(Sender: TObject);
var fileSource, fileDest,dirName: string;
begin
dirname := ExtractFilePath(Application.ExeName)+'\backup database' ;
if not directoryexists(dirName) then
CreateDir(dirName);
if MessageDlg('Create database copy ?'+ #13#10 +'Attention,new copy will overwrite existing one',
mtConfirmation, [mbYes, mbNo], 0, mbYes) = mrNo then
exit
else
try
dataModule2.ACRDatabase1.Close;
fileSource := ExtractFilePath(Application.ExeName)+'sgit.adb';
fileDest := ExtractFilePath(Application.ExeName)+'\backup database\sgit.adb';
If CopyFile(PChar(fileSource), PChar(fileDest), False) then begin
dataModule2.ACRDatabase1.Open;
dataModule2.FIRME.Open;
ShowMessage('Copy was made !');
end else
RaiseLastOSError;
except on E: Exception do
showMessage(Format('Error during copying : %s',[E.Message]));
end;
end;
So in your case you can write to an *.ini file where the database was copied to and then load the information during startup of your application. So basically you must always supply the database path (fileSource). How you do it is up to you. Just an idea ...
When I instantiate a Directory in Dart, and that file exists, how can I check whether the Directory is a real folder, or just a symlink?
The way you can recognize a symlink is if the path differs from the full path. Directory doesn't have fullPath() or fullPathSync(), but File.fullPathSync() works on directories. So you can do this:
bool maybeIsSymlink(String path) {
var fullPath = new File(path).fullPathSync();
return path != fullPath;
}
However this only works correctly when path is absolute, and none of its ancestors are symlinks. To work around that you can get the full path of the directory's parent, append the directory name and compare that:
bool isSymlink(String pathString) {
var path = new Path(path);
var parentPath = path.directoryPath;
var fullParentPath = new File.fromPath(parentPath).fullPathSync();
var expectedPath = new Path(fullParentPath).append(path.filename).toString();
var fullPath = new File.fromPath(path).fullPathSync();
return fullPath != expectedPath;
}
Note that I have not tested this, but I've dealt with symlinks a lot in Dart, and this should work. If pathString ends in '/' you'll have to remove it. I usually end up getting paths from a directory listing, so I track the expected path as I recurse down the directory structure.
You can see a special listDirectory() implementation that detects symlinks and sends Symlink instances to the stream along with Files and Directorys in a branch in buildtool: https://github.com/dart-lang/buildtool/blob/m4/lib/src/util/io.dart
In bleeding edge, there now is a static FileSystemEntity.isLinkSync(path) methods that will tell you if something is a symlink; also when it is a broken symlink.
http://api.dartlang.org/docs/bleeding_edge/dart_io/FileSystemEntity.html
For operations on links we are adding a Link class. The code is out for review now:
https://codereview.chromium.org/12691002
If you want to check from the command line to verify what is happening,
ls -al DIRNAME and check for 'l' in the permissions section, and examine what it's pointing "to" on the right side of the output.
see also man 1 stat
If you're wanting to check that from within Dart itself, I don't know how.
FileSystemEntity.typeSync(path)
return an FileSystemEntityType with one of the values
FileSystemEntityType.DIRECTORY
FileSystemEntityType.FILE
FileSystemEntityType.LINK
FileSystemEntityType.NOT_FOUND
I have started playing around with Berkeley DB. This one is really interesting, but I am facing one problem.
While creating the Environment we create it like this -
Environment env = new Environment(new File("./bdb"), envConfig);
It initially threw an exception saying "bdb" location was not found. I created the location and it all worked.
My question is how would I set up berkeley DB to create this directory for me if it does not exist. I actually checked at the config method SetAllowCreate(boolean flag) .. but its functionality is different.
Any help would be appreciated.
Thanks.
You can do -
File file = new File("file path goes here");
// Either the file exists or mkdirs is successful
if (file.exists() || file.mkdirs()) {
Environment env = new Environment(file, envConfig);
}
I actually ended up doing something like this (not sure whether it is right solution but it works)
boolean x = new file("./bdb.data").mkdir();
Environment env = new Environment(new File("./bdb"), envConfig);