【问题标题】:Xamarin SQLite SelectXamarin SQLite 选择
【发布时间】:2019-10-30 12:55:54
【问题描述】:

我在从 Android 设备版本 7.0 上的本地数据库中选择时遇到一个问题。 当我选择时,它返回System.Collections.Generic.List`1[Projectname.Database]

真的不知道问题出在哪里,我只需要在它的字符串中获取值。感谢任何人的帮助。代码:

barcode.TextChanged += delegate
        {             
            string bcode = ((EditText)barcode).Text.ToString();
            if(bcode != "")
            {
                string kod = bcode.Substring(bcode.IndexOf("Data:") + 5);

                try
                {                      
                    Console.WriteLine("**COUNT = " + db.Query<Prijem>("SELECT Count FROM Prijem WHERE BCode = ?", kod));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                db.Query<Prijem>("INSERT INTO Prijem(BCode,Name,FirmName,ItemCode,Count) values(" + kod + ", 'Test11', 'FirmText', 'XDD286', '2')");
                ((EditText)barcode).Text = "";
                dataGrid.View.Refresh();
            }
        };

数据库启动器:

public class Prijem
{
    [PrimaryKey, AutoIncrement]
    public string BCode { get; set; }
    public string Name { get; set; }
    public string FirmName { get; set; }
    public string ItemCode { get; set; }
    public string Count { get; set; }
}
public class GridDB
{
    static object locker = new object();
    SQLiteConnection db;
    public GridDB()
    {
        var dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "ISQLite.db");
        db = new SQLiteConnection(dbPath);

        db.CreateTable<Prijem>();
    }

    public IEnumerable<Prijem> GetItems()
    {
        lock (locker)
        {
            var table = (from i in db.Table<Prijem>() select i);
            return table;
        }
    }
}

【问题讨论】:

  • 选择总是会返回一组行。如果您只想要一个结果,您可以使用集合中的第一个结果
  • 但是怎么做呢?当我使用 FirstOrDefault() 它返回 Projectname.Database
  • 尝试使用Query&lt;int&gt;
  • 如果我更改 WHERE 参数或选择参数,也总是打印 0
  • 您确定您的查询语法正确吗?

标签: android sqlite xamarin


【解决方案1】:

我给你写了一个关于 xamarin 表单中的 sqlite 的演示。 这里正在运行 GIF。

首先,你应该改变主键的类型,你应该像下面Prijem那样把它从string改为int

   public class Prijem
{
    [PrimaryKey, AutoIncrement, Unique]
    public int BCode { get; set; }
    public string Name { get; set; }
    public string FirmName { get; set; }
    public string ItemCode { get; set; }
    public string Count { get; set; }
}

然后,这里是关于 CRUD 类的操作。

   public  class PrijemDatabase
{
    readonly SQLiteAsyncConnection _database;

    public PrijemDatabase(string dbPath)
    {
        _database = new SQLiteAsyncConnection(dbPath);
        _database.CreateTableAsync<Prijem>().Wait();
    }

    public Task<List<Prijem>> GetAllPrijemAsync()
    {
        return _database.Table<Prijem>().ToListAsync();
    }

    public Task<Prijem> GetPrijemByNameAsync(string Name)
    {
        return _database.Table<Prijem>()
                        .Where(i => i.Name == Name)
                        .FirstOrDefaultAsync();
    }

    public Task<int> SavePrijemAsync(Prijem prijem)
    {
        if (prijem.BCode != 0)
        {
            return _database.UpdateAsync(prijem);
        }
        else
        {
            return _database.InsertAsync(prijem);
        }
    }

    public Task<int> DeletePrijemAsync(Prijem note)
    {
        return _database.DeleteAsync(note);
    }
}

这是 App.xaml.cs 代码。

 public partial class App : Application
{

    static PrijemDatabase pdatabase;

    public static PrijemDatabase Pdatabase
    {
        get
        {
            if (pdatabase == null)
            {
                pdatabase = new PrijemDatabase(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Prijem2.db3"));
            }
            return pdatabase;
        }
    }




    public App()
    {
        InitializeComponent();
        MainPage = new NavigationPage(new PrijemPage());
    }
 }

这里是PrijemPage.xaml

       <ListView x:Name="listView"
          Margin="20"
           ItemSelected="ListView_ItemSelected"
          >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        <Label Text="{Binding Name}"></Label>

                        <Label Text="{Binding FirmName}"></Label>
                        <Label Text="{Binding ItemCode}"></Label>

                        <Label Text="{Binding Count}"></Label>
                    </StackLayout>

                </ViewCell>


            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

这里是PrijemPage.xaml.cs

  public partial class PrijemPage : ContentPage
{
    public PrijemPage()
    {
        InitializeComponent();
        InsertData();

    }

    private async void InsertData()
    {

        List<Prijem> PrijemLIST = new List<Prijem>();
        PrijemLIST.Add(new Prijem() { Name = "Leon", FirmName = "11Fame", ItemCode = "NE", Count = "11" });
        PrijemLIST.Add(new Prijem() { Name = "Jame", FirmName = "22Fame", ItemCode = "Daw", Count = "12" });
        PrijemLIST.Add(new Prijem() { Name = "Leborn", FirmName = "33Fame", ItemCode = "Caow", Count = "13" });
        PrijemLIST.Add(new Prijem() { Name = "Rebeeca", FirmName = "44fame", ItemCode = "DacNow", Count = "14" });

        //Insert data
        foreach (var item in PrijemLIST)
        {
            await App.Pdatabase.SavePrijemAsync(item);
        }

        //GetAllData
        listView.ItemsSource = await App.Pdatabase.GetAllPrijemAsync();
    }


    private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        var myPrijem = (Prijem)e.SelectedItem;
        Navigation.PushAsync(new Page1(myPrijem));
    }
}

这里是Page1.xaml

    <StackLayout>
        <Label x:Name="MyLabel"
            VerticalOptions="CenterAndExpand" 
            HorizontalOptions="CenterAndExpand" />
    </StackLayout>

这里是Page1.xaml.cs

  public partial class Page1 : ContentPage
{
    public Page1 (Prijem myPrijem)
    {
        InitializeComponent ();

        GetResult(myPrijem);




    }

    private async void GetResult(Prijem myPrijem)
    {
        // Operate a query

        Prijem  prijem= await  App.Pdatabase.GetPrijemByNameAsync(myPrijem.Name);

        MyLabel.Text= "BCode: "+prijem.BCode+ "  Name: "+ myPrijem.Name + "   ItemCode: " + prijem.ItemCode ;
    }
}

我更新了我的演示。你可以参考一下。 https://github.com/851265601/prijemDemo

【讨论】:

  • 非常感谢 :) 正如我所看到的,您也在使用 iOS 和 android 项目。比只有安卓好?
猜你喜欢
  • 2019-02-05
  • 2011-01-02
  • 2021-03-25
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-15
  • 1970-01-01
相关资源
最近更新 更多