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.
Related
A relative sqlalchemy path to a sqlite database can be written as:
sqlite:///folder/db_file.db
And an absolute one as:
sqlite:////home/user/folder/db_file.db
Is it possible to write a path relative to home? Like this:
sqlite:///~/folder/db_file.db
Or even better, can the path contain environment variables?
sqlite:////${MY_FOLDER}/db_file.db
This is the context of an alembic.ini file. So if the previous objectives are not possible directly, may I be able to cheat using variable substitution?
[alembic]
script_location = db_versions
sqlalchemy.url = sqlite:///%(MY_FOLDER)s.db
...
I have gone around this issue by modifying the values in the config object just after env.py imports it:
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# import my custom configuration
from my_app import MY_DB_URI
# overwrite the desired value
config.set_main_option("sqlalchemy.url", MY_DB_URI)
Now config.get_main_option("sqlalchemy.url") returns the MY_DB_URI you wanted.
As others have pointed out, one key is 3 slashes for relative, 4 for absolute.
But it took for me than just that...
Had trouble with just a string, I had to do this:
db_dir = "../../database/db.sqlite"
print(f'os.path.abspath(db_dir): {str(os.path.abspath(db_dir))}')
SQLALCHEMY_DATABASE_URI = "sqlite:///" + os.path.abspath(db_dir) # works
# SQLALCHEMY_DATABASE_URI = "sqlite:///" + db_dir # fails
From the alembic documentation (emphasis mine):
sqlalchemy.url - A URL to connect to the database via SQLAlchemy. This configuration value is only used if the env.py file calls upon them; in the “generic” template, the call to config.get_main_option("sqlalchemy.url") in the run_migrations_offline() function and the call to engine_from_config(prefix="sqlalchemy.") in the run_migrations_online() function are where this key is referenced. If the SQLAlchemy URL should come from some other source, such as from environment variables or a global registry, or if the migration environment makes use of multiple database URLs, the developer is encouraged to alter the env.py file to use whatever methods are appropriate in order to acquire the database URL or URLs.
So for this case, the sqlalchemy url format can be circunvented and generated by python itself.
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 ...
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.
There are two problems with SAP HANA custom dictonaries.
Updating and recompiling the dictionary has no effect on the full-text-index table (even by dropping and generating the full-text-index again)
using custom dictionaries & configuration may lead to an empty fulltext-index-table
For the 1. Problem
deleting the configuration file and replace it with a new file (same content but different file name) then activating all changes (activates the deletion of the old config and adds the new config) seems to be a work-around.
Note: this means you also have to change the configuration name in the SQL command.
For the 2. Problem
Check this trace file:
/usr/sap/HDB/HDB00/hanadb/trace/preprocessor_alert_hanadb.trc
This error message:
File read Error '/usr/sap/HDB/SYS/global/hdb/custom/config/lexicon//EXTRACTION_CORE_MOD2', error='Storage object does not exist: $STORAGEOBJECT$'
occurs if the configuration file EXTRACTION_CORE_MOD2 is not properly activated in the repository under sap.hana.ta.config. So double check the repository if the configuration file exists in the specified path.
For the first problem, I have the same scenario in which I need to make some changes in the custom dictionary and activated it. It did not affect my index table unit I run the following statement:
ALTER INDEX MYINDEX REBUILD;
I have checked it and the changes affect the index table by this statement. So you do not have to remove your index or save the changes of your custom dictionary in a file with new name.
I developed a application on my local system (Windows 7 & wamp)its working fine so I decided to deploy it to my shared hosting account
I create a blank database on server and then imported my local database file
but if try to login to my system I am getting table not found error although table are present in database
I am getting error like below
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'ezwebsto_ezwoms.WomsLog' doesn't exist") in
"AdminBundle:Default:home.html.twig
I suspect that the issue is because case sensitive table name, so renamed table name but still I am facing issue
can anyone guide me what is proper way to move local db of symfony application to web to p
MySQL on Windows by default is NOT case sensitive. On Unix based system it is.
The best practice is to turn on case sensitive in MySQL settings, and in future moving database to server will be simple.
You have to set up variable
lower_case_table_names = 0
in your my.ini file. On Windows, default value is 1.
But now, you have to map table name inside each entity, like wrote #prashant. Please compare table names with mapping names, and rename tables if necessary.
After change, your entity should look like:
/**
* Content
*
* #ORM\Table(name="content")
* #ORM\Entity()
*/
class Content
{
//some usefull code here
}
Just try to define the tablename in your entity class, with the Table annotation:
#ORM\Table(name="WomsLog")
As well make sure your table is correct in DB , W and L must be in upper case.