Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin SQLite数据库检查_C#_Xamarin_Xamarin.forms_Sqlite - Fatal编程技术网

C# Xamarin SQLite数据库检查

C# Xamarin SQLite数据库检查,c#,xamarin,xamarin.forms,sqlite,C#,Xamarin,Xamarin.forms,Sqlite,我是Xamarin的新手。我想确认数据库是否已创建,以及数据是否正在插入SQLite数据库。谢谢你的帮助 问题: 1.如何检查数据库是否存在/创建? 2.如何检查用户插入成功还是失败? 3.这些文件在我的手机中放在哪里? 下面是我的代码: App.xaml.cs using System; using Xamarin.Forms; using Xamarin.Forms.Xaml; using TBSMobileApplication.Views; using TBSMobileApplicat

我是Xamarin的新手。我想确认数据库是否已创建,以及数据是否正在插入SQLite数据库。谢谢你的帮助

问题:
1.如何检查数据库是否存在/创建?
2.如何检查用户插入成功还是失败?
3.这些文件在我的手机中放在哪里?

下面是我的代码:

App.xaml.cs

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using TBSMobileApplication.Views;
using TBSMobileApplication.Data;

[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
namespace TBSMobileApplication
{
    public partial class App : Application
    {
        static TokenDatabaseController tokenDatabase;
        static UserDatabaseController userDatabase;

        public App ()
        {
            InitializeComponent();

            MainPage = new LoginPage();
        }

        protected override void OnStart ()
        {
            // Handle when your app starts
        }

        protected override void OnSleep ()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume ()
        {
            // Handle when your app resumes
        }

        public static UserDatabaseController UserDatabase
        {
             get
            {
                if(userDatabase == null)
                {
                    userDatabase = new UserDatabaseController();
                }

                return userDatabase;
            }
        }

        public static TokenDatabaseController TokenDatabase
        {
            get
            {
                if (tokenDatabase == null)
                {
                    tokenDatabase = new TokenDatabaseController();
                }

                return tokenDatabase;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TBSMobileApplication.Models;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace TBSMobileApplication.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LoginPage : ContentPage
    {
        public LoginPage ()
        {
            InitializeComponent ();
        }

        void LoginProcedure(object sender, EventArgs e)
        {
            User user = new User(entUser.Text, entPassword.Text);
            if (user.CheckInformation())
            {
                //DisplayAlert("Login Message", "Login Success", "Ok");
                try
                {
                    App.UserDatabase.SaveUser(user);
                    DisplayAlert("Database Message", "User Saved", "Ok");
                }
                catch(Exception ex)
                {
                    DisplayAlert("Message", ex.Message, "Ok");
                }

            }
            else
            {
                DisplayAlert("Login Message", "Login Failed", "Ok");
            }
        }
    }
}
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;

namespace TBSMobileApplication.Data
{
    public interface ISQLite
    {
        SQLiteConnection GetConnection();
    }
}
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;

namespace TBSMobileApplication.Models
{
    public class User
    {
        [PrimaryKey, AutoIncrement]
        public int ContactID { get; set; }
        [Unique]
        public string UserID { get; set; }
        public string UserPassword { get; set; }

        public User() { }
        public User(string Username, string Password)
        {
            this.UserID = Username;
            this.UserPassword = Password;
        }

        public bool CheckInformation()
        {
            if(!this.UserID.Equals("") || !this.UserPassword.Equals(""))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
LoginPage.xaml.cs(基本上这是我登录页面中的代码)

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using TBSMobileApplication.Views;
using TBSMobileApplication.Data;

[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
namespace TBSMobileApplication
{
    public partial class App : Application
    {
        static TokenDatabaseController tokenDatabase;
        static UserDatabaseController userDatabase;

        public App ()
        {
            InitializeComponent();

            MainPage = new LoginPage();
        }

        protected override void OnStart ()
        {
            // Handle when your app starts
        }

        protected override void OnSleep ()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume ()
        {
            // Handle when your app resumes
        }

        public static UserDatabaseController UserDatabase
        {
             get
            {
                if(userDatabase == null)
                {
                    userDatabase = new UserDatabaseController();
                }

                return userDatabase;
            }
        }

        public static TokenDatabaseController TokenDatabase
        {
            get
            {
                if (tokenDatabase == null)
                {
                    tokenDatabase = new TokenDatabaseController();
                }

                return tokenDatabase;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TBSMobileApplication.Models;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace TBSMobileApplication.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LoginPage : ContentPage
    {
        public LoginPage ()
        {
            InitializeComponent ();
        }

        void LoginProcedure(object sender, EventArgs e)
        {
            User user = new User(entUser.Text, entPassword.Text);
            if (user.CheckInformation())
            {
                //DisplayAlert("Login Message", "Login Success", "Ok");
                try
                {
                    App.UserDatabase.SaveUser(user);
                    DisplayAlert("Database Message", "User Saved", "Ok");
                }
                catch(Exception ex)
                {
                    DisplayAlert("Message", ex.Message, "Ok");
                }

            }
            else
            {
                DisplayAlert("Login Message", "Login Failed", "Ok");
            }
        }
    }
}
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;

namespace TBSMobileApplication.Data
{
    public interface ISQLite
    {
        SQLiteConnection GetConnection();
    }
}
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;

namespace TBSMobileApplication.Models
{
    public class User
    {
        [PrimaryKey, AutoIncrement]
        public int ContactID { get; set; }
        [Unique]
        public string UserID { get; set; }
        public string UserPassword { get; set; }

        public User() { }
        public User(string Username, string Password)
        {
            this.UserID = Username;
            this.UserPassword = Password;
        }

        public bool CheckInformation()
        {
            if(!this.UserID.Equals("") || !this.UserPassword.Equals(""))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
ISQLite.cs(这是连接数据库的地方)

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using TBSMobileApplication.Views;
using TBSMobileApplication.Data;

[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
namespace TBSMobileApplication
{
    public partial class App : Application
    {
        static TokenDatabaseController tokenDatabase;
        static UserDatabaseController userDatabase;

        public App ()
        {
            InitializeComponent();

            MainPage = new LoginPage();
        }

        protected override void OnStart ()
        {
            // Handle when your app starts
        }

        protected override void OnSleep ()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume ()
        {
            // Handle when your app resumes
        }

        public static UserDatabaseController UserDatabase
        {
             get
            {
                if(userDatabase == null)
                {
                    userDatabase = new UserDatabaseController();
                }

                return userDatabase;
            }
        }

        public static TokenDatabaseController TokenDatabase
        {
            get
            {
                if (tokenDatabase == null)
                {
                    tokenDatabase = new TokenDatabaseController();
                }

                return tokenDatabase;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TBSMobileApplication.Models;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace TBSMobileApplication.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LoginPage : ContentPage
    {
        public LoginPage ()
        {
            InitializeComponent ();
        }

        void LoginProcedure(object sender, EventArgs e)
        {
            User user = new User(entUser.Text, entPassword.Text);
            if (user.CheckInformation())
            {
                //DisplayAlert("Login Message", "Login Success", "Ok");
                try
                {
                    App.UserDatabase.SaveUser(user);
                    DisplayAlert("Database Message", "User Saved", "Ok");
                }
                catch(Exception ex)
                {
                    DisplayAlert("Message", ex.Message, "Ok");
                }

            }
            else
            {
                DisplayAlert("Login Message", "Login Failed", "Ok");
            }
        }
    }
}
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;

namespace TBSMobileApplication.Data
{
    public interface ISQLite
    {
        SQLiteConnection GetConnection();
    }
}
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;

namespace TBSMobileApplication.Models
{
    public class User
    {
        [PrimaryKey, AutoIncrement]
        public int ContactID { get; set; }
        [Unique]
        public string UserID { get; set; }
        public string UserPassword { get; set; }

        public User() { }
        public User(string Username, string Password)
        {
            this.UserID = Username;
            this.UserPassword = Password;
        }

        public bool CheckInformation()
        {
            if(!this.UserID.Equals("") || !this.UserPassword.Equals(""))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
User.cs(这是用户表的参数所在)

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using TBSMobileApplication.Views;
using TBSMobileApplication.Data;

[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
namespace TBSMobileApplication
{
    public partial class App : Application
    {
        static TokenDatabaseController tokenDatabase;
        static UserDatabaseController userDatabase;

        public App ()
        {
            InitializeComponent();

            MainPage = new LoginPage();
        }

        protected override void OnStart ()
        {
            // Handle when your app starts
        }

        protected override void OnSleep ()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume ()
        {
            // Handle when your app resumes
        }

        public static UserDatabaseController UserDatabase
        {
             get
            {
                if(userDatabase == null)
                {
                    userDatabase = new UserDatabaseController();
                }

                return userDatabase;
            }
        }

        public static TokenDatabaseController TokenDatabase
        {
            get
            {
                if (tokenDatabase == null)
                {
                    tokenDatabase = new TokenDatabaseController();
                }

                return tokenDatabase;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TBSMobileApplication.Models;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace TBSMobileApplication.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LoginPage : ContentPage
    {
        public LoginPage ()
        {
            InitializeComponent ();
        }

        void LoginProcedure(object sender, EventArgs e)
        {
            User user = new User(entUser.Text, entPassword.Text);
            if (user.CheckInformation())
            {
                //DisplayAlert("Login Message", "Login Success", "Ok");
                try
                {
                    App.UserDatabase.SaveUser(user);
                    DisplayAlert("Database Message", "User Saved", "Ok");
                }
                catch(Exception ex)
                {
                    DisplayAlert("Message", ex.Message, "Ok");
                }

            }
            else
            {
                DisplayAlert("Login Message", "Login Failed", "Ok");
            }
        }
    }
}
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;

namespace TBSMobileApplication.Data
{
    public interface ISQLite
    {
        SQLiteConnection GetConnection();
    }
}
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;

namespace TBSMobileApplication.Models
{
    public class User
    {
        [PrimaryKey, AutoIncrement]
        public int ContactID { get; set; }
        [Unique]
        public string UserID { get; set; }
        public string UserPassword { get; set; }

        public User() { }
        public User(string Username, string Password)
        {
            this.UserID = Username;
            this.UserPassword = Password;
        }

        public bool CheckInformation()
        {
            if(!this.UserID.Equals("") || !this.UserPassword.Equals(""))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
SQLite\u Android.cs(这是我创建数据库的地方)

UserDatabaseController.cs(我在这里控制用户表,如添加、删除或从用户表获取数据)

使用SQLite;
使用制度;
使用System.Collections.Generic;
使用系统文本;
使用TBSMobileApplication.Models;
使用Xamarin.Forms;
命名空间TBSMobileApplication.Data
{
公共类UserDatabaseController
{
静态对象锁定器=新对象();
SQLiteConnection数据库;
公共用户数据库控制器()
{
数据库=DependencyService.Get().GetConnection();
CreateTable();
}
公共用户GetUser()
{
锁(储物柜)
{
if(database.Table().Count()==0)
{
返回null;
}
其他的
{
返回database.Table().First();
}
}
}
public int SaveUser(用户)
{
锁(储物柜)
{
如果(user.ContactID!=0)
{
数据库更新(用户);
返回user.ContactID;
}
其他的
{
返回数据库。插入(用户);
}
}
}
公共int删除用户(int联系人ID)
{
锁(储物柜)
{
返回数据库。删除(联系人ID);
}
}
}
}

从问题的第3部分开始-数据库文件在哪里在这里:

var DBFileName = "backend.db3";
string DocumentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
这相当于:

/data/data/[your.package.name]/files/backend.db3
对于问题的第一部分,要检查数据库是否已创建,只需检查文件是否存在:

    public static bool DBExists()
    {
        string DocumentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var path = Path.Combine(DocumentPath, "backend.db3");
        return File.Exists(path);
    }
如果没有根设备,在那里访问文件是困难和不可能的。你不应该能够访问那里的文件-只有你的应用程序才能访问它们。这是一项安全措施

但是,您的应用程序在访问数据库文件时不会遇到任何问题,因此您可以在应用程序中实现一种方法,将其复制到更易访问的位置(例如下载目录)

把这个放在你的Android项目中:

    public static void CopyDBToDownloadsDirectory()
    {
        var path = System.IO.Path.Combine(
            Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath,
            "backend.db3");
        string DocumentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var safePath = Path.Combine(DocumentPath, "backend.db3");
        File.Copy(safePath, path, true);
    }
调用它创建数据库副本,您可以在手机内置的文件浏览器上轻松访问该数据库


因此,对于问题的第2部分,无论事务成功还是失败,您都可以在代码中对数据库运行查询以进行检查,或者在中打开数据库文件的副本并浏览数据

您可以使用Android adb工具将backend.db3文件提取到桌面,然后使用SQLite工具检查数据我如何使用Android adb工具?在SO和其他网站上有数千篇文章解释如何使用adb-我再写一篇没有意义one@Jason我不能用try-catch语句吗?当我转到/data/data/[your.package.name]时/files/backend.db3没有文件名为backend.db3的文件在哪里添加公共静态void copydb2todownloadsdirectory()?在SQLite_Android.cs中?当我点击登录按钮时,在哪里执行?