using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;
public class SQLite2 {
public SqliteConnection connection;
public SqliteCommand command;
private SqliteDataReader reader;
public SQLite2(string path)
{
//创建SQLite对象的同时也创建SqliteConnection对象
connection = new SqliteConnection (path);
//打开数据库
connection.Open ();
commm ();
}
public SqliteCommand commm()
{
command = connection.CreateCommand ();
return command;
}
//增加数据
public SqliteDataReader insertt(string table_name,string [] fieldnames,object[] values)
{
//如果字段的个数,和数据的个数不相等,无法执行插入的语句,所以返回一个null
if (true) {
}
command.CommandText = "insert into " + table_name + "(";
for (int i = 0; i < fieldnames.Length; i++) {
command.CommandText += fieldnames [i];
if (i < fieldnames.Length - 1) {
command.CommandText += ",";
}
}
command.CommandText += ")" + "values (";
for (int i = 0; i < values.Length; i++) {
command.CommandText += values [i];
if (i < values.Length - 1) {
command.CommandText += ",";
}
}
command.CommandText += ")";
return command.ExecuteReader ();
}
//删除数据
public SqliteDataReader DeleteData(string table_name,string[] conditions)
{
command.CommandText = "delete from " + table_name + " where " + conditions [0];
for (int i = 1; i < conditions.Length; i++) {
//or 表示或者
//and 表示并且
command.CommandText += " or " + conditions[i];
}
return command.ExecuteReader ();
}
//【修改数据】
public SqliteDataReader updatedata(string table_name,string[] values,string[] conditions)
{
command.CommandText = "update " + table_name + " set " + values [0];
for (int i = 1; i < values.Length; i++) {
command.CommandText += "," + values [i];
}
command.CommandText += " where " + conditions[0];
for (int i = 1; i < conditions.Length; i++) {
command.CommandText += " or " + conditions [i];
}
Debug.Log (command.CommandText);
return command.ExecuteReader ();
}
//查询数据
public SqliteDataReader Selectdata(string table_name, string[] fields)
{
command.CommandText = "select " + fields [0];
for (int i = 1; i < fields.Length; i++) {
command.CommandText += "," + fields [i];
}
command.CommandText += " from " + table_name;
return command.ExecuteReader ();
}
//查询整张表的数据
public SqliteDataReader selecrfullTableData(string table_name)
{
command.CommandText = "select * from " + table_name;
return command.ExecuteReader ();
}
public void CloseDaraBase()
{
connection.Close ();
command.Cancel ();
}
}
数据库
最新推荐文章于 2024-02-20 14:48:07 发布