Xamarin Forms应用程序抛出"SQLite.SQLiteException:无法打开数据库文件" [英] Xamarin Forms app throws 'SQLite.SQLiteException: Could not open database file'

查看:111
本文介绍了Xamarin Forms应用程序抛出"SQLite.SQLiteException:无法打开数据库文件"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xamarin Forms应用程序在单击导航按钮"期间抛出以下错误. 如果我在SoccerDailyStatus类中评论以下方法DisplaySoccerStatus();,则该应用程序将运行.因此,我认为数据库中很少有记录为空会导致此问题?不确定,如何解决该问题?

Xamarin Forms app throws following error during on click on Navigation Button. If I comment the following method DisplaySoccerStatus(); in the SoccerDailyStatus class then the app will work. So I assume, few records in the database which are null is causing the issue ? Not sure, how can I resolve the problem ?

SQLite.SQLiteException: Could not open database file: /data/user/0/com.companyname.soccerapp/files/.config/soccerpep (CannotOpen

 public partial class SoccerDailyStatus : ContentPage
{
    private SQLiteConnection conn;
    SoccerAvailability status; 

    public SoccerDailyStatus(SoccerAvailability soccerStatus)
    {
        InitializeComponent();
        status = soccerStatus;
        BindingContext = status;
       //DisplaySoccerStatus();
    }

    protected override async void OnAppearing()
    {
        conn = DependencyService.Get<Isqlite>().GetConnection();
        conn.CreateTable<SoccerAvailability>();
        base.OnAppearing();

        async Task DisplaySoccerStatus()
        {
            var datetoday = DateTime.Now.ToString("ddMMyyyy");
            //List<SoccerAvailability> myList = (from x in conn.Table<SoccerAvailability>() select x).ToList();
            List<SoccerAvailability> myList = (from x in conn.Table<SoccerAvailability>().Where(x => x.CurrentDate == datetoday) select x).ToList();
            if (myList != null)
            {
                SoccerAvailability soccerAvailability = new SoccerAvailability();
                soccerAvailability.SoccerStatus = myList[0].SoccerStatus;
                soccerAvailability.CurrentDate = DateTime.Now.ToString("ddMMyyyy");
                await Navigation.PushAsync(new SoccerDailyStatus(soccerAvailability) { });
            }
            else
            {
                await DisplayAlert("Notification", "Unable to proccess status", "Cancel");
            }

        }

        await DisplaySoccerStatus();
    }


}

}

堆栈跟踪:

05-04 13:51:12.462 D/Mono    (23709): DllImport attempting to load: '__Internal'.
05-04 13:51:12.462 D/Mono    (23709): DllImport loaded library '(null)'.
05-04 13:51:12.462 D/Mono    (23709): DllImport searching in: '__Internal' ('(null)').
05-04 13:51:12.462 D/Mono    (23709): Searching for 'monodroid_get_system_property'.
05-04 13:51:12.462 D/Mono    (23709): Probing 'monodroid_get_system_property'.
05-04 13:51:12.462 D/Mono    (23709): Found as 'monodroid_get_system_property'.
05-04 13:51:12.462 D/Mono    (23709): DllImport searching in: '__Internal' ('(null)').
05-04 13:51:12.462 D/Mono    (23709): Searching for 'monodroid_free'.
05-04 13:51:12.462 D/Mono    (23709): Probing 'monodroid_free'.
05-04 13:51:12.462 D/Mono    (23709): Found as 'monodroid_free'.
05-04 13:51:12.904 D/Mono    (23709): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so').
05-04 13:51:12.904 D/Mono    (23709): Searching for 'sqlite3_changes'.
05-04 13:51:13.123 I/art     (23709): Starting a blocking GC Explicit
05-04 13:51:13.132 I/art     (23709): Explicit concurrent mark sweep GC freed 206(21KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 3MB/5MB, paused 651us total 8.285ms
05-04 13:51:13.134 D/Mono    (23709): GC_TAR_BRIDGE bridges 87 objects 901 opaque 262 colors 87 colors-bridged 87 colors-visible 87 xref 0 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.03ms tarjan 0.19ms scc-setup 0.04ms gather-xref 0.02ms xref-setup 0.02ms cleanup 0.74ms
05-04 13:51:13.134 D/Mono    (23709): GC_BRIDGE: Complete, was running for 11.21ms
05-04 13:51:13.135 D/Mono    (23709): GC_MINOR: (Nursery full) time 7.23ms, stw 7.85ms promoted 1963K major size: 2848K in use: 2132K los size: 1024K in use: 55K
05-04 13:51:13.306 D/Mono    (23709): GC_TAR_BRIDGE bridges 0 objects 0 opaque 0 colors 0 colors-bridged 0 colors-visible 87 xref 0 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.03ms tarjan 0.19ms scc-setup 0.04ms gather-xref 0.02ms xref-setup 0.02ms cleanup 0.02ms
05-04 13:51:13.306 D/Mono    (23709): GC_BRIDGE: Complete, was running for 0.15ms
05-04 13:51:13.307 D/Mono    (23709): GC_MINOR: (Nursery full) time 5.49ms, stw 5.88ms promoted 2025K major size: 4992K in use: 4209K los size: 1024K in use: 55K
05-04 13:51:13.468 D/Mono    (23709): GC_TAR_BRIDGE bridges 0 objects 0 opaque 0 colors 0 colors-bridged 0 colors-visible 87 xref 0 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.03ms tarjan 0.19ms scc-setup 0.04ms gather-xref 0.02ms xref-setup 0.02ms cleanup 0.03ms
05-04 13:51:13.468 D/Mono    (23709): GC_BRIDGE: Complete, was running for 0.26ms
05-04 13:51:13.468 D/Mono    (23709): GC_MINOR: (Nursery full) time 6.25ms, stw 6.64ms promoted 1963K major size: 7024K in use: 6222K los size: 1024K in use: 55K
05-04 13:51:13.629 D/Mono    (23709): GC_TAR_BRIDGE bridges 0 objects 0 opaque 0 colors 0 colors-bridged 0 colors-visible 87 xref 0 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.03ms tarjan 0.19ms scc-setup 0.04ms gather-xref 0.02ms xref-setup 0.02ms cleanup 0.03ms
05-04 13:51:13.629 D/Mono    (23709): GC_BRIDGE: Complete, was running for 0.19ms
05-04 13:51:13.629 D/Mono    (23709): GC_MINOR: (Nursery full) time 7.41ms, stw 7.90ms promoted 1886K major size: 9024K in use: 8157K los size: 1024K in use: 55K
05-04 13:51:13.817 D/Mono    (23709): GC_TAR_BRIDGE bridges 0 objects 0 opaque 0 colors 0 colors-bridged 0 colors-visible 87 xref 0 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.03ms tarjan 0.19ms scc-setup 0.04ms gather-xref 0.02ms xref-setup 0.02ms cleanup 0.03ms
05-04 13:51:13.817 D/Mono    (23709): GC_BRIDGE: Complete, was running for 0.19ms
05-04 13:51:13.817 D/Mono    (23709): GC_MINOR: (Nursery full) time 9.31ms, stw 9.72ms promoted 1818K major size: 10896K in use: 10021K los size: 1024K in use: 55K
05-04 13:51:14.140 I/art     (23709): Starting a blocking GC Explicit
05-04 13:51:14.147 I/art     (23709): Explicit concurrent mark sweep GC freed 156(9KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 3MB/5MB, paused 599us total 6.464ms
05-04 13:51:14.147 D/Mono    (23709): GC_TAR_BRIDGE bridges 29 objects 29 opaque 0 colors 29 colors-bridged 29 colors-visible 29 xref 0 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.04ms tarjan 0.03ms scc-setup 0.03ms gather-xref 0.02ms xref-setup 0.02ms cleanup 0.27ms
05-04 13:51:14.147 D/Mono    (23709): GC_BRIDGE: Complete, was running for 7.41ms
05-04 13:51:14.147 D/Mono    (23709): GC_MINOR: (Nursery full) time 7.24ms, stw 7.62ms promoted 501K major size: 11472K in use: 10539K los size: 1024K in use: 55K
05-04 13:51:14.157 D/Mono    (23709): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so').
05-04 13:51:14.158 D/Mono    (23709): Searching for 'sqlite3_close_v2'.
05-04 13:51:14.651 I/Choreographer(23709): Skipped 167 frames!  The application may be doing too much work on its main thread.
05-04 13:51:14.688 E/EGL_emulation(23709): tid 23733: swapBuffers(531): error 0x300d (EGL_BAD_SURFACE)
05-04 13:51:14.688 W/OpenGLRenderer(23709): swapBuffers encountered EGL_BAD_SURFACE on 0x9c46bd40, halting rendering...
05-04 13:51:14.690 D/EGL_emulation(23709): eglMakeCurrent: 0xb077de80: ver 3 1 (tinfo 0x9c459e20)
Unhandled Exception:

SQLite.SQLiteException: Could not open database file: /data/user/0/com.companyname.soccerapp/files/.config/soccerpep (CannotOpen)

推荐答案

我认为问题是由于ctor中调用的async方法DisplaySoccerStatus引起的. 单个数据连接不能同时在两个或多个线程中使用.

I think the problem is due to the async method DisplaySoccerStatus called in ctor. A single data connection cannot be used simultaneously in two or more threads.

我建议两件事:

  1. async onAppearing中呼叫您的DisplaySoccerStatus,然后使用await DisplaySoccerStatus.哦,请使用async Task DisplaySoccerStatus而不是async void,这样您就可以知道任务何时完成.
  1. Call your DisplaySoccerStatus in async onAppearing and use await DisplaySoccerStatus. Oh, and use async Task DisplaySoccerStatus and not async void so you can know when task is finished.

异步无效的情况是生硬而忘了":您启动了任务链,但并不关心它何时完成.当函数返回时,您所知道的就是直到第一次等待的所有事情都已执行.第一次等待后的所有内容都将在将来无法访问的某个不确定时间运行.

The async void case is a "fire and forget": You start the task chain, but you don't care about when it's finished. When the function returns, all you know is that everything up to the first await has executed. Everything after the first await will run at some unspecified point in the future that you have no access to.

  1. 按照官方文档

这篇关于Xamarin Forms应用程序抛出"SQLite.SQLiteException:无法打开数据库文件"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆