将数据库从已安装的位置复制到本地文件

时间:2017-06-19 07:57:10

标签: c# sqlite uwp

我有一个数据库" ms-appx:///Databases/tryout2.sqlite" (安装位置)。我想将它复制到本地应用程序文件夹,因为我需要在用户每次登录时将数据插入数据库中的表(如果名称和电子邮件相同,则不会插入数据库)。我在用户首次登录时遇到问题,然后可以将数据库复制到本地文件夹。但如果用户再次登录,则会显示如下错误消息: error message

App.xaml.cs:

    public static string DB_PATH = Path.Combine(Path.Combine(Package.Current.InstalledLocation.Path, @"Databases\tryout2.sqlite"));
            public App()
            {
                this.InitializeComponent();
                this.Suspending += OnSuspending;
                if (!CheckFileExists("tryout2.sqlite").Result)
                {
                    using (var db = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DB_PATH))
                    {
                        db.CreateTable<DBQUIZ>();
                        db.CreateTable<DBQUESTION>();
                        db.CreateTable<DBOPTION>();
                        db.CreateTable<DBUSER>();
                        db.CreateTable<DBRESULT>();
                    }
                }
            }

            private async Task<bool> CheckFileExists(string fileName)
            {
                try
                {
                    var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
                    return true;
                }
                catch
                {
                }
                return false;
            }

DatabaseHelper.cs:

string DB_PATH_COPY;

        public void CreateDatabase(string DB_PATH)
        {
            if (!CheckFileExists(DB_PATH).Result)
            {
                using (SQLite.Net.SQLiteConnection dbConn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), App.DB_PATH))
                {
                    dbConn.CreateTable<DBQUIZ>();
                    dbConn.CreateTable<DBQUESTION>();
                    dbConn.CreateTable<DBOPTION>();
                    dbConn.CreateTable<DBUSER>();
                    dbConn.CreateTable<DBRESULT>();
                }
            }
        }

        private async Task<bool> CheckFileExists(string fileName)
        {
            try
            {
                var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
                return true;
            }
            catch
            {
                return false;
            }
        }

public async void InsertUser(DBUSER objUser)
        {
            if (!CheckFileExists(DB_PATH_COPY).Result)
            {
                StorageFile dbFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Databases/tryout2.sqlite"));
                StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                StorageFile dbFileCopy = await dbFile.CopyAsync(localFolder);
                DB_PATH_COPY = Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, dbFileCopy.Path.ToString()));
            }

            using (var dbConn = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DB_PATH_COPY))
            {
                    dbConn.RunInTransaction(() =>
                    {
                        dbConn.Insert(objUser);
                    });
            }
        }

代码:

DatabaseHelper Db_Helper = new DatabaseHelper();//Creating object for DatabaseHelperClass.cs from ViewModel/DatabaseHelperClass.cs  
            if (emailField.Text != "" & namaField.Text != "")
            {
                Db_Helper.InsertUser(new Tryout.Models.DBUSER(emailField.Text, "", 0, namaField.Text));
                this.Frame.Navigate(typeof(Tryout.TryoutHome1));
            }
            else
            {
                MessageDialog messageDialog = new MessageDialog("Nama dan Email harus diisi");//Text should not be empty  
                await messageDialog.ShowAsync();
            }

如何处理?或者是否有另一种方法将数据插入到安装位置的数据库中的表中?

0 个答案:

没有答案