Xamarin形式的sqlite多表

时间:2019-03-29 16:09:17

标签: sqlite xamarin.forms

不能使用多个表。如何处理两个,三个或更多表?

Visual Studio >>> Xamarin-Forms

/// I think maybe this code needs to be fixed somehow.
/// this code is placed in App.cs (up)
static Data.TableTwo tabletwo;
static Data.TableOne tableone;
/// crashed
 public Task<int> SaveItemAsync(TableTwo item)
        {
            if (item.ID != 0)
            {
                return tabletwo.UpdateAsync(item);
            }
            else
            {
                return tabletwo.InsertAsync(item);
            }
        } 
/// ***I think maybe this code needs to be fixed somehow.
/// this code is placed in App.cs (down below)
public static Data.TableTwo tabletwo
        {
            get
            {
                if (datadistance == null)
                {
                    tabletwo = new Data.TableTwo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TodoSQLite.db3"));
                }
                return tabletwo;
            }
        }
/// ***I think maybe this code needs to be fixed somehow.
/// this code is placed in App.cs (down below)
        public static Data.TableOne tableone
        {
            get
            {
                if (tableone == null)
                {
                    tableone = new Data.TableOne(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TodoSQLite.db3"));
                }
                return tableone;
            }
        }

以上代码正常工作。当上面的代码被调用时。申请失败。

我有两个桌子。用户工作时使用一个表(保存和删除数据),则一切正常。如果用户开始使用另一个表(保存数据),则应用程序崩溃。

!!!应用程序树! 数据库(文件夹)    TableTwo(文件)    TableOne(文件) 型号(文件夹)    TableTwo(文件)    TableOne(文件)

一切都根据文章https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/databases#using-sqlite

中的代码完成

实际上,我只是第二次复制了代码,然后将其粘贴到项目中。 -这就是我创建第二张表并使用它(删除插入数据)完成的操作

1 个答案:

答案 0 :(得分:0)

假设您的两个表的类型为 Load Category ,而您的数据库的类型为 MyDatabase 您可能希望在 MyDatabase 类内与SqlLite保持单一连接,并添加与表进行交互的方法,如下所示:

public class MyDatabase
{
    private readonly SQLiteAsyncConnection _connection;

    public MyDatabase():this(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MyDatabase.db3"))
    {
    }

    internal MyDatabase(string dbPath)
    {
        _connection = new SQLiteAsyncConnection(dbPath);

        _connection.CreateTableAsync<Load>().Wait();
        _connection.CreateTableAsync<Category>().Wait();
    }

    public Task<List<Load>> GetLoads() => 
        _connection.Table<Load>().ToListAsync();

    public Task<List<Category>> GetCategories() => 
        _connection.Table<Category>().ToListAsync();

    public Task<Load> GetLoad(int id) => 
        _connection.Table<Load>().Where(i => i.Id == id).FirstOrDefaultAsync();

    public Task<int> SaveLoad(Load item) =>
        item.Id != 0 ? _connection.UpdateAsync(item) : _connection.InsertAsync(item);

    public Task<int> DeleteLoad(Load item) => 
        _connection.DeleteAsync(item);
}

以下是一个很好的示例:https://github.com/xamarin/xamarin-forms-samples/blob/master/Todo/Todo/Data/TodoItemDatabase.cs,但其中包含一个表