using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;
/// <summary>
/// [SQLitemanaqer]
/// [Design]
/// 1.用来设计当前数据库内容
/// 2.能设计当前数据库有哪些表,每个表里面有哪些字段,以及每个字段的类型
///
/// 【Data】
/// 1.用来查看和修改数据
///
/// [SQL]
/// 1.用来执行SQL语句
/// 2.输入完语句之后点击Execute执行
///
/// SQL语句
/// 增加
/// 1.向FirstTable表中茶如一条数据,这个表有两个字段,第一个字段为字符串类型,第二个字段为float类型
/// insert into FristTable values ('文字强',99.3)
///
/// 2.向FirstTable表中插入一条数据,指定只插入name字段的数据为LONG
/// insert into FirstTable (name) values ('Long')
///
/// 删除
/// 1.删除一条数据从FirstTable表中,根据name字段,删除字段值为LONG的数据
/// delete from FirstTable where name = 'LONG'
///
/// 2.删除FirstTable表中所有的数据(不是删除表,而是清空所有数据)
/// delete from FirstTable
///
/// 【改】
/// 1.修改FirstTable表中的score字段的值为60,当数据小于60的时候
/// update FirstTable set score = '60' WHERE score < 60
///
/// 2.修改FirstTable表中的score字段的值为32,条件是name字段值为’文字强‘
///
/// 3.修改FirstTable表中的name为王宁宁,条件是当数据的name字段值为王宁
/// update FirstTable set name = '王宁宁' where name = '王宁'
///
/// 【查】
/// 1.只查看FirstTable的score值
/// select score from FirstTable
///
/// 2.如果想要查看多列的信息,列名称需要使用,号分隔
/// select name,score from FirstTable
///
/// 3.如果想要查看表中所有的数据,使用*号通配符
/// select * from FirstTable
/// </summary>
public class MySQLite : MonoBehaviour {
// Use this for initialization
void Start () {
// Debug.Log(Application.streamingAssetsPath);
// Command ();
ReadFullTable();
}
// Update is called once per frame
void Update () {
}
void Connection()
{
SqliteConnection con;
string path = "data source = " + Application.streamingAssetsPath + "/database0117.sqlite";
con = new SqliteConnection (path);
con.Open ();
con.Close ();
}
void Command()
{
SqliteConnection con = new SqliteConnection ("data source =" + Application.streamingAssetsPath + "/database0117.sqlite");
con.Open();
SqliteCommand Com = con.CreateCommand();
// Com.CommandText = "inset into FirstTable values ('小芳’,99)";
// Com.ExecuteNonQuery();
//
// Com.CommandText = "delete from FirstTable where name = '麦林炮手'";
// int result = Com.ExecuteNonQuery ();
// Debug.Log ("成功删除" + result + "数据");
//当数据库中存在多个满足条件的数据时,只返回第一个满足查找条件的数值
Com.CommandText = "select score from FirstTable where name = '小方'";
object result = Com.ExecuteScalar();
Debug.Log (result);
// con.Close ();
}
void ReadFullTable()
{
SqliteConnection con = new SqliteConnection ("data source =" + Application.streamingAssetsPath + "/database0117.sqlite");
con.Open ();
SqliteCommand com = con.CreateCommand ();
com.CommandText = "select * from FirstTable";
SqliteDataReader reader = com.ExecuteReader ();
//读取reader内容,当吧reader的内容读完的时候返回false
while (reader.Read()) {
//获取第0列的字段名
// Debug.Log(reader.GetName(0));
for (int i = 0; i < reader.FieldCount; i++) {
//获取第1列的字段所对应的值
Debug.Log (reader.GetValue (i));
}
//获取第0列的字段所对应的值
// Debug.Log (reader.GetValue (0));
}
//得到当前读取表的列数
Debug.Log (reader.FieldCount);
con.Close ();
}
}
数据库
最新推荐文章于 2024-07-29 15:22:37 发布