SQLite入门指南:轻松学习带有实例的完整教程(含示例)

SQLite入门指南:轻松学习带有实例的完整教程(含示例)

SQLite官网:https://www.sqlite.org/index.html

菜鸟教程文档:https://www.runoob.com/sqlite/sqlite-tutorial.html

一、数据库简介与基本语法

1.1-数据库的作用

txt去保存1万行的数据.(数据量超过一定量级[ 大于1w ])

数据格式的管理,以及数据内容的分片

1.2-数据库的选择

目前所说:都是SQL(结构化查询语言)语句

单机版本:

ACCESS(微软)

最大缺点:必须要安装Office、数据量、查询速度、写法有少许不同

SQLite

唯一携带一个DLL驱动文件(几百K)

缺点:超过10w的,不建议使用。

企业级数据库:

MsSQLServer

数据量:5000w没什么问题

最适合C#

My SQL:

要一份非.net官方的驱动

开源

相对于MSSQL Server,优势是体积小,跨平台

Oracle:

需要非官方驱动

适合JAVA

MongDB:

后期支秀

非关系型数据库

二、数据库增删改查语法与实例

2.1-创建表

(1)下载并打开这个工具

(2)创建一个数据库,然后创建一个表如下:

(3)添加列明、数据类型、约束

2.2-增删改查

--插入

--注意:Integer允许自动增长(不要被Identity 忽悠)

insert into UserInfo(UserId,UserNames,UserPasss,RegDate) values(1001,'admin','admin','2021-01-21')

insert into UserInfo(UserId,UserNames,UserPasss,RegDate) values(1002,'sanha','sanha', datetime('now','localtime'))

--查询

select * from UserInfo

--Limit 跳过几个,取几个

--Limit 2,2 跳过2个,取2个

--删除

delete from UserInfo where UserId=1002

--修改

update UserInfo set UserNames='sanha_update' where UserId=1002

2.3-使用WinForm和SQLite做登录注册

(1)管理Nuget程序包,下载这个类库:

1.1-将数据库文件拷贝在Bin路径下。

(2)写一个SQLite帮助类

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data;

using System.Data.SQLite;

using System.Threading.Tasks;

using System.Configuration;

namespace SQLite

{

public class SQLiteHelper

{

private readonly string _str;

public SQLiteHelper(string str) {

_str = str;

}

//获取连接字符串

//private static readonly string str = ConfigurationManager.ConnectionStrings["DBFilerURL"].ConnectionString;

///

/// 做增删改的功能

///

/// SQL语句

/// SQL语句中的参数

/// 受影响的行数

public int ExecuteNonQuery(string sql, params SQLiteParameter[] ps)

{

//连接数据库

using (SQLiteConnection con = new SQLiteConnection(_str))

{

using (SQLiteCommand cmd = new SQLiteCommand(sql, con))

{

con.Open();//打开数据库

if (ps != null)

{

cmd.Parameters.AddRange(ps);//参数,加集合(ps)

}

return cmd.ExecuteNonQuery();

}

}

}

///

/// 查询首行首列

///

/// SQL语句

/// SQL语句的参数

/// 返回首行首列object

public object ExecuteScalar(string sql, params SQLiteParameter[] ps)

{

using (SQLiteConnection con = new SQLiteConnection(_str))

{

using (SQLiteCommand cmd = new SQLiteCommand(sql, con))

{

con.Open();

if (ps != null)

{

cmd.Parameters.AddRange(ps);

}

return cmd.ExecuteScalar();

}

}

}

///

/// 查询多行

///

/// SQL语句

/// SQL语句的参数

/// 返回多行SQLiteDataReader

public SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] ps)

{

SQLiteConnection con = new SQLiteConnection(_str);

using (SQLiteCommand cmd = new SQLiteCommand(sql, con))

{

if (ps != null)

{

cmd.Parameters.AddRange(ps);

}

try

{

con.Open();

return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

}

catch (Exception ex)

{

con.Close();

con.Dispose();

throw ex;

}

}

}

///

/// 查询数据表

///

/// SQL语句

/// SQL语句中的参数

/// 返回表DataTable

public DataTable ExecuteTable(string sql, params SQLiteParameter[] ps)

{

DataTable dt = new DataTable();

using (SQLiteDataAdapter sda = new SQLiteDataAdapter(sql, _str))

{

if (ps != null)

{

sda.SelectCommand.Parameters.AddRange(ps);

}

sda.Fill(dt);

return dt;

}

}

}

}

(3)写一个简单的界面

(4)在后端代码中先写上这些代码

//获取数据库路径

public static string SQLitePath = AppDomain.CurrentDomain.BaseDirectory + "db/SQLiteDemo1.db";

//数据库连接字符串

public static string str = string.Format("Data Source={0};Pooling=true;FailIfMissing=false;", SQLitePath);

//实例化对象

SQLiteHelper SQLite = new SQLiteHelper(str);

(5)【登录】的逻辑

private void button2_Click(object sender, EventArgs e)

{

string name = this.textBox1.Text.ToString();

string password = this.textBox2.Text.ToString();

//参数化查询

string sql = string.Format("select UserId from UserInfo where UserNames=@name and UserPasss=@password;");

SQLiteParameter[] parameters =new SQLiteParameter[]

{

new SQLiteParameter("@name",name),

new SQLiteParameter("@password",password)

};

object obj=SQLite.ExecuteScalar(sql, parameters);

int i =Convert.ToInt32(obj);

if (i > 0)

{

this.label4.Text = "登录成功!";

this.label4.Show();

}

else {

this.label4.Text = "登录失败!";

this.label4.Show();

}

}

(6)【注册】的逻辑

private void button1_Click(object sender, EventArgs e)

{;

string name = this.textBox1.Text.ToString();

string password = this.textBox2.Text.ToString();

//参数化查询

string sql = string.Format("insert into UserInfo(UserId,UserNames,UserPasss,RegDate) values(@userid,@username,@passwod,datetime('now','localtime'))");

SQLiteParameter[] parameters = new SQLiteParameter[]

{

new SQLiteParameter("@userid",new Random().Next(10)),

new SQLiteParameter("@username",name),

new SQLiteParameter("@passwod",password)

};

object obj = SQLite.ExecuteNonQuery(sql, parameters);

int i = Convert.ToInt32(obj);

if (i > 0)

{

this.label4.Text = "注册成功!";

this.label4.Show();

}

else

{

this.label4.Text = "注册失败!";

this.label4.Show();

}

}

原文链接:

https://www.cnblogs.com/kimiliucn/p/17595354.html

猜你喜欢

壁虎害怕什么东西(壁虎最怕的7种东西)
365be是啥

壁虎害怕什么东西(壁虎最怕的7种东西)

📅 08-19 ❤️ 282
CAD中怎么放大图形?CAD缩放快捷键应用技巧
365体育竞彩足球

CAD中怎么放大图形?CAD缩放快捷键应用技巧

📅 09-04 ❤️ 287
南非世界杯 德国队 南非世界杯 德国队 全场回放
【先锋快递】美国公司
365be是啥

【先锋快递】美国公司

📅 07-17 ❤️ 796
iphone语音备忘录能录多久
365be是啥

iphone语音备忘录能录多久

📅 06-28 ❤️ 168
SMC(中国)有限公司怎么样?
365体育竞彩足球

SMC(中国)有限公司怎么样?

📅 07-03 ❤️ 922
世界杯东道主要打预选赛吗 世界杯东道主是直接进世界杯吗?
揭秘天津:共享单车分布哪家强?带你探秘热门骑行地
uzi为什么叫小狗
365体育竞彩足球

uzi为什么叫小狗

📅 09-21 ❤️ 39