Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

安卓使用sqlite一点问题都没, 但是ios就是不行。 因为之前一直用的是mssql,所以都没仔细测试过sqlite #183

Closed
densen2014 opened this issue Jan 12, 2020 · 6 comments

Comments

@densen2014
Copy link
Member

安卓使用sqlite一点问题都没, 但是ios就是不行。 因为之前一直用的是mssql,所以都没仔细测试过sqlite。 难道当时调试没测试过ios的sqlite?以下是使用最新的源码以及以前你给我的app工程文件调试的。

主库数据库出错(执行SQL)〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
select 1 from main.sqlite_master where type='table' and name='Item'
【主库】状态不可用,等待后台检查程序恢复方可使用。Object reference not set to an instance of an object
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Exception: 【主库】状态不可用,等待后台检查程序恢复方可使用。Object reference not set to an instance of an object
at FreeSql.Internal.CommonProvider.CodeFirstProvider.SyncStructure (System.ValueTuple`2[System.Type,System.String][] objects) [0x00167] in :0
at FreeSql.Internal.CommonProvider.CodeFirstProvider.SyncStructure[TEntity] () [0x0001d] in :0
at App3.Services.MockDataStore..ctor () [0x00008] in /Users/alex/Desktop/testxmarin/App3/App3/Services/MockDataStore.cs:47
at (wrapper managed-to-native) System.Reflection.RuntimeConstructorInfo.InternalInvoke(System.Reflection.RuntimeConstructorInfo,object,object[],System.Exception&)
at System.Reflection.RuntimeConstructorInfo.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:936
--- End of inner exception stack trace ---
at System.Reflection.RuntimeConstructorInfo.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) [0x00018] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:944
at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic, System.Boolean wrapExceptions) [0x00095] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/ReferenceSources/RuntimeType.cs:185
at System.RuntimeType.CreateInstanceSlow (System.Boolean publicOnly, System.Boolean wrapExceptions, System.Boolean skipCheckThis, System.Boolean fillCache) [0x00009] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/ReferenceSources/RuntimeType.cs:155
at System.RuntimeType.CreateInstanceDefaultCtor (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Boolean wrapExceptions, System.Threading.StackCrawlMark& stackMark) [0x00027] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/rttype.cs:5770
at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic, System.Boolean wrapExceptions) [0x00039] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/activator.cs:206
at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/activator.cs:190
at System.Activator.CreateInstance (System.Type type) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/activator.cs:134
at Xamarin.Forms.DependencyService.Get[T] (Xamarin.Forms.DependencyFetchTarget fetchTarget) [0x00099] in D:\a\1\s\Xamarin.Forms.Core\DependencyService.cs:52
at App3.ViewModels.BaseViewModel.get_DataStore () [0x00000] in /Users/alex/Desktop/testxmarin/App3/App3/ViewModels/BaseViewModel.cs:15
at App3.ViewModels.ItemsViewModel.ExecuteLoadItemsCommand () [0x0004a] in /Users/alex/Desktop/testxmarin/App3/App3/ViewModels/ItemsViewModel.cs:42

1.使用
new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.Sqlite, connectionString) .UseAutoSyncStructure(true) .UseNoneCommandParameter(true) .UseMonitorCommand(cmd => Console.WriteLine(cmd.CommandText)) .Build());

报错

{System.NullReferenceException: Object reference not set to an instance of an object
at SQLitePCL.raw.sqlite3_open_v2 (SQLitePCL.utf8z filename, SQLitePCL.sqlite3& db, System.Int32 flags, SQLitePCL.utf8z vfs) [0x00000] in <15ecb38d58394d7b88b3f841a7dda078>:0
at SQLitePCL.raw.sqlite3_open_v2 (System.String filename, SQLitePCL.sqlite3& db, System.Int32 flags, System.String vfs) [0x0000e] in <15ecb38d58394d7b88b3f841a7dda078>:0
at Microsoft.Data.Sqlite.SqliteConnection.Open () [0x00122] in <99138fe6fbac4a1b964cb2ae266abca2>:0
at FreeSql.Sqlite.DbConnectionExtensions.OpenAndAttach (System.Data.Common.DbConnection that, System.String[] attach) [0x00001] in /Users/alex/Documents/GitHub/FreeSql/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteConnectionPool.cs:242
at FreeSql.Sqlite.SqliteConnectionPoolPolicy.OnGet (SafeObjectPool.Object`1[T] obj) [0x000ac] in /Users/alex/Documents/GitHub/FreeSql/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteConnectionPool.cs:151 }

2.和使用
new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.Sqlite, connectionString, typeof(FreeSql.Sqlite.SqliteProvider<>)) .UseAutoSyncStructure(true) .UseNoneCommandParameter(true) .UseMonitorCommand(cmd => Console.WriteLine(cmd.CommandText)) .Build());

报错

{System.NullReferenceException: Object reference not set to an instance of an object
at SQLitePCL.raw.sqlite3_open_v2 (SQLitePCL.utf8z filename, SQLitePCL.sqlite3& db, System.Int32 flags, SQLitePCL.utf8z vfs) [0x00000] in <15ecb38d58394d7b88b3f841a7dda078>:0
at SQLitePCL.raw.sqlite3_open_v2 (System.String filename, SQLitePCL.sqlite3& db, System.Int32 flags, System.String vfs) [0x0000e] in <15ecb38d58394d7b88b3f841a7dda078>:0
at Microsoft.Data.Sqlite.SqliteConnection.Open () [0x00122] in <99138fe6fbac4a1b964cb2ae266abca2>:0
at FreeSql.Sqlite.DbConnectionExtensions.OpenAndAttach (System.Data.Common.DbConnection that, System.String[] attach) [0x00001] in /Users/alex/Documents/GitHub/FreeSql/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteConnectionPool.cs:242
at FreeSql.Sqlite.SqliteConnectionPoolPolicy.OnGet (SafeObjectPool.Object`1[T] obj) [0x000ac] in /Users/alex/Documents/GitHub/FreeSql/Providers/FreeSql.Provider.Sqlite/SqliteAdo/SqliteConnectionPool.cs:151 }

@2881099
Copy link
Collaborator

2881099 commented Jan 12, 2020

System.NullReferenceException: Object reference not set to an instance of an object
at SQLitePCL.raw.sqlite3_open_v2 (SQLitePCL.utf8z filename, SQLitePCL.sqlite3& db, System.Int32 flags, SQLitePCL.utf8z vfs) [0x00000] in <15ecb38d58394d7b88b3f841a7dda078>:0
at SQLitePCL.raw.sqlite3_open_v2 (System.String filename, SQLitePCL.sqlite3& db, System.Int32 flags, System.String vfs) [0x0000e] in <15ecb38d58394d7b88b3f841a7dda078>:0
at Microsoft.Data.Sqlite.SqliteConnection.Open () [0x00122] in <99138fe6fbac4a1b964cb2ae266abca2>:0
at

手工使用 Microsoft.Data.Sqlite.SqliteConnection Open 试试会不会,Microsoft.Data.Sqlite 3.1.0

@densen2014
Copy link
Member Author

经测试,是连接字串问题

测试代码:

var dir = Path.Combine(Xamarin.Essentials.FileSystem.AppDataDirectory, "db111.db"); var dir4 = Path.Combine(Xamarin.Essentials.FileSystem.AppDataDirectory, "db222.db"); var connv3 = new Microsoft.Data.Sqlite.SqliteConnection($"Data Source={dir};"); connv3.Open(); var connv4 = new Microsoft.Data.Sqlite.SqliteConnection($"Data Source={dir4};version=3"); connv4.Open();

测试结果,db111.db成功建立。连接字串不包含version=3即可

@densen2014
Copy link
Member Author

真机还不能正确运行,微软这个反射的太混乱。暂时无结果

2881099 pushed a commit that referenced this issue Jan 19, 2020
2881099 pushed a commit that referenced this issue Jan 19, 2020
2881099 pushed a commit that referenced this issue Jan 19, 2020
@densen2014
Copy link
Member Author

使用1.1.0-preview6后,ios能正确使用sqlite了。但是以前能运行的 FreeSqlBuilder() 连接也必须加上类似 typeof(FreeSql.SqlServer.SqlServerProvider<>) , 否则会报错。

@2881099
Copy link
Collaborator

2881099 commented Jan 30, 2020

使用1.1.0-preview6后,ios能正确使用sqlite了。但是以前能运行的 FreeSqlBuilder() 连接也必须加上类似 typeof(FreeSql.SqlServer.SqlServerProvider<>) , 否则会报错。

这个是反射不到类型的表现,可能是项目包引用冲突

@densen2014
Copy link
Member Author

1.1.0-preview6 最终运行方案:

1.主工程接口
public interface ISQLite
{
DbConnection GetConnection2();
}

3.安卓和ios分别平台分别引用Mono.Data.Sqlite,System.Data,实现 DbConnection GetConnection()

public DbConnection GetConnection2()
{
string MicrosoftdbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Microsoft.Data.Sqlite.db");
var connv3 = new Mono.Data.Sqlite.SqliteConnection($"Data Source={MicrosoftdbPath};");
//connv3.Open();
return connv3;
}

4.主工程最终获取连接,建立IFreeSql

IFreeSql _fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionFactory(FreeSql.DataType.Sqlite, () => DependencyService.Get().GetConnection2(), typeof(FreeSql.Sqlite.SqliteProvider<>))
.UseAutoSyncStructure(true)
//.UseMonitorCommand(cmd => Console.WriteLine(cmd.CommandText))
.UseNoneCommandParameter(true)
.Build();

5.感谢叶老板无私帮助,奋战了xxx个日日夜夜,前几天工作忙,现在才总结一下写出来。愿跟我一样Xamarin开发者早日吃上freeSql这个全家桶,少走弯路。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants