Xamarin.forms 在何处创建SQLite DB以及如何在任何页面中创建Select语句

Xamarin.forms 在何处创建SQLite DB以及如何在任何页面中创建Select语句,xamarin.forms,sqlite-net,Xamarin.forms,Sqlite Net,xamarin与SQLite的新形式。我需要一些关于如何在Xamarin表单中使用SQLite的指导。下面是代码 1) Create Interface using System; using SQLite.Net; namespace SQLiteSample { public interface ISQLite { SQLiteConnection GetConnection(); } } 2) Implementing ISQLite in

xamarin与SQLite的新形式。我需要一些关于如何在Xamarin表单中使用SQLite的指导。下面是代码

1) Create Interface 

using System;
using SQLite.Net;

namespace SQLiteSample
{
    public interface ISQLite
    {
        SQLiteConnection GetConnection();
    }
}


2) Implementing ISQLite interface

using System;
using Xamarin.Forms;
using SQLiteEx.Droid;
using System.IO;

[assembly: Dependency(typeof(SqliteService))]
namespace SQLiteEx.Droid
{
    public class SqliteService : ISQLite
    {
        public SqliteService() { }

        public SQLite.Net.SQLiteConnection GetConnection()
        {
            var sqliteFilename = "myDB.db3";
            // Documents folder
            string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
            var path = Path.Combine(documentsPath, sqliteFilename);

            Console.WriteLine(path);

            if (!File.Exists(path)) File.Create(path);
            var plat = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
            var conn = new SQLite.Net.SQLiteConnection(plat, path);
            // Return the database connection 
            return conn;
        }

    }
}


3) Class for Database Operation :CRUD

using SQLite.Net;
using Xamarin.Forms;

namespace SQLiteEx
{
    public class DataAccess
    {
        SQLiteConnection dbConn;
        public DataAccess()
        {
            dbConn = DependencyService.Get<ISQLite>().GetConnection();
            // create the table(s)
            dbConn.CreateTable<Employee>();
        }

        public List<Employee> GetAllEmployees()
        {
            return dbConn.Query<Employee>("Select * From [Employee]");
        }

        public int SaveEmployee(Employee aEmployee)
        {
            return dbConn.Insert(aEmployee);            
        }

        public int DeleteEmployee(Employee aEmployee)
        {
            return dbConn.Delete(aEmployee);
        }

        public int EditEmployee(Employee aEmployee)
        {
            return dbConn.Update(aEmployee);
        }
    }
}
1)创建接口
使用制度;
使用SQLite.Net;
名称空间SQLiteSample
{
公共接口ISQLite
{
SQLiteConnection GetConnection();
}
}
2) 实现ISQLite接口
使用制度;
使用Xamarin.Forms;
使用SQLiteEx.Droid;
使用System.IO;
[程序集:依赖项(typeof(SqliteService))]
名称空间SQLiteEx.Droid
{
公共类SqliteService:ISQLite
{
公共SqliteService(){}
public SQLite.Net.SQLiteConnection GetConnection()
{
var sqliteFilename=“myDB.db3”;
//文档文件夹
字符串documentsPath=Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var path=path.Combine(documentsPath,sqliteFilename);
控制台写入线(路径);
如果(!File.Exists(path))File.Create(path);
var plat=new SQLite.Net.Platform.xamariandroid.SQLitePlatformAndroid();
var conn=new SQLite.Net.SQLiteConnection(plat,path);
//返回数据库连接
返回连接;
}
}
}
3) 用于数据库操作的类:CRUD
使用SQLite.Net;
使用Xamarin.Forms;
名称空间SQLiteEx
{
公共类数据访问
{
sqliteconnectiondbconn;
公共数据访问()
{
dbConn=DependencyService.GetISQLite().GetConnection();
//创建表
dbConn.CreateTable();
}
公共列表GetAllEmployees()
{
返回dbConn.Query(“从[Employee]中选择*”;
}
public int SaveEmployee(员工aEmployee)
{
返回数据库连接插入(aEmployee);
}
public int DeleteEmployee(员工aEmployee)
{
返回dbConn.Delete(aEmployee);
}
公共雇员(雇员)
{
返回数据库连接更新(aEmployee);
}
}
}
我想知道:

1) 在哪里可以创建一个可以在整个应用程序中使用的数据库。这意味着我可以在任何页面的任何位置使用它,而无需在每次需要时重新创建它

2) 在上述代码中,是否每次都会重新创建表

3) 如何在任何页面(如CustomerPage.xaml或SalesPage.xaml)中执行select语句

在WinRT中,我使用了以下代码。如何以Xamarin的形式进行?我需要重新创建SQLite数据库吗?如何获得路径

Using (var db = new SQLite.SQLiteConnection(App.DBPath){

  var query = db.query<CashReceivable>("select * from CashRcvdTbl where Cust='" + Id + "'";

   foreach( var item in query)
  {

   }

}
使用(var db=new SQLite.SQLiteConnection(App.DBPath){
var query=db.query(“从CashRcvdTbl中选择*,其中Cust='”+Id+“”;
foreach(查询中的var项)
{
}
}
1)这里

string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
您的
ISQlite
实现中的这一行指向您的
myDB.db3 SQLite
文件。 您可以在
/data/data/@APP\u PACKAGE\u NAME@/files
目录中查看此文件

if (!File.Exists(path)) File.Create(path);
此行检查上述路径中是否存在数据库文件。如果不存在,则创建此文件一次。这次它将是一个空的sqlite文件,其中没有任何表。 因此,它不会每次都重新创建

但是,我认为不需要这个文件存在检查,因为当您创建到SQLite文件的连接时,它会自动创建不存在的文件

因此,它可以简单如下:

public class SqliteService : ISQLite
{         
    string dpPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), “myDB.db3”);
    public SQLiteConnection GetConnection()
    {
        return new SQLiteConnection(new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid(), dpPath, false);
    }
}
public List<Employee> GetAllEmployees()
{
    using (var conn = DependencyService.Get<ISQLite>().GetConnection())
    {                 
        return conn.Table<Employee>().ToList(); 
        //OR
        return conn.Query<Employee>("Select * From Employee");            
    }         
}
2)每次创建
DataAccess
类的实例时,它都会根据表是否存在重新创建
Employee
表/定义,正如它调用
dbConn.CreateTable()
我通常在应用程序启动代码时写这行代码

public App () 
{ 
    InitializeComponent (); 

    //Create all tables here. This will create them once when app launches.
    using (var conn = DependencyService.Get<ISQLite>().GetConnection())             
    {     
        conn.CreateTable<Employee>();
    }
    MainPage = new HomePage ();
}

在任何页面中,您都可以调用此
GetAllEmployees
方法来检索
员工列表

有人可以在这方面提供帮助吗?