Delete Database in Sqflite package in flutter - database

I am using the package 'sqflite' in flutter and I could not find a way to delete the entire database when the user in my app clicks on 'Delete Account'.
There are methods to delete data in a table but is there a way to delete the entire database using the package?

There is a delete database function in the DatabaseFactory that can delete a database based on path.
Future<void> deleteDatabase(String path) =>
databaseFactory.deleteDatabase(path);
See documentation for more information: https://pub.dev/documentation/sqflite_common/latest/sqlite_api/DatabaseFactory-class.html

Related

Missing Migration data in SQL when trying to add-migration

I have made use of a backup database and have added a new table using Code First. Now when I tried using add-migration, I got this error:
Unable to generate an explicit migration because the following
explicit migrations are pending
Suggesting that the Migrations in the project do not match which are in the DB, which when I looked, there are NO migrations in the __migration table any more ?
As these tables exist in the DB, how do I get all of these migrations inserted into my SQL DB ? Is there a way I can get them all added, so that the projects migrations and the SQL DB migrations will match, so that I can continue adding/editing tables ?
Thanks in advance for any help.
So I found something that did help! It unfortunately meant that I lost all of my migration files, but thats not the end of the world.
The below is reference from this site: Click here
Remove the _MigrationHistory table from the Database
Remove the individual migration files in your project's Migrations folder
Enable-Migrations in Package Manager Console
Add-migration Initial in PMC
Comment out the code inside of the Up method in the Initial Migration
Update-database in PMC (does nothing but creates Migration Entry)
Remove comments in the Initial method
I hope this helps someone else as well.

How to create database using Database Project in Visual Studio if doesn't exists?

I have a Database project for my personal project and I am trying to deploy my code to my DEV server. I frequently delete and re-create my DEV Server. Right now, DEV Server is newly created with SQL Server. Every time I want to deploy my code I have to manually create Database Project and then publish database project. I want to automate creation of Database with database project deployment.
Right now, I have a script that creates database, but I have to execute it manually. And this is working perfectly but I want to automate this step as well.
Is this even possible? If yes, then how? Please explain step by step. Also what will we mention for Initial Catalog in connection string?
Edit:
I tried to create Database by using
CREATE DATABASE LocalDbTest
in Pre-Deployment Script. But it didn't work. It is creating Database, but then tables are not getting created tables under it. Since I used master database as default database, it is creating table under master. It is not letting me select LocalDbTest database as default because it is not yet created, so I have to select Master as my default database. I tried to Change Database by:
USE LocalDbTest
GO
I used it just after creating Database but this didn't work because when generating script it is changing back to default database. This part is coming automatically when generating script.
USE [$(DatabaseName)];
GO
Also Visual Studio is not letting me add database name in front of table name like:
CREATE TABLE [LocalDbTest].[dbo].[TestTable]
I am getting error:
When you create an object of this type in a database project, the object's name must contain no more than two parts.
If you have a script ready for database creation, you can use the Pre-build event to call SQLCMD and run your script.
Edit:
If you have trouble pointing to a database that does not exist, you may have to manually edit the publish profile (ex. dev.publish.xml) and set the TargetDatabaseName element explicitly. You can also set CreateNewDatabase element to True if you want to be recreated every time it gets published.
Answer:
You can use a publish profile and hardcode the target database in it.

How Do I Initialize an Existing Microsoft SQL Server Database?

I have a database which has users assigned against it. I want to initialize the database (as though I'd just created it) rather than creating a new one, naming it appropriately, and re-adding all of the users.
Is it possible to completely reinitialize/reset a database?
Is it possible to do (1) whilst retaining the users and their permissions?
Thanks for looking :)
You can use generate script to create SQL file with Schema and user information.
Rename the existing db and run the script to create new one. Generate script provides lot of options to customize the script according to your needs
Right click on db -> Tasks -> Generate script -> Click next on the
wizard
Enable 'script logins' in Choose script options section of
the wizard
Select the users
Generate the script

How can I store required 'base' or 'initial' data for a database (in particular Symfony)?

I use the doctrine migrations bundle to track changes in my database structure. I would like to ensure that when I'm deploying / adding a new server for my application that:
(A) the database schema is up to date (doctrine:migrations:migrate)
(B) the database always contains a pre-defined set of data
For (B) a good example is roles. I want a certain set of roles to always be present. I realize it is possible with database migrations, but I don't like the idea of mixing schema changes with data changes. Also if I use MySql migrations I would have to create a equivalent Sqlite migration for my test database.
Another option I'm aware of is data fixtures. However from reading the documentation I get the feeling that fixtures are more for loading test data. Also if I changed a role name I don't know how that would be updated using fixtures (since they either delete all data in the database before loading or append to it). If I use append then unique keys would also be a problem.
I'm considering creating some sort of command that takes a set of configuration files and ensures that certain tables are always in a consistent state matching the config files - but if another option exists I'd like to use it of course.
What is the best way to handle loading and managing required data into a database?
If you're using Doctrine Migrations, you can generate initial migration with whole database schema, then you should generate migrations (doctrine:migrations:generate or doctrine:migrations:diff) for all changes that are made in database structure AND also add there queries that will migrate existing data.
Fixtures are designed to pre-populate data (with doctrine:fixtures:load) and, in my opinion, they should be kept up-to-date with latest database schema and executed after doctrine:migrations:migrate / doctrine:schema:create.
So finally:
Create base migration with initial database schema (instead of executing doctrine:schema:create just generate migration file and migrate it)
Create new migrations for each database schema change AND for migrating existing data (such as role name changing)
Keep fixtures up-to-date with latest schema (you can use --append option and only update fixtures instead of deleting all database data first)
Then, when deploying new instance you can run doctrine:schema:create, then doctrine:migrations:version --add --all --no-interaction (mark all migrations as migrated, because you have already created latest schema) and doctrine:fixtures:load which will populate data to the database (also latest version, so data migrations from Doctrine migrations files are not required).
Note: Existing instances should NOT use doctrine:schema:update, but only doctrine:migrations:migrate. In our app we even block usage of this command, in app/console:
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Helper\FormatterHelper;
// Deny using doctrine:schema:update command
if(in_array(trim($input->getFirstArgument()), ['doctrine:schema:update'])) {
$formatter = new FormatterHelper();
$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, true);
$formattedBlock = $formatter->formatBlock(['[[ WARNING! ]]', 'You should not use this command! Use doctrine:migrations:migrate instead!'], 'error', true);
$output->writeln($formattedBlock);
die();
}
This is what I figured out from my experience. Hope you will find it useful :-)

SQLite problem (doesn't update)

I have made an SQLite database file with some testdata in it. When I use it in my application it goes well.
Next I have inserted a new row with data into my table with an SQLite manager. Here's where the problem is. When i run my application now, the new data doesn't appear. It's still all the old data (without the newly inserted one).
Does anyone know why the SQLite file isn't updated (Or maybe it's updated but doesn't show the data)?
Update:
I use the SQLite manager in FireFox.
I've inserted it with that manager, and when i query the select * from table it shows the new data.
I use the following command to open my database
var db = Titanium.Database.install('../test2.sqlite', 'test');
What is the current path when you open the database? ../ refers to the parent folder; are you absolutely certain that is the same folder where you’ve been creating a database using SQLite Manager? SQLite doesn’t error out if you open a non-existing database; it’ll just create a new, empty, one.
Use a full, absolute path, and try again.
this is typically i think. your database seems to be cached. you should change the name
var db = Titanium.Database.install('../test2.sqlite', 'test_v2');
or delete the cached database.
You don't need to rename your database.
You can try to delete the app in your simulator or emulator and install the app again. It worked for me. Since the app still has the old database. Re-installing the app with the new data (i.e. columns, rows). I use SQLite Manager plugin in Firefox as well.

Resources