xamarin SQLite路径

xamarin使用SQLite时对应的访问路径各个平台不一样,需要单独引用。在使用前添加SQLite引用包,在解决方案上右击选择解决方案的Nuget管理选项,在浏览中输入sqlite-net-pcl,选择sqlite-net-pcl这个安装好就可以了,请认准安装包,不要装错了,有很多类似的包,但是不是同一个人写的。

下面各平台罗列代码:

Windows8.1:

public SQLiteConnection GetConnection(string dbName) {
            var sqliteFilename = string.Format("{0}.db3", dbName);
            string documentsPath = global::Windows.Storage.ApplicationData.Current.LocalFolder.Path; 
            
            var path = Path.Combine(documentsPath, sqliteFilename);
            return new SQLite.SQLiteConnection(path);
        }

在windows Phone下注意要添加一个引用:引用>>添加引用>>windows phones8.1>>扩展>>Microsoft Visual C++Runting Time Packge for windows Phone

UWP:

public SQLiteConnection GetConnection(string dbName) {
            var sqliteFilename = string.Format("{0}.db3", dbName);
            string documentsPath = global::Windows.Storage.ApplicationData.Current.LocalFolder.Path; 
            
            var path = Path.Combine(documentsPath, sqliteFilename);
            return new SQLite.SQLiteConnection(path);
        }

Android:

public SQLite.SQLiteConnection GetConnection(string dbName)
        {
            var sqliteFilename = string.Format("{0}.db3", dbName);
            string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); 
            var path = Path.Combine(documentsPath, sqliteFilename);
            return new SQLite.SQLiteConnection(path);
        }

IOS:

public SQLite.SQLiteConnection GetConnection(string dbName)
        {
            var sqliteFilename = string.Format("{0}.db3", dbName);
            string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // Documents folder
            string libraryPath = Path.Combine(documentsPath, "..", "Library"); // Library folder
            var path = Path.Combine(libraryPath, sqliteFilename);
           return new SQLite.SQLiteConnection(path);
        }

 在使用的时候也要注意下,下面罗列一下。

案例一,在我们使用new SQLiteConnection().Insert(new model)时注意,这里的Insert方法虽然可以输入LIst<T>类型,但是掉用的时候会报错,如uwp上报Parameter count mismatch.等,无法插入数据,记得这个方法只能插入一条数据,不能批量操作,楼主在这里栽了跟头。

希望以上东西对于你写xamarin有帮助。欢迎交流。

转载于:https://www.cnblogs.com/zuimengaitianya/p/5752535.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值