vs2010下使用sqlite

时间:2023-03-09 02:46:58
vs2010下使用sqlite

1.SQLite安装
SQlite官网:http://www.sqlite.org/download.html

找到以下截图中内容

第一个解压之后是sqlite3.exe,第二个解压之后是sqlite3.dll和sqlite3.def文件,第三个是sqlite3_analyzer(暂时没用到)

可将sqlite3.exe复制到C:\Windows\System32\下,这样一来可以从任何目录下运行CLP(SQLite CLP是使用和管理SQLite数据库最常用的工具),

打开命令行输入sqlite3回车,出现如下截图

2.使用SQLite创建数据库及数据表
重新进入命令行输入sqlite3 newsql_learn.db创建newsql_learn数据库

然后就是SQL语句了

其中.tables是查看数据库newsql_learn.db中所有的数据表,mytable是另一个已建好的数据表

在http://blog.****.net/byxdaz/article/details/5846023还有相关SQL介绍

3.要在VS2010中使用,那么需要添加SQLite的引用
网上有关于System.Data.Sqlite 下载及安装的注意事项:http://blog.****.net/norsd/article/details/6795695

本人也还不能确定哪个版本,自己的电脑是win8 64位,可是装的SQLite只有32位,VS2010目标生成CPU为32位,(之前整的Oracle是64位,默认目标生成为Any CPU,改成32位的Oracle出错,只好两者分开测试学习了),.NET 4.0版本

官网上下载System.Data.Sqlite:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

其实我是一个一个试的····最后是从上往下算第二个,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,果然,

5.利用代码创建数据表及访问显示表中数据,以下代码主要参照C#使用System.Data.SQLite操作SQLite原文地址http://hzy3774.iteye.com/blog/1691932

<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了,接下来可以在此基础上开发使用数据库的其他功能了···········
---------------------
作者:Micro_Ryan
来源:****
原文:https://blog.****.net/micro_ryan/article/details/47147557
版权声明:本文为博主原创文章,转载请附上博文链接!