Best way to get rows changed in an sqlite db - sqlite

I'm watching an sqlite db which an app uses.
I want to know what changes have been made since
I last checked.
I can dump to sql and diff against the last dump,
but it seems there should be a better way.
Is there?
Thanks,
Kent
PS Not to be coy, specifics: I'm managing photos with Shotwell, which has a great GUI.
I'm mirroring Shotwell's db in Postgresql, where I've restructured and augmented to my liking. After a Shotwell session, which involves adding, tagging, adjusting ... I want
to apply those changes to Postgres.

Add a field named _changed to your table(s). On every manipulation (update, insert into...) of a row set the field to the current timestamp. Now you can check which rows have been updated since.

Related

Is it possible to change a column without run raw sql in DbVisualizer + SQLite

Working on a SQLite database, seems DbVisualizer Pro does a lot of work very well, except one,
Changing table schema.
I often need to change column name, data type, etc, but don't want to do it through raw SQL statement. My workaround is opening Firefox's SQLite Manager to just change the schema.
Is it possible to use DbVisualizer to change the schema? many thanks!
Edit:
Alter table action mentioned below by roger, seems to be the right way to go. But somehow I can only add column, the existing column appears to be read only.
Mine is DBVisualizer Pro Evaluation. Is non-Evaluation different?
Edit2:
Using SQLite Manager is sometimes dangerous, as warned below. just learned, renaming a column may cause the foreign key loss. but workaround is here
In DbVisualizer Pro there is the Alter Table action (and Create Table for creating new tables). Select the actual table you want to change in the Databases tab, right-click and chose Alter Table. In order for this to work you need DbVisualizer Pro and the Database Type for your connection must be set to either Auto Detect (recommended) or SQLite.

Rename a table?

I have a DynamoDB table called Transactions in which I am recording an audit trail of activity in my app. After midnight I would like to rename the table to something like Transactions.2015-10-01 and create a new table called Transactions, to which I would record the next 24 hours of activity in my app. At the end of that period I would rename that to Transactions.2015-10-02 etc.
In other words, I'm trying to implement a rotating log of activity (though linear rather than circular).
I'm not sure there's a way to rename a table in DynamoDB. Is there a way? If not, is there another approach? For example, if there was a table pointer that was called Transactions that would point to Transactions.2015-10-01 and then at midnight that pointer would switch to to Transactions.2015-10-02. My app audit logic would simply perform insertions into Transactions which delegates to the right table.
Amazon DynamoDB does not have a command to rename tables. The only table-related commands are CreateTable, DeleteTable, DescribeTable, ListTables and UpdateTable (which does not permit a rename).
Nor is there the ability to create aliases to tables.
The best approach would be for your application to create the desired tables and then direct transactions to the appropriate table.
Not exactly a work around, but you can create a backup, restore the backup to a new table with a new name, then optionally remove the original table.
This is not a "rename", and it could break a lot of things. Just a hint. Don't go ahead and do it on your production server!

Meta-data from SQLite

Is there any way to query a SQLite database for basic meta data such as:
Last date/time updated
Hash of database to indicate "state"
I am just looking for a simple, infrastructural way to have a script evaluate different databases and take a reasonable point of view on whether they are the same "state" as other databases in a different environment (PROD and DEV for instance).
In my experience, if no update, new record, or any change is made to the SQLite database file, the last modified time of the file doesn't change. So the last modified time should suffice for the time of any change made to database.
If 2 database files with same state are only accessed for reading, their modified times are always the same.
Similarly you get the file sizes for comparison.
You can use the whole file to calculate hash. If you consider same data in the database as the same "state" regardless of any difference in the past, then maybe you want hash of the all records in database, which is probably not simple.

how to you alter a sqlite db with coolstorage on wp7?

I'm writing an app for wp7 using coolstorage and sqlite as the database.
The reason I chose sqlite was because it appears to have a good ability to do updates to the schema.
However, in my testing I can run an 'alter table...' command but if I add a column and then try to read that column I get an error that the column doesn't exist.
But if I close the app and run it again, the column is there.
I thought about trying to close my connection, but I don't know how to do that using coolstorage. Is there a way? Would that work?
I'm new to both Coolstorage and SQLite so any help is appreciated.
How can I alter a sqlite schema without closing and re-oppening the app?
[Update: 15 Dec 2011]
We avoided this problem by making sure we ran our schema updates before querying the affected tables, which is probably the right thing to do anyway.
If anyone comes up with a way to do the schema change after running a query against a table, I would still be interested in knowing.
Sqlite has the ability to add columns to the end of existing tables in-place, however any queries which have already been prepared will not show the changes. In my experience, if you prepare the query again, it works. Alternatively, you could close and re-open the database. Note that other schema changes are not directly supported, and will require you to do the following:
Rename the original table
Create a new table with the desired changes.
Copy the data from the old table to the new one, as applicable.
Delete the original table.

Quickest way to delete all content in a database and rebuild from scratch?

I am designing a standard ASP.Net site with a SQL database. I have a database schema and During the tests I am changing data types amongst other tasks and the data contained inside really is not that important.
I keep getting errors as the old data does not match the new rules. This is not important and I am happy to clear everything but currently, I have to export/publish the database to a .sql file then import it from scratch - which is time consuming.
Is there a quick button / feature that I have missed that allows you to reset autonumbers / IDs to 1 and delete all content, or just speed up what I currently do?
There are a few options you could take, the "fastest" really depends on your database.
To firstly answer your questions on seeding, etc - TRUNCATE TABLE will delete all information in a table (very fast, as it is not logged) and will reset your identity column.
eg:
TRUNCATE TABLE dbo.table
http://msdn.microsoft.com/en-us/library/aa260621(SQL.80).aspx
The significant restriction here is that you cannot use it on a table that is referenced by another table. In this case you can use a standard delete and then use DBCC CHECKIDENT
eg:
DELETE FROM dbo.table
GO
DBCC CHECKIDENT(dbo.table, reseed, 0)
http://msdn.microsoft.com/en-us/library/ms176057.aspx
Remember with delete to make sure you delete information in the correct order (i.e. taking into account foreign keys).
Another approach I often use is simply writing a complete tear-down / rebuild script when I want to reset the database. The basic premise is to tear down, or drop all database objects at the beginning of the script and then recreate them. This is not necessarily a solution for all scenarios, but for basic tasks works well for me. To avoid errors I would usually add my drop statements in IF statements, eg:
IF EXISTS
(
SELECT *
FROM information_schema.tables
WHERE table_name = 'table' AND table_schema = 'dbo'
)
BEGIN
DROP TABLE dbo.table
END
Why don't you write some T-SQL code to delete (or truncate, even quicker) all your tables? Be careful to take into consideration your integrity rules while clearing the tables: allways clean the tables containing the foreign key before cleaning the one containing the primary key.
If you just need to clear out data then just write a script to truncate all the data in each table. The truncate command also resets any IDENTITY fields as well.
TRUNCATE TABLE myTable
For each table you have. Then just run that script each time.
Here'a a quick way to delete all of the data in a table:
TRUNCATE TABLE YourTableName
You could write a script that would truncate all of your tables.
The alternative is to just DROP the table and re-create it.
If you really want to drop all data, then you could detach the database and create a brand new one; it's a bit extreme, but possibly faster than dropping everything first.
As others have suggested I find it preferable to maintain a script that builds the database from scratch and can tear down the database prior to rebuilding it. Develop this script just as you'd develop the rest of the application. I find it easier to understand the database through a script than by building it through a GUI, especially where there are complex relationships, triggers and so on.
It's also useful if you have other developers, and perhaps quicker and less prone to errors than copying your working database and handing it to another developer.
On release you can freeze that script and then create delta scripts for the next release which has just the changes from the initial schema to the new. This could also tear down the new objects created in the delta before recreating them so it can be easily re-run without having to wipe the entire database.
if you use Visual Studio 2010 then
open the App_Data folder of the solution and double click on the MDF File.
right click on your table , in the menu select "Show Table Data".
select all rows and delete all them.

Resources