【问题标题】:How to get values from SQLite with the matching ID in Xamarin如何在 Xamarin 中从 SQLite 获取具有匹配 ID 的值
【发布时间】:2017-08-14 11:31:40
【问题描述】:

我正在使用 Xamarin,我的代码是:

    DatabaseUpdates updatedb = new DatabaseUpdates();
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.ProjectListViewPage);

        updatedb.SetContext(this);

        //db = new ProjectDatabaseFunctions();
        // db.CreatDataBaseProject();
        string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        lstData = FindViewById<ListView>(Resource.Id.listView);

        Log.Info("DB_Path", folder);
        LoadData();



        lstData.ItemClick +=  (s, e) =>
        {
            var ID =  lstSource.Where(xc => xc.ID == e.Id).Select(cx => cx.ID);
            FragmentTransaction transation = FragmentManager.BeginTransaction();
            listMenuProject list = new listMenuProject(ID.ToString());
            list.Show(transation, "dialog fragment");
        };

    }
    private void LoadData()
    {
        lstSource = updatedb.selectTableProject();
        var adapter = new ListViewAdapterProject(this, lstSource);
        lstData.Adapter = adapter;
    } 

这是我从列表中单击的内容中获取 ID 的地方。有了这个 ID,我需要从该 ID 中获取所有值,因此我编辑该数据,并且还需要创建一个具有相同列表的列表。

目前我写的代码是。

 public class DatabaseUpdates
{
    private ProjectDatabase _helper;
    public void SetContext(Context context)
    {
        if (context != null)
        {
            _helper = new ProjectDatabase(context);
        }

    }
    public bool insertIntoTableProject(Project proj)
    {
        try
        {
            using (var connection = new SQLiteConnection(_helper.WritableDatabase.Path))
            {
                connection.Insert(proj);
                return true;
            }
        }
        catch (SQLiteException ex)
        {
            Log.Info("SQLiteEx", ex.Message);
            return false;
        }
    }
    public List<Project> selectTableProject()
    {
        try
        {
            using (var connection = new SQLiteConnection(_helper.ReadableDatabase.Path))
            {
                return connection.Table<Project>().ToList();
            }
        }
        catch (SQLiteException ex)
        {
            Log.Info("SQLiteEx", ex.Message);
            return null;
        }
    }
    public bool deleteTableProject(int proj)
    {
        try
        {
            using (var connection = new SQLiteConnection(_helper.WritableDatabase.Path))
            {
                connection.Delete(proj);
                return true;
            }
        }
        catch (SQLiteException ex)
        {
            Log.Info("SQLiteEx", ex.Message);
            return false;
        }
    }

【问题讨论】:

    标签: c# sqlite xamarin android-sqlite


    【解决方案1】:

    假设您使用的是 sqlite-net 或 sqlite-net-pcl,则可以使用 LINQ 查询表!

    这里有一些你可能会用到的东西:

    var project = connection.Table<Project>().FirstOrDefault(p => p.Id == yourId);
    

    你也可以通过同样的方式过滤得到一个集合:

    var projects = connection.Table<Project>().Where(p => p.SomeProperty == someValue);
    

    【讨论】:

    • 是的,我正在使用 sqlite-net,谢谢:D
    猜你喜欢
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多