用PHP跟.net操作sqlite数据库
<div>用PHP操作sqlite数据库</div><div> </div><div>a、 如何连接sqlite数据库?</div><div>if ($db = sqlite_open('mysqlitedb', 0666, $sqliteerror)) {</div><div>select from sqlite_master;</div><div>echo "数据库连接成功!n</div><div>";</div><div>} else {</div><div>die($sqliteerror);</div><div>}<div style="page-break-after: always;"><span style="display: none;"><!--more-->& nbsp ;</span></div></div><div>b、 如何列出数据库中所有的表?</div><div>if ($db = sqlite_open('mysqlitedb', 0666, $sqliteerror)) {</div><div>$result = sqlite_array_query($db, 'select * from sqlite_master;');</div><div>foreach ($result as $entry) {</div><div>echo 'talbe name='.$entry['name']."</div><div>n";</div><div>echo 'sql='.$entry['sql']."</div><div>n";</div><div>echo "</div><div>--------------------------------------------------------------------------------</div><div>";</div><div>}</div><div>sqlite_close($db);</div><div>} else {</div><div>die($sqliteerror);</div><div>}}</div><div>c、 对sqlite数据库的查询,以及结果集的显示</div><div>if ($db = sqlite_open('mysqlitedb', 0666, $sqliteerror)) {</div><div>$result = sqlite_array_query($db, 'select name, email from user ', SQLITE_ASSOC);</div><div>echo "user表查询结果:</div><div>n";</div><div>echo " n name email</div><div>n";</div><div>foreach ($result as $entry) {</div><div>echo ' '.$entry['name']." " $entry['email']."</div><div>n";</div><div>}</div><div>echo ' ';</div><div>sqlite_close($db);</div><div>} else {</div><div>die($sqliteerror);</div><div>}</div><div>d、 数据库对象记录的增加、删除、修改</div><div>sqlite_query($db, "INSERT INTO user VALUES('user".$i."'" ",'user".$i."@ hichina. com')");</div><div>sqlite_query($db, "delete from user where user=’user99’");</div><div>sqlite_query($db, 'UPDATE user SET email="lilz@ hichina .com" where name="user1"');</div><div> </div><div>使用.NET操作SQLLITE<div>先下载ADO.NET2.0 Provider for SQLite。下载binaries zip版就可以了。下载完后解压缩,可以在bin目录下找到System.Data.SQLite.DLL。在vs2008中用Add Reference功能把System.Data.SQLite.DLL加到工程里就可以了。运行下面代码试试:</div><div>string datasource = "e:/tmp/test.db";</div><div>System.Data.SQLite.SQLiteConnection.CreateFile(datasource);</div><div>//连接数据库</div><div>System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection();</div><div>System.Data.SQLite.SQLiteConnectionStringBuilder connstr = new System.Data.SQLite.SQLiteConnectionStringBuilder();</div><div>connstr.DataSource = datasource;</div><div>connstr.Password = "admin";//设置密码,SQLite ADO.NET实现了数据库密码保护</div><div>conn.ConnectionString = connstr.ToString();</div><div>conn.Open();</div><div>//创建表</div><div>System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();</div><div>string sql = "CREATE TABLE test(username varchar(20),password varchar(20))";</div><div>cmd.CommandText = sql;</div><div>cmd.Connection = conn;</div><div>cmd.ExecuteNonQuery();</div><div>//插入数据</div><div>sql = "INSERT INTO test VALUES('a','b')";</div><div>cmd.CommandText = sql;</div><div>cmd.ExecuteNonQuery();</div><div>//取出数据</div><div>sql = "SELECT FROM test";</div><div>cmd.CommandText = sql;</div><div>System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader();</div><div>StringBuilder sb = new StringBuilder();</div><div>while (reader.Read())</div><div>{</div><div>sb.Append("username:").Append(reader.GetString(0)).Append("n")</div><div>.Append("password:").Append(reader.GetString(1));</div><div>}</div><div>MessageBox.Show(sb.ToString());</div>