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

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.

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.

How to make sure SQLite database was not modified?

I am looking for a way to tell if a database file was modified or not.
The amount of data stored is not large, however updates are often and running select statements after any update to create a new checksum of all data would be too much.
Previously most of our data was stored as entries with JSON, so it was much easier to get few rows and create a checksum of it. Now however, we need to use the database properly, so data will be normalized across few tables and multiple rows.
I need this to be handled by the database, so I don't want to create an md5 of the database file and check that.
Is there any way I could achieve that?
Whenever a database is modified, the file change counter in the database header is incremented.

Best way to get rows changed in an sqlite db

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.

How to reset your database in visual studio 2010

I want to wipe out all the data in the rows in the tables that I have, how do i do it?.. I want to completely delete them. I have 4 tables, and i prefer to delete/ reset them altogether..
``
This article may help you. It uses the built in sp_MSForEachTable to check/remove constraints and then truncate the data
SQL Table?
try truncate table [tablename] - this should delete and reset ids
Tim's solution is good, but just make sure that you are aware of any dependencies in your table.
start by deleting the farthest child table in the relation, and go up one level till you reach to a table with only foreign keys to other tables.
and it's always better to keep it as a SQL script that you may run whenever you need to do the reset.

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