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