0
点赞
收藏
分享

微信扫一扫

Sqlite

青乌 2022-01-26 阅读 60
sqlite

using App41.Models;
using SQLite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace App
{
    public class SQLiteHelper
    {
        public string connstr = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "User.db");
        public SQLiteConnection db;
        public SQLiteHelper()
        {
            db = new SQLiteConnection(connstr);
            db.CreateTable<Item>();
        }

        public int Add<T>(T model) where T : BaseModel
        {
            return db.Insert(model);
        }

        public int Update<T>(T model) where T : BaseModel
        {
            return db.Update(model);
        }

        public int Delete<T>(T model) where T : BaseModel
        {
            return db.Update(model);
        }
        public List<T> Query<T>() where T : BaseModel, new()
        {
            return db.Table<T>().ToList();
        }
        public T Query<T>(int id) where T : BaseModel, new()
        {
            return db.Table<T>().Where(x=>x.Id == id).FirstOrDefault();
        }
        public int Execute(string sql)
        {
            return db.Execute(sql);
        }
    }
    public class BaseModel
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
    }
}
 

举报

相关推荐

sqlite

sqlite基础

SQLite 简介

SQLite 事务

sqlite更新

SQLITE排序

0 条评论