什么是SQLite
1. SQLite是一款轻型的数据库
2. SQLite的设计目标是嵌入式的
3. SQLite占用资源非常的低
4. SQLite能够支持Windows/Linux/Unix等等主流的操作系统
准备工作
安装SQLiteManager
在Unity中导入
Mono.Data.Sqlite.dll
sqlite3.dll
System.Data.dll
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mono.Data.Sqlite;
using System;
using UnityEngine.UI;
public class StudentManager : MonoBehaviour {
//输出文字
public Text UItext;
//数据库地址
string path;
//连接
public SqliteConnection connection;
//命令行
public SqliteCommand command;
//数据库操作类
public SqliteDataReader reader;
// Use this for initialization
void Start () {
//初始化地址
path = "Data source = " + Application.dataPath + "/Student.sqlite";
//创建连接
connection = new SqliteConnection(path);
}
//OnGUI按钮
private void OnGUI()
{
if (GUILayout.Button(" 创建 "))
{
CreateDataBase();
//关闭数据库
Dispose();
}
if (GUILayout.Button(" 添加 "))
{
Dispose();
AddStudent();
//关闭数据库
Dispose();
}
if (GUILayout.Button(" 查询 "))
{
Dispose();
UItext.text = SelectStudent();
//关闭数据库
Dispose();
}
if (GUILayout.Button(" 查询 2 "))
{
Dispose();
UItext.text = SelectStudent2();
//关闭数据库
Dispose();
}
if (GUILayout.Button(" 修改 "))
{
Dispose();
UpdateStudent();
//关闭数据库
Dispose();
}
if (GUILayout.Button(" 删除 "))
{
DeleteStudent();
Dispose();
}
}
private void Dispose()
{
if (command != null)
{
command.Dispose();
command = null;
}
if (reader != null)
{
reader.Close();
reader = null;
}
connection.Close();
}
private void DeleteStudent()
{
//打开数据库
connection.Open();
//创建命令行
command = connection.CreateCommand();
//编辑命令_删除单数
string commandtext0 = "Delete From Student where rowid%2 != 0";
//commandtext0 = "Delete From Student ";
command.CommandText = commandtext0;
//执行命令
command.ExecuteNonQuery();
//清除命令
command.Dispose();
command = null;
}
private void UpdateStudent()
{
//打开数据库
connection.Open();
//创建命令行
command = connection.CreateCommand();
//编辑命令_修改年龄
string commandtext0 = "Update Student set Sage = '18' where Sname = '杨文川' ";
command.CommandText = commandtext0;
//执行命令
command.ExecuteNonQuery();
//清除命令
command.Dispose();
command = null;
}
//
//查询
private string SelectStudent()
{
//创建返回对象
string text = "";
//打开数据库
connection.Open();
//创建命令行
command = connection.CreateCommand();
//编辑命令_名字
string commandtext0 = "select \"Sname\" ,\"Sid\" ,\"Sage\" from Student";
command.CommandText = commandtext0;
//保存信息
reader = command.ExecuteReader();
//创建字典
Dictionary dic = new Dictionary();
for (int i = 0; i < 20; i++)
{
if (reader.Read())
{
print(reader.GetString(2));
dic.Add("学生:"+(i+1).ToString()+"的id为"+reader.GetString(1).ToString(), reader.GetString(0));
}
}
foreach(KeyValuePair item in dic)
{
text += item.Key;
text += "name:" + item.Value +"\n";
}
//清除命令
command.Dispose();
command = null;
//关闭SqliteDataReader
reader.Close();
reader = null;
//返回字符串
return text;
}
//查询2
private string SelectStudent2()
{
//创建返回对象
string text = "";
//打开数据库
connection.Open();
//创建命令行
command = connection.CreateCommand();
//编辑命令
string commandtext = "select * from student where Sage>=12 and Sage<18";
command.CommandText = commandtext;
//保存信息
reader = command.ExecuteReader();
for (int i = 0; i < 20; i++)
{
if (reader.Read())
{
for (int a = 0; a < reader.FieldCount; a++)
{
text += reader.GetString(a);
}
}
}
//清除命令
command.Dispose();
command = null;
//返回字符串
return text;
}
//添加
private void AddStudent()
{
//打开数据库
connection.Open();
//创建命令行
command = connection.CreateCommand();
//编辑命令
string commandtext0 = "insert into student (Sid,Sname,Ssex, Sage, Sdept)values('171111-1','杨文博','男','16','vr')";
command.CommandText = commandtext0;
//无返回值
command.ExecuteNonQuery();
//清除命令
command.Dispose();
command = null;
//创建命令行
command = connection.CreateCommand();
string commandtext1 = "insert into student (Sid,Sname,Ssex, Sage, Sdept)values('171111-2','李枭','男','16','vr')";
command.CommandText = commandtext1;
//无返回值
command.ExecuteNonQuery();
//清除命令
command.Dispose();
command = null;
//创建命令行
command = connection.CreateCommand();
string commandtext2 = "insert into student (Sid,Sname,Ssex, Sage, Sdept)values('171111-3','袁喆','女','16','vr')";
command.CommandText = commandtext2;
//无返回值
command.ExecuteNonQuery();
//清除命令
command.Dispose();
command = null;
//创建命令行
command = connection.CreateCommand();
string commandtext3 = "insert into student (Sid,Sname,Ssex, Sage, Sdept)values('171111-4','杨文川','男','16','vr')";
command.CommandText = commandtext3;
//无返回值
command.ExecuteNonQuery();
//清除命令
command.Dispose();
command = null;
}
//创建数据库,检查数据库是否存在
void CreateDataBase()
{
//打开数据库
connection.Open();
//创建命令行
command = connection.CreateCommand();
//编辑命令
string commandtext0 = "Select count(*) from sqlite_master where type = \'table\' and name = \'Student\'";
command.CommandText = commandtext0;
//保存获取信息
reader = command.ExecuteReader();
//设置是否存在表student
bool isExit = false;
//读取数据库
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
if (reader.GetValue(i).ToString() != "0")
{
isExit = true;
Debug.Log("已存在");
}
}
}
//清除命令
command.Dispose();
command = null;
//关闭SqliteDataReader
reader.Close();
//如果不存在Student该表
if (!isExit)
{
string commandtext1 = "Create Table Student(Sid text,Sname text,Ssex text, Sage text, Sdept text)";
command = connection.CreateCommand();
command.CommandText = commandtext1;
//无返回值
command.ExecuteNonQuery();
command.Dispose();
command = null;
Debug.Log("创建完成");
}
}
// Update is called once per frame
void Update () {
}
}