SQLite安裝及在Visual studio 2010(VS2010)中的使用
本人菜鳥,本文僅自己學習過程中總結,望高手指點
1.SQLite安裝
找到以下截圖中內容
第一個解壓之後是sqlite3.exe,第二個解壓之後是sqlite3.dll和sqlite3.def檔案,第三個是sqlite3_analyzer(暫時沒用到)
可將sqlite3.exe複製到C:\Windows\System32\下,這樣一來可以從任何目錄下執行CLP(SQLiteCLP是使用和管理SQLite資料庫最常用的工具),
開啟命令列輸入sqlite3回車,出現如下截圖
2.使用SQLite建立資料庫及資料表
重新進入命令列輸入sqlite3 newsql_learn.db建立newsql_learn資料庫
然後就是SQL語句了
其中.tables是檢視資料庫newsql_learn.db中所有的資料表,mytable是另一個已建好的資料表
3.要在VS2010中使用,那麼需要新增SQLite的引用
本人也還不能確定哪個版本,自己的電腦是win8 64位,可是裝的SQLite只有32位,VS2010目標生成CPU為32位,(之前整的Oracle是64位,預設目標生成為Any CPU,改成32位的Oracle出錯,只好兩者分開測試學習了),.NET 4.0版本
其實我是一個一個試的····最後是從上往下算第二個,sqlite-netFx40-setup-x86-2010-1.0.97.0.exe,哎,擦TM
下載完,雙擊安裝,預設路徑是C:\Program Files (x86)\System.Data.SQLite
安裝完到程式中點選新增引用(Project(專案)-->Add reference..(新增引用..)-->.Net(框架))
若你的左側列中有System.Data.SQLite恭喜你,你不用糾結了,就是那個了,選中點選確定,要是沒有,可以點選右下角的瀏覽(不同VS版本,可能瀏覽的位置不同),進入C:\Program Files (x86)\System.Data.SQLite\2010\bin\找到System.Data.SQLite.dll點選確定
4.開始呼叫SQLite
名稱空間新增
using System.Data;
using System.Data.SQLite;
主函式中新增
string connectString = @"Data Source=F:\newsql_learn.db;Pooling=true;FailIfMissing=false";
SQLiteConnection conn = new SQLiteConnection(connectString); //新建一個連線
conn.Open(); //開啟連線,如果sqlite.db存在就正常開啟,如果不存在則建立一個SQLite.db檔案
SQLiteCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from table1"; //資料庫中要事先有個orders表
cmd.CommandType = CommandType.Text;
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
Console.WriteLine(reader[0].ToString());
}
注:這裡出現了錯誤,莫名其妙的錯誤,系統顯示沒有找到table1資料表,可是利用SQLite命名行進行查詢卻可以找到table1及該資料表下的所有資料,無奈之下下載了SQLiteBrowser,可是安裝之後發現數據庫newsql_learn中沒有table1,只是有之前建立的mytable,這下更鬱悶了,什麼情況,也就是說在SQLite3.exe中可以發現table1表,在呼叫該表以及SQLiteBrowser中都沒有發現該表························
難道是建立的路徑不正確,他們訪問的資料庫名字雖然全都是newsql_learn.db可是更本不是一個數據庫?於是整個電腦搜尋newsql_learn.db,果然,
<span style="font-size:18px;"> //第一步先建立資料表</span>
<span style="font-size:18px;"> string dbPath = Environment.CurrentDirectory + "/test.db";//指定資料庫路徑 位於程式根目錄下
using (SQLiteConnection conn = new SQLiteConnection("Data Source =" + dbPath))//建立連線
{
conn.Open();//開啟連線
string sql = "CREATE TABLE IF NOT EXISTS student(id integer, name varchar(20), sex varchar(2));";//建立資料表語句
SQLiteCommand cmdCreateTable = new SQLiteCommand(sql, conn);
cmdCreateTable.ExecuteNonQuery();//如果表不存在,建立資料表
using (SQLiteTransaction tran = conn.BeginTransaction())//例項化一個事務 使用事務操作提高效率
{
for (int i = 0; i < 100; i++)
{
SQLiteCommand cmd = new SQLiteCommand(conn);//例項化SQL命令
cmd.Transaction = tran;
cmd.CommandText = "insert into student values(@id, @name, @sex)";//設定帶參SQL語句
cmd.Parameters.AddRange(new[] {//新增引數
new SQLiteParameter("@id", i),
new SQLiteParameter("@name", "中國人"),
new SQLiteParameter("@sex", "男")
});
cmd.ExecuteNonQuery();//執行查詢
}
tran.Commit();//提交
}
}
</span>
<span style="font-size:18px;"> //第二步,建立第二個資料表</span>
<span style="font-size:18px;"> SQLiteConnection conn1 = null;
string dbPath2 = "Data Source =" + Environment.CurrentDirectory + "/test.db";
conn1 = new SQLiteConnection(dbPath2);//建立資料庫例項,指定檔案位置
conn1.Open();//開啟資料庫,若檔案不存在會自動建立
//建立第二張表
string sql1 = "CREATE TABLE IF NOT EXISTS student2(id integer, name varchar(20));";//建表語句
SQLiteCommand cmdCreateTable2 = new SQLiteCommand(sql1, conn1);
cmdCreateTable2.ExecuteNonQuery();//如果表不存在,建立資料表
SQLiteCommand cmdInsert = new SQLiteCommand(conn1);
cmdInsert.CommandText = "INSERT INTO student2 VALUES(1, '小紅')";//插入幾條資料
cmdInsert.ExecuteNonQuery();
cmdInsert.CommandText = "INSERT INTO student2 VALUES(2, '小李')";
cmdInsert.ExecuteNonQuery();
cmdInsert.CommandText = "INSERT INTO student2 VALUES(3, '小明')";
cmdInsert.ExecuteNonQuery();
</span>
<span style="font-size:18px;"> //第三步查詢資料</span>
<span style="font-size:18px;"> string sql2 = "select * from student";
SQLiteCommand cmdQ = new SQLiteCommand(sql2, conn1);
SQLiteDataReader reader = cmdQ.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader.GetInt32(0) + " " + reader.GetString(1) + " " + reader.GetString(2));
}
string sql3 = "select id,name from student2";
SQLiteCommand cmdQ2 = new SQLiteCommand(sql3, conn1);
SQLiteDataReader reader2 = cmdQ2.ExecuteReader();
while (reader2.Read())
{
Console.WriteLine(reader2.GetInt32(0) + " " + reader2.GetString(1) );
}
conn1.Close();</span>
O了,接下來可以在此基礎上開發使用資料庫的其他功能了···········