步骤一:
方法一:
对于C#而言,在https://www.nuget.org/packages/System.Data.SQLite.Core/中复制后粘贴到程序包管理器控制台可以直接下载并引入;
方法二:
SQLite3下载地址:https://www.sqlite.org/download.html
根据自己电脑位数选择Precompiled Binaries for Windows中的两项进行下载
然后解压到指定文件夹后将文件夹加入环境变量
输入sqlite3跳出以下语句便为成功
C:\Users\Administrator>sqlite3 SQLite version 3.36.0 2021-06-18 18:36:39 Enter ".help" for usage hints. Connected to a transient in-memory database. Use ".open FILENAME" to reopen on a persistent database. sqlite>
然后到http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki根据自己的框架版本下载对应的DLL;
在VS2019添加引用将DLL添加上去,然后根据DLL的位数x86/x64在项目属性-生成-目标平台中选择对应的位数(否则会报错!);
————————到这一步为止,配置部分已经完成了,比起其它数据库来说四舍五入算是0配置了;
步骤二:
接下来我们需要一个工具类,我网上找了一个模板然后做了些修改,后续根据自己的需求再做进一步的修改:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoWhatToday
{
class SQLiteDB
{
static SQLiteConnection m_dbConnection;
static string fileName;
/// <summary>
/// 设置要连接的数据库
/// </summary>
/// <param name="fileName">文件名</param>
public static void setFileName(string fileName)
{
SQLiteDB.fileName = fileName;
}
/// <summary>
/// 生成连接字符串
/// </summary>
/// <param name="fileName">文件名</param>
/// <returns></returns>
private static string CreateConnectionString()
{
SQLiteConnectionStringBuilder connectionString = new SQLiteConnectionStringBuilder();
connectionString.DataSource = @"Data/" + fileName;
string conStr = connectionString.ToString();
return conStr;
}
/// <summary>
/// 连接到数据库
/// </summary>
/// <param name="fileName">文件名</param>
/// <returns></returns>
private static SQLiteConnection dbConnection()
{
m_dbConnection = new SQLiteConnection(CreateConnectionString());
m_dbConnection.Open();
return m_dbConnection;
}
/// <summary>
/// 创建数据库文件
/// </summary>
/// <param name="fileName">文件名</param>
public static void CreateDBFile(string fileName)
{
string path = Environment.CurrentDirectory + @"/Data/";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string databaseFileName = path + fileName;
if (!File.Exists(databaseFileName))
{
SQLiteConnection.CreateFile(databaseFileName);
}
}
/// <summary>
/// 删除数据库
/// </summary>
/// <param name="fileName">文件名</param>
public static void DeleteDBFile(string fileName)
{
string path = Environment.CurrentDirectory + @"/Data/";
if (File.Exists(path))
{
File.Delete(path);
}
}
/// <summary>
/// 在指定数据库中创建一个table
/// </summary>
/// <param name="sql">sql语言</param>
/// <returns></returns>
public static bool CreateTable(string sql)
{
try
{
SQLiteCommand command = new SQLiteCommand(sql, dbConnection());
command.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Console.WriteLine("ExecuteNonQuery(" + sql + ")Err:" + ex);
return false;
}
finally
{
closeConn();
}
}
/// <summary>
/// 在指定数据库中删除一个table
/// </summary>
/// <param name="tablename">表名称</param>
/// <returns></returns>
public static bool DeleteTable(string tablename)
{
try
{
SQLiteCommand cmd = new SQLiteCommand("DROP TABLE IF EXISTS " + tablename, dbConnection());
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Console.WriteLine("ExecuteNonQuery(DROP TABLE IF EXISTS " + tablename + ")Err:" + ex);
return false;
}
finally
{
closeConn();
}
}
/// <summary>
/// 在指定表中添加列
/// </summary>
/// <param name="tablename">表名</param>
/// <param name="columnname">列名</param>
/// <param name="ctype">列的数值类型</param>
/// <returns></returns>
public static bool AddColumn(string tablename, string columnname, string ctype)
{
try
{
SQLiteCommand cmd = new SQLiteCommand("ALTER TABLE " + tablename + " ADD COLUMN " + columnname + " " + ctype, dbConnection());
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Console.WriteLine("ExecuteNonQuery(ALTER TABLE " + tablename + " ADD COLUMN " + columnname + " " + ctype + ")Err:" + ex);
return false;
}
finally
{
closeConn();
}
}
/// <summary>
/// 执行增删改查操作
/// </summary>
/// <param name="sql">查询语言</param>
/// <returns></returns>
public static int ExecuteNonQuery(string sql)
{
try
{
SQLiteCommand cmd;
cmd = new SQLiteCommand(sql, dbConnection());
cmd.ExecuteNonQuery().ToString();
return 1;
}
catch (Exception ex)
{
Console.WriteLine("ExecuteNonQuery(" + sql + ")Err:" + ex);
return 0;
}
finally
{
closeConn();
}
}
/// <summary>
/// 返回一条记录查询
/// </summary>
/// <param name="sql">sql查询语言</param>
/// <returns>返回字符串数组</returns>
public static string[] SqlRow(string sql)
{
try
{
SQLiteCommand sqlcmd = new SQLiteCommand(sql, dbConnection());//sql语句
SQLiteDataReader reader = sqlcmd.ExecuteReader();
if (!reader.Read())
{
return null;
}
string[] Row = new string[reader.FieldCount];
for (int i = 0; i < reader.FieldCount; i++)
{
Row[i] = (reader[i].ToString());
}
reader.Close();
return Row;
}
catch (Exception ex)
{
Console.WriteLine("SqlRow(" + sql + ")Err:" + ex);
return null;
}
finally
{
closeConn();
}
}
/// <summary>
/// 唯一结果查询
/// </summary>
/// <param name="sql">sql查询语言</param>
/// <returns>返回一个字符串</returns>
public static string sqlone(string sql)
{
try
{
SQLiteCommand sqlcmd = new SQLiteCommand(sql, dbConnection());//sql语句
return sqlcmd.ExecuteScalar().ToString();
}
catch
{
return "";
}
finally
{
closeConn();
}
}
/// <summary>
/// 获取一列数据
/// </summary>
/// <param name="sql">单列查询</param>
/// <param name="count">返回结果数量</param>
/// <returns>返回一个数组</returns>
public static List<string> sqlcolumn(string sql)
{
try
{
List<string> Column = new List<string>();
SQLiteCommand sqlcmd = new SQLiteCommand(sql, dbConnection());//sql语句
SQLiteDataReader reader = sqlcmd.ExecuteReader();
while (reader.Read())
{
Column.Add(reader[0].ToString());
}
reader.Close();
return Column;
}
catch (Exception ex)
{
Console.WriteLine("sqlcolumn(" + sql + ")Err:" + ex);
return null;
}
finally
{
closeConn();
}
}
/// <summary>
/// 返回记录集查询
/// </summary>
/// <param name="sql">sql查询语言</param>
/// <returns>返回查询结果集</returns>
public static DataTable SqlTable(string sql)
{
try
{
SQLiteCommand sqlcmd = new SQLiteCommand(sql, dbConnection());//sql语句
sqlcmd.CommandTimeout = 120;
SQLiteDataReader reader = sqlcmd.ExecuteReader();
DataTable dt = new DataTable();
if (reader != null)
{
dt.Load(reader, LoadOption.PreserveChanges, null);
}
return dt;
}
catch (Exception ex)
{
Console.WriteLine("SqlReader(" + sql + ")Err:" + ex);
return null;
}
finally
{
closeConn();
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public static void closeConn()
{
try
{
if (m_dbConnection.State == ConnectionState.Open)
m_dbConnection.Close();
else if (m_dbConnection.State == ConnectionState.Broken)
{
m_dbConnection.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("closeConnErr:" + ex);
}
}
}
}
步骤三:
接下来就是SQLITE一些常规SQL语句的学习,具体的可以去相关站点学习,我就列出常用的;
存储类 | 描述 |
---|---|
NULL | 值是一个 NULL 值。 |
INTEGER | 值是一个带符号的整数,根据值的大小存储在 1、2、3、4、6 或 8 字节中。 |
REAL | 值是一个浮点值,存储为 8 字节的 IEEE 浮点数字。 |
TEXT | 值是一个文本字符串,使用数据库编码(UTF-8、UTF-16BE 或 UTF-16LE)存储。 |
BLOB | 值是一个 blob 数据,完全根据它的输入存储。 |
创建删除数据库、删除表等的SQL语句已经在工具类里了(毕竟只需要指定库名或表名),接下来是一些要自己写的;
创建表:
CREATE TABLE database_name.table_name(
column1 datatype PRIMARY KEY(one or more columns),
column2 datatype,
column3 datatype,
.....
columnN datatype,
);
插入:
INSERT INTO TABLE_NAME [(column1, column2, column3,...columnN)]
VALUES (value1, value2, value3,...valueN);
查询:
SELECT column1, column2, columnN FROM table_name;
修改:
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
删除:
DELETE FROM table_name
WHERE [condition];
啊,总体来说和常用的SQL SERVE以及MySQL没太大的区别就是了,还有些join、limit之类的这边就些先不写了,等实际用到了再说吧(不过我估计我这自用的软件是用不太到了);
步骤四:
接下来就是实际运用部分了,等我写完发到github上然后改个标题这篇文章就结档了,还有好几篇要更新呢;
功能需求:
通过日期查询某天做过的事情
记录今天做过的事情
修改今天做过的事情
查询可以查询过去任意有记录的一天
记录和修改只能记录/修改今天或者昨天(修改只能修改上一次提交的记录)
可以输入一定数量的关键词(不同关键词用空格隔开,暂定最多5个)
可以通过关键词批量查找
UI:
主UI:记录、修改、查询
记录和修改可以共用一个UI,记录会新创建空白UI,修改会查询上一次记录的内容并进行填充
查询分为指定日期查询和关键词查询,也可以通过日期范围查询,范围差不能超过30天(先选择是否使用日期查询,然后选择使用指定日期还是日期范围,并列地,指定关键词)
后续功能目标:
可以指定今天或者第二天要做的事情
可以将要做的事情更变为做完的事情
统计完成率等数据,并进行图表输出