Sunday, August 10, 2008

sqlite database encryption

copied from http://sqlite.phxsoftware.com/forums/t/130.aspx

Encrypt sqlite by using System.Data.SQLite.dll is pretty simple.

To encrypt an existing unencrypted database, or to change the password of an encrypted database, open the database and then use the ChangePassword() function of SQLiteConnection:


// Opens an unencrypted database
SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db3");
cnn.Open();
// Encrypts the database. The connection remains valid and usable afterwards.
cnn.ChangePassword("mypassword");


To decrypt an existing encrypted database call ChangePassword() with a NULL or "" password:


// Opens an encrypted database
SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db3;Password=mypassword");
cnn.Open();
// Removes the encryption on an encrypted database.
cnn.ChangePassword(null);

To open an existing encrypted database, or to create a new encrypted database, specify a password in the ConnectionString as shown in the previous example, or call the SetPassword() function before opening a new SQLiteConnection. Passwords specified in the ConnectionString must be cleartext, but passwords supplied in the SetPassword() function may be binary byte arrays.


// Opens an encrypted database by calling SetPassword()
SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db3");
cnn.SetPassword(new byte[] { 0xFF, 0xEE, 0xDD, 0x10, 0x20, 0x30 });
cnn.Open();
// The connection is now usable

By default, the ATTACH keyword will use the same encryption key as the main database when attaching another database file to an existing connection. To change this behavior, you use the KEY modifier as follows:
If you are attaching an encrypted database using a cleartext password:


// Attach to a database using a different key than the main database
SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db3");
cnn.Open();
cmd = new SQLiteCommand("ATTACH DATABASE 'c:\\pwd.db3' AS [Protected] KEY 'mypassword'", cnn);
cmd.ExecuteNonQuery();

To attach an encrypted database using a binary password:


// Attach to a database encrypted with a binary key
SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db3");
cnn.Open();
cmd = new SQLiteCommand("ATTACH DATABASE 'c:\\pwd.db3' AS [Protected] KEY X'FFEEDD102030'", cnn);
cmd.ExecuteNonQuery();

Monday, July 28, 2008

NDbUnit Framework

NDbUnit test is a good UnitTest framework if you are familiar with C#. But this project seems not active since 2006.
Home Page: http://qualitylabs.org/projects/ndbunit/

Monday, July 21, 2008

SQL 2005 Restore database error

当我用SQL Server 2005 restore一个数据库时候, 总是报下面的错. 我试着用sa登录,试着用Windows Authentication方式登录都不行.
System.Data.SqlClient.SqlError: The operating system returned the error '5(Access is denied.)' while attempting 'RestoreContainer::ValidateTargetForCreation' on 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DWH.mdf'. (Microsoft.SqlServer.Express.Smo)

google之后, 发现原因是SQL Server服务的账号必须有权限读写mdf文件. 而SQL Server 2005服务可以用指定的windows账号来启动, 仅仅内建的启动账号就有local system,local service和network service三种, 还可以用其他账号来启动.
出现restore错误时候, SQL Server2005是由network service账号来启动的, 该账号没有权限读写mdf文件. 所以需要切换启动账号, 可以用SQL Server configuration manager这个工具.

The service account for sqlexpress does not have access/permission. Use Configuration Mgr to change the startup account to LocalSystem (if your database and backup file are on the local machine) or a domain user account that has permission to disk resources (locally and remotely).

Wednesday, June 4, 2008

Exploring Table and Index Partitioning in SQL Server 2005

参考 http://www.sqljunkies.com/article/f4920050-6c63-4109-93ff-c2b7eb0a5835.scuk

Indexed Views in SQL Server 2000

参考 http://www.sqlteam.com/article/indexed-views-in-sql-server-2000

When and where do we use hash index in sql server

参考http://www.fotia.co.uk/fotia/Blog/2006/01/hash-indexes.html
当我设计一个Product表的时候, 对于ProductName这个字段的数据类型, 我一直拿捏不定, 最后为了不至于ProductName被截断, 我选用了NVarchar(4000), 我没有选择Text(NText)类型, 因为在SQL Server 2005中,它们被标记为Deprecated.
幸运的是,我选取了NVarchar(4000), 在实际情况下, 有个名称也确实够长, 足足1000个byte.
同时我又需要在ProductName上创建Index, 对于length>=900byte的字段, 将SQL Server拒绝为你创建普通的index.
怎么办呢? 我们可以为ProductName这样的长字段, 创建Hash Index, 方法是:
第一步, 为Product表创建一个计算列, 比如ProductNameHashField, 让它的值为CheckSum(ProductName), 这个字段的值将是Integer类型.
第二步, 创建一个ProductNameHashField字段的Index, 名为IX_Product_NameHash


-- Index it. Note that this is a non-unqiue index for collisions.
create index IX_Product_NameHash
on Product(ProductNameHashField);
go

怎么利用这个Hash Index, 来加速我们的query呢?


-- Look for the hash of the DName.
-- Always, always include the original data in case of collisions.
select customerId
from Product
where ProductNameHashField= checksum(@ProductName)-- this is the key to speed up query
and ProductName= @ProductName
go

通过观察执行计划, 发现查询速度确实有了很大的提高.

sqlite provider for ado.net

ado.net 2 dll download
http://sqlite.phxsoftware.com/

http://sourceforge.net/projects/sqlite-dotnet2

SQL Server management studio(sp2)

SQL Server management studio Express版本是Free的. 在SP2之前是不能进行管理SQL Server Compact 数据库的, SP2版本可以管理Compact数据库.

Download URL: http://www.microsoft.com/downloads/details.aspx?FamilyID=6053C6F8-82C8-479C-B25B-9ACA13141C9E&displaylang=en

SQL Server Express vs Compact Edition

面对Sybase和SQLite和Firebird以及VistaDB的竞争,SQL Server也推出了Compact版本, 它是比Express版本更加小巧的版本.
Express版本是用来代替以前的MSDE(Desktop Edition)的. 它是标准的C/S架构.
而Compact版本可以认为是文件数据库, 它的支持dll仅需要1.5M. 没有专门的log文件.

但Compact版本不像其他版本的SQL Server拥有丰富的SQL 语句. 比如它不支持Select Intersect/Except等操作.

Sqlite admin tools

1. SQLite Spy (http://www.yunqa.de/delphi/sqlitespy/),一个非常不错的SQLite Database Explorer and Query Analyzer。不需要安装。支持Unicode, 比较适合查询.
2. sqliteadmin http://sqliteadmin.orbmu2k.de/
包括structure定义向导, 比较适合数据库定义. 刚推出了支持Unicode的beta版本
3. Firefox Addon- SQLite Manager, 可以定义Table, 也可以进行查询, 它还有一个非常好的特点是, 能够让你有机会设置很多DB全局变量, 比如Cache size和page Size等等.

Thursday, May 8, 2008

one powerful front end-DbVisualizer

DbVisualizer
http://www.dbvis.com/products/dbvis/doc/

Features:
Support all main stream db.
Java application, so support windows and linux OS.
Can display the table reference and relationship diagram.

mysql server option explain

mysqld --help --verbose
用shell>mysqld --help --verbose这个命令, 显示mysql选项和可配置变量的表.输出以下信息:

possible variables for option--set-variable(-o) are:

back_log current value:5 //要求mysql能有的连接数量.back_log指出在mysql暂停接受连接的时间内有多少个连接请求可以被存在堆栈中

connect_timeout current value:5 //mysql服务器在用bad handshake(不好翻译)应答前等待一个连接的时间

delayed_insert_timeout current value:200 //一个insert delayed在终止前等待insert的时间

delayed_insert_limit current value:50 //insert delayed处理器将检查是否有任何select语句未执行,如果有,继续前执行这些语句

delayed_queue_size current value:1000 //为insert delayed分配多大的队

flush_time current value:0 //如果被设置为非0,那么每个flush_time 时间,所有表都被关闭

interactive_timeout current value:28800 //服务器在关上它之前在洋交互连接上等待的时间

join_buffer_size current value:131072 //用与全部连接的缓冲区大小

key_buffer_size current value:1048540 //用语索引块的缓冲区的大小,增加它可以更好的处理索引

lower_case_table_names current value:0 //

long_query_time current value:10 //如果一个查询所用时间大于此时间,slow_queried计数将增加

max_allowed_packet current value:1048576 //一个包的大小

max_connections current value:300 //允许同时连接的数量

max_connect_errors current value:10 //如果有多于该数量的中断连接,将阻止进一步的连接,可以用flush hosts来解决

max_delayed_threads current value:15 //可以启动的处理insert delayed的数量

max_heap_table_size current value:16777216 //

max_join_size current value:4294967295 //允许读取的连接的数量

max_sort_length current value:1024 //在排序blob或者text时使用的字节数量

max_tmp_tables current value:32 //一个连接同时打开的临时表的数量

max_write_lock_count current value:4294967295 //指定一个值(通常很小)来启动mysqld,使得在一定数量的write锁定之后出现read锁定

net_buffer_length current value:16384 //通信缓冲区的大小--在查询时被重置为该大小

query_buffer_size current value:0 //查询时缓冲区大小

record_buffer current value:131072 //每个顺序扫描的连接为其扫描的每张表分配的缓冲区的大小

sort_buffer current value:2097116 //每个进行排序的连接分配的缓冲区的大小

table_cache current value:64 //为所有连接打开的表的数量

thread_concurrency current value:10 //

tmp_table_size current value:1048576 //临时表的大小

thread_stack current value:131072 //每个线程的大小

wait_timeout current value:28800 //服务器在关闭它3之前的一个连接上等待的时间

How to choose the mysql table storage engine

在MySQL 5.1中,MySQL AB引入了新的插件式存储引擎体系结构,允许将存储引擎加载到正在运新的MySQL服务器中。 MySQL支持多种Table的存储引擎, 各有优缺点.
缺省的Engine为MyISAM, 一般情况下, 它有最好的效果, 尤其是query时候, 但它不支持事务.
还有一个InnoDB, 它支持最多的特性, 包括ACID. 还支持Clustered Index, Foreign Key等特性.
BDB(Berkeley DB的引擎, 该DB已经被MySQL收购):可替代InnoDB的事务引擎,支持COMMIT、ROLLBACK和其他事务特性。并且它的综合性能要好于InnoDB.

具体文档看:
http://dev.mysql.com/doc/refman/5.1/zh/pluggable-storage.html#pluggable-storage-choosing

MySQL Chinese Version Document

Home page:
http://dev.mysql.com/doc/refman/5.1/zh/

table storage option:
http://dev.mysql.com/doc/refman/5.1/zh/pluggable-storage.html

Optimization
http://dev.mysql.com/doc/refman/5.1/zh/optimization.html#