Perst嵌入式数据库应用小结
PERST这种嵌入式数据库的概念与我们常用的关系数据库有些不同.
讲讲它的用法
首先定义一个类,其结构与数据表结构一样,继承于Persistent.
比如:
再要调用数据库的地方打开数据库,方法如下 :
插入數據方法如下:
讲讲它的用法
首先定义一个类,其结构与数据表结构一样,继承于Persistent.
比如:
public Class : Persistent
{
int _id;
public int ID
{
get {return _id;}
set {_id = value;}
}
string _name;
public string Name
{
get {return _id;}
set {_id = value;}
}
}
然后再给此类定义一个索引类,继承于Persistent{
int _id;
public int ID
{
get {return _id;}
set {_id = value;}
}
string _name;
public string Name
{
get {return _id;}
set {_id = value;}
}
}
public class ClassRoot : Persistent
{
internal Index IdInex;
internal FieldIndex NameIndex;
}
{
internal Index IdInex;
internal FieldIndex NameIndex;
}
再要调用数据库的地方打开数据库,方法如下 :
Perst.Storage storage = Perst.StorageFactory.Instance.CreateStorage();
storage.Open("保存数据文件路径",32 * 1024 * 1024);
Perst.ClassRoot root=(Perst.ClassRoot)storage.Root;
if (root==null)
{
root=new ClassRoot();
root.IdInex=storage.CreateIndex(typeof(int),true); //创建一个索引
root.XmIndex=storage.CreateFieldIndex(typeof(Class),"Name",false); //创建一个文件索引,类型,字段,是否唯一
storage.Root=root;
}
return storage;
}
storage.Open("保存数据文件路径",32 * 1024 * 1024);
Perst.ClassRoot root=(Perst.ClassRoot)storage.Root;
if (root==null)
{
root=new ClassRoot();
root.IdInex=storage.CreateIndex(typeof(int),true); //创建一个索引
root.XmIndex=storage.CreateFieldIndex(typeof(Class),"Name",false); //创建一个文件索引,类型,字段,是否唯一
storage.Root=root;
}
return storage;
}
插入數據方法如下:
public void Insert()
{
Storage storage = OpenDataBase(); //打开数据库
ClassRoot root = (ClassRoot)storage.Root; //指定接口的索引类型
Class class = new Class();
class.ID = 1;
class.Name = "1";
root.IdIndex.Put(class.ID,class);
root.NameIndex.Put(class);
storage.Commit(); //执行操作,假如操作失败则恢复到操作之前的状态
storage.Close(); //关闭数据连接
}
{
Storage storage = OpenDataBase(); //打开数据库
ClassRoot root = (ClassRoot)storage.Root; //指定接口的索引类型
Class class = new Class();
class.ID = 1;
class.Name = "1";
root.IdIndex.Put(class.ID,class);
root.NameIndex.Put(class);
storage.Commit(); //执行操作,假如操作失败则恢复到操作之前的状态
storage.Close(); //关闭数据连接
}
下面是删除操作:
public void Delete()
{
Storage storage = OpenDataBase(); //打开数据库
ClassRoot root = (ClassRoot)storage.Root; //指定接口的索引类型
Class class = new Class(); //这时class则是已知的将要删除的对象
class.ID = 1;
class.Name = "1";
root.IdIndex.Remove(class.ID,class);
root.NameIndex.Remove(class);
storage.Commit(); //执行操作,假如操作失败则恢复到操作之前的状态
storage.Close(); //关闭数据连接
}
{
Storage storage = OpenDataBase(); //打开数据库
ClassRoot root = (ClassRoot)storage.Root; //指定接口的索引类型
Class class = new Class(); //这时class则是已知的将要删除的对象
class.ID = 1;
class.Name = "1";
root.IdIndex.Remove(class.ID,class);
root.NameIndex.Remove(class);
storage.Commit(); //执行操作,假如操作失败则恢复到操作之前的状态
storage.Close(); //关闭数据连接
}
下面是更新操作的Set()方法,使用set方法时,假如更新的对象存在则执行更新,否则就执行插入操作。
public void Update()
{
Storage storage = OpenDataBase(); //打开数据库
ClassRoot root = (ClassRoot)storage.Root; //指定接口的索引类型
Class class = new Class(); //这时class则是已知的要更新的对象
class.ID = 1;
class.Name = "1";
root.IdIndex.Set(class.ID,class);
root.NameIndex.Set(class);
storage.Commit(); //执行操作,假如操作失败则恢复到操作之前的状态
storage.Close(); //关闭数据连接
}
{
Storage storage = OpenDataBase(); //打开数据库
ClassRoot root = (ClassRoot)storage.Root; //指定接口的索引类型
Class class = new Class(); //这时class则是已知的要更新的对象
class.ID = 1;
class.Name = "1";
root.IdIndex.Set(class.ID,class);
root.NameIndex.Set(class);
storage.Commit(); //执行操作,假如操作失败则恢复到操作之前的状态
storage.Close(); //关闭数据连接
}
下面就是查询操作了。对于查询出来的对象可以用foreach循环来提取。
public Class Select()
{
Storage storage = OpenDataBase(); //打开数据库
ClassRoot root = (ClassRoot)storage.Root; //指定接口的索引类型
Class class = new Class();
foreach (Class c in root.IdIndex.Select(typeof(Class),"ID = 1"))//参数:查询表的类型,查询的谓词
{
Response.Write(c.ID + "__" + c.Name);
class = c;
}
storage.Commit(); //执行操作,假如操作失败则恢复到操作之前的状态
storage.Close(); //关闭数据连接
return class;
}
{
Storage storage = OpenDataBase(); //打开数据库
ClassRoot root = (ClassRoot)storage.Root; //指定接口的索引类型
Class class = new Class();
foreach (Class c in root.IdIndex.Select(typeof(Class),"ID = 1"))//参数:查询表的类型,查询的谓词
{
Response.Write(c.ID + "__" + c.Name);
class = c;
}
storage.Commit(); //执行操作,假如操作失败则恢复到操作之前的状态
storage.Close(); //关闭数据连接
return class;
}
上面是Perst數據庫的一種方法,它是一個文件一個數據表的
下面再介紹一種方法,Database类,它是直接对数据库进行操作,而且一个数据文件里可以存放多张表
1.首先一样先打开数据库
using Perst;
public Database OpenDatabase()
{
Storage storage=StorageFactory.Instance.CreateStorage();
s.Open("数据文件路径");
Database db= new Database(storage); //创建操作数据库的对象
db.CreateTable(typeof(Class)); //创建class类型的表,成功返回TRUE,如果存在返回FALSE;
db.CreateIndex(typeof(Class),"ID",true); //创建索引参数:类型,对应字段,是否唯一
return db;
}
public Database OpenDatabase()
{
Storage storage=StorageFactory.Instance.CreateStorage();
s.Open("数据文件路径");
Database db= new Database(storage); //创建操作数据库的对象
db.CreateTable(typeof(Class)); //创建class类型的表,成功返回TRUE,如果存在返回FALSE;
db.CreateIndex(typeof(Class),"ID",true); //创建索引参数:类型,对应字段,是否唯一
return db;
}
Database类插入数据:
public void Insert()
{
Database db = OpenDatabase();
Class c = new Class();
c.ID = 1;
c.Name = "1";
db.AddRecord(typeof(Class),c); //插入操作 参数:插入表的类型,插入对象
db.Storage.Commit();
db.Storage.Close();
}
{
Database db = OpenDatabase();
Class c = new Class();
c.ID = 1;
c.Name = "1";
db.AddRecord(typeof(Class),c); //插入操作 参数:插入表的类型,插入对象
db.Storage.Commit();
db.Storage.Close();
}
Database类删除数据:
public void Delete()
{
Database db = OpenDatabase();
Class c= new Class(); //已知的对象
c.ID = 1;
c.Name = "1";
db.DeleteRecord(typeof(Class),c); //删除操作 参数:删除表的类型,删除对象
db.Storage.Commit();
db.Storage.Close();
}
{
Database db = OpenDatabase();
Class c= new Class(); //已知的对象
c.ID = 1;
c.Name = "1";
db.DeleteRecord(typeof(Class),c); //删除操作 参数:删除表的类型,删除对象
db.Storage.Commit();
db.Storage.Close();
}
Database类查询数据:
public void Select()
{
Database db = OpenDatabase();
Class class = new Class();
foreach (Class c in db.Select(typeof(Class),"ID =1");
{
class = c;
}
db.Storage.Commit();
db.Storage.Close();
}
{
Database db = OpenDatabase();
Class class = new Class();
foreach (Class c in db.Select(typeof(Class),"ID =1");
{
class = c;
}
db.Storage.Commit();
db.Storage.Close();
}
小結:
有一个Query类,这是跟查询有关的,应该还有一些让查询更丰富的方法。除了这些基本的,Perst还提供了事务,使用非常简单,只要把操作写在事务start和事务end之间就可以了。
小项目可以考虑使用perst,至少不用考虑数据库的部署和安全了。
下面是PERST自带的SAMPLE,一看就知道用法了
public class Supplier : Persistent
{
public string name;
public string location;
public Link shipments;
}
public class Detail : Persistent
{
public string id;
public float weight;
public Link shipments;
}
public class Shipment : Persistent
{
public Supplier supplier;
public Detail detail;
public int quantity;
public long price;
}
static void skip(string prompt)
{
Console.Write(prompt);
Console.ReadLine();
}
static String input(string prompt)
{
while (true)
{
Console.Write(prompt);
String line = Console.ReadLine().Trim();
if (line.Length != 0)
{
return line;
}
}
}
static long inputLong(string prompt)
{
while (true)
{
try
{
return Int32.Parse(input(prompt));
}
catch (FormatException)
{
Console.WriteLine("Invalid integer constant");
}
}
}
static double inputDouble(string prompt)
{
while (true)
{
try
{
return Double.Parse(input(prompt));
}
catch (FormatException)
{
Console.WriteLine("Invalid floating point constant");
}
}
}
static public void Main(String[] args)
{
Storage storage = StorageFactory.Instance.CreateStorage();
storage.Open("testssd2.dbs");
Database db = new Database(storage);
db.CreateTable(typeof(Supplier));
db.CreateIndex(typeof(Supplier), "name", true);
db.CreateTable(typeof(Detail));
db.CreateIndex(typeof(Detail), "id", true);
db.CreateTable(typeof(Shipment));
Query supplierQuery = db.Prepare(typeof(Supplier), "name like ?");
Query detailQuery = db.Prepare(typeof(Detail), "id like ?");
while (true)
{
try
{
switch ((int)inputLong("-------------------------------------\n" +
"Menu:\n" +
"1. Add supplier\n" +
"2. Add detail\n" +
"3. Add shipment\n" +
"4. List of suppliers\n" +
"5. List of details\n" +
"6. Suppliers of detail\n" +
"7. Details shipped by supplier\n" +
"8. Exit\n\n>>"))
{
case 1: //普通數據插入
{
Supplier supplier = new Supplier();
supplier.name = input("Supplier name: ");
supplier.location = input("Supplier location: ");
supplier.shipments = storage.CreateLink();
db.AddRecord(supplier);
storage.Commit();
continue;
}
case 2: //普通的數據插入
{
Detail detail = new Detail();
detail.id = input("Detail id: ");
detail.weight = (float)inputDouble("Detail weight: ");
detail.shipments = storage.CreateLink();
db.AddRecord(detail);
storage.Commit();
continue;
}
case 3: //以下演示了表之间的关联
{
Shipment shipment = null;
foreach (Supplier supplier in db.Select(typeof(Supplier), "name='" + input("Supplier name: ") + "'"))
{
foreach (Detail detail in db.Select(typeof(Detail), "id='" + input("Detail ID: ") + "'"))
{
shipment = new Shipment();
shipment.quantity = (int)inputLong("Shipment quantity: ");
shipment.price = inputLong("Shipment price: ");
shipment.detail = detail;
shipment.supplier = supplier;
detail.shipments.Add(shipment);
supplier.shipments.Add(shipment);
db.AddRecord(shipment);
storage.Commit();
}
}
if (shipment == null)
{
Console.WriteLine("Supplier+Detail not found");
}
continue;
}
case 4://普通的查询
foreach (Supplier supplier in db.GetRecords(typeof(Supplier)))
{
Console.WriteLine("Supplier name: " + supplier.name + ", supplier.location: " + supplier.location);
}
break;
case 5:
foreach (Detail detail in db.GetRecords(typeof(Detail)))
{
Console.WriteLine("Detail ID: " + detail.id + ", detail.weight: " + detail.weight);
}
break;
case 6://使用QUERY类查询
{
Hashtable result = new Hashtable();
detailQuery[1] = input("Detail ID: ");
foreach (Detail detail in detailQuery.Execute(db.GetRecords(typeof(Detail))))
{
foreach (Shipment shipment in detail.shipments)
{
result[shipment.supplier] = shipment;
}
}
foreach (Supplier supplier in result.Keys)
{
Console.WriteLine("Suppplier name: " + supplier.name);
}
break;
}
case 7:
{
Hashtable result = new Hashtable();
supplierQuery[1] = input("Supplier name: ");
foreach (Supplier supplier in supplierQuery.Execute(db.GetRecords(typeof(Supplier))))
{
foreach (Shipment shipment in supplier.shipments)
{
result[shipment.detail] = shipment;
}
}
foreach (Detail detail in result.Keys)
{
Console.WriteLine("Detail ID: " + detail.id);
}
break;
}
case 8:
storage.Close();
return;
}
skip("Press ENTER to continue");
}
catch (StorageError x)
{
Console.WriteLine("Error: " + x.Message);
skip("Press ENTER to continue");
}
}
}
{
public string name;
public string location;
public Link shipments;
}
public class Detail : Persistent
{
public string id;
public float weight;
public Link shipments;
}
public class Shipment : Persistent
{
public Supplier supplier;
public Detail detail;
public int quantity;
public long price;
}
static void skip(string prompt)
{
Console.Write(prompt);
Console.ReadLine();
}
static String input(string prompt)
{
while (true)
{
Console.Write(prompt);
String line = Console.ReadLine().Trim();
if (line.Length != 0)
{
return line;
}
}
}
static long inputLong(string prompt)
{
while (true)
{
try
{
return Int32.Parse(input(prompt));
}
catch (FormatException)
{
Console.WriteLine("Invalid integer constant");
}
}
}
static double inputDouble(string prompt)
{
while (true)
{
try
{
return Double.Parse(input(prompt));
}
catch (FormatException)
{
Console.WriteLine("Invalid floating point constant");
}
}
}
static public void Main(String[] args)
{
Storage storage = StorageFactory.Instance.CreateStorage();
storage.Open("testssd2.dbs");
Database db = new Database(storage);
db.CreateTable(typeof(Supplier));
db.CreateIndex(typeof(Supplier), "name", true);
db.CreateTable(typeof(Detail));
db.CreateIndex(typeof(Detail), "id", true);
db.CreateTable(typeof(Shipment));
Query supplierQuery = db.Prepare(typeof(Supplier), "name like ?");
Query detailQuery = db.Prepare(typeof(Detail), "id like ?");
while (true)
{
try
{
switch ((int)inputLong("-------------------------------------\n" +
"Menu:\n" +
"1. Add supplier\n" +
"2. Add detail\n" +
"3. Add shipment\n" +
"4. List of suppliers\n" +
"5. List of details\n" +
"6. Suppliers of detail\n" +
"7. Details shipped by supplier\n" +
"8. Exit\n\n>>"))
{
case 1: //普通數據插入
{
Supplier supplier = new Supplier();
supplier.name = input("Supplier name: ");
supplier.location = input("Supplier location: ");
supplier.shipments = storage.CreateLink();
db.AddRecord(supplier);
storage.Commit();
continue;
}
case 2: //普通的數據插入
{
Detail detail = new Detail();
detail.id = input("Detail id: ");
detail.weight = (float)inputDouble("Detail weight: ");
detail.shipments = storage.CreateLink();
db.AddRecord(detail);
storage.Commit();
continue;
}
case 3: //以下演示了表之间的关联
{
Shipment shipment = null;
foreach (Supplier supplier in db.Select(typeof(Supplier), "name='" + input("Supplier name: ") + "'"))
{
foreach (Detail detail in db.Select(typeof(Detail), "id='" + input("Detail ID: ") + "'"))
{
shipment = new Shipment();
shipment.quantity = (int)inputLong("Shipment quantity: ");
shipment.price = inputLong("Shipment price: ");
shipment.detail = detail;
shipment.supplier = supplier;
detail.shipments.Add(shipment);
supplier.shipments.Add(shipment);
db.AddRecord(shipment);
storage.Commit();
}
}
if (shipment == null)
{
Console.WriteLine("Supplier+Detail not found");
}
continue;
}
case 4://普通的查询
foreach (Supplier supplier in db.GetRecords(typeof(Supplier)))
{
Console.WriteLine("Supplier name: " + supplier.name + ", supplier.location: " + supplier.location);
}
break;
case 5:
foreach (Detail detail in db.GetRecords(typeof(Detail)))
{
Console.WriteLine("Detail ID: " + detail.id + ", detail.weight: " + detail.weight);
}
break;
case 6://使用QUERY类查询
{
Hashtable result = new Hashtable();
detailQuery[1] = input("Detail ID: ");
foreach (Detail detail in detailQuery.Execute(db.GetRecords(typeof(Detail))))
{
foreach (Shipment shipment in detail.shipments)
{
result[shipment.supplier] = shipment;
}
}
foreach (Supplier supplier in result.Keys)
{
Console.WriteLine("Suppplier name: " + supplier.name);
}
break;
}
case 7:
{
Hashtable result = new Hashtable();
supplierQuery[1] = input("Supplier name: ");
foreach (Supplier supplier in supplierQuery.Execute(db.GetRecords(typeof(Supplier))))
{
foreach (Shipment shipment in supplier.shipments)
{
result[shipment.detail] = shipment;
}
}
foreach (Detail detail in result.Keys)
{
Console.WriteLine("Detail ID: " + detail.id);
}
break;
}
case 8:
storage.Close();
return;
}
skip("Press ENTER to continue");
}
catch (StorageError x)
{
Console.WriteLine("Error: " + x.Message);
skip("Press ENTER to continue");
}
}
}