c# - SQLite PCL 数据库插入花费太多时间

标签 c# sqlite windows-8.1 windows-phone-8.1

    namespace App3
    {

        public sealed partial class App : Application
        {
            private TransitionCollection transitions;

            public App()
            {
                this.InitializeComponent();
                this.Suspending += this.OnSuspending;

            }


            public static async Task ReadFile(String file1, String table1)
            {
                using (var connection = new SQLiteConnection("Storage.db"))
                {
                    string a;
                    var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(file1));
                    var stream = await file.OpenStreamForReadAsync();
                    using (StreamReader sr = new StreamReader(stream))
                    {
                        while (sr.Peek() >= 0)
                        {
                            a = sr.ReadLine();
                            string[] l = a.Split(',');
                            if (table1 == "commercial")
                            {
                                using (var statement = connection.Prepare(@"INSERT INTO " + table1 + " VALUES (\'" + l[0] + "\',\'" + l[1] + "\',\'" + l[2] + "\',\"" + l[3] + "\",\"" + l[4] + "\",\'" + l[5] + "\',\'" + l[6] + "\',\'"
                                    + l[7] + "\',\'" + l[8] + "\',\'" + l[9] + "\',\'" + l[10] + "\'); ON DUPLICATE UPDATE ID=ID"))
                                {
                                    statement.Step();
                                    statement.Reset();
                                    statement.ClearBindings();                   
                                }
                            }
                            else
                            {
                                using (var statement = connection.Prepare(@"INSERT INTO " + table1 + " VALUES (\'" + l[0] + "\',\'" + l[1] + "\',\'" + l[2] + "\',\"" + l[3] + "\",\"" + l[4] + "\",\'" + l[5] + "\',\'" + l[6] + "\',\'"
                                    + l[7] + "\',\'" + l[8] + "\',\'" + l[9] + "\'); ON DUPLICATE UPDATE ID=ID"))
                                {
                                    // Inserts data.
                                    statement.Step();
                                    statement.Reset();
                                    statement.ClearBindings();
                                }
                            }
                        }
                    }
                }
            }


            public void createdb(String table){
                using (var connection = new SQLiteConnection("Storage.db"))
                {


  using (var statement1 = connection.Prepare(@"DROP TABLE IF EXISTS " + table+";"))
            {
                statement1.Step();
                statement1.Reset();
                statement1.ClearBindings();
            }
                    using (var statement = connection.Prepare(@"
                                                   CREATE TABLE IF NOT EXISTS "+table+" (id INT NOT NULL PRIMARY KEY,"
                                                      + "TYPE VARCHAR(255),"
                                                      +"MAKE VARCHAR(255),"
                                                      +"MODEL VARCHAR(255),"
                                                      +"Fitment VARCHAR(255),"
                                                      +"Part VARCHAR(255),"
                                                      +"NRB_No VARCHAR(255),"
                                                      +"Dimension VARCHAR(255),"
                                                      +"No_Off INT,"
                                                      +"Company VARCHAR(255)"
                                                    +");"))
                    {
                        statement.Step();

                    }
                }
            }


            protected async override void OnLaunched(LaunchActivatedEventArgs e)
            {

                createdb("Moped");
                await ReadFile("ms-appx:///Assets/Mopeds.txt", "Moped");

                createdb("Scooter");
                await ReadFile("ms-appx:///Assets/Scooters.txt", "Scooter");

                createdb("Motorcycle");
                await ReadFile("ms-appx:///Assets/Motorcycles.txt", "Motorcycle");

                createdb("Auto");
                await ReadFile("ms-appx:///Assets/3W.txt", "Auto");

                createdb("Passenger");
                await ReadFile("ms-appx:///Assets/cars.txt", "Passenger");

                createdb("Muv");
                await ReadFile("ms-appx:///Assets/Muv.txt", "Muv");

                createdb("Commercial");
                await ReadFile("ms-appx:///Assets/Commercial.txt", "Commercial");

                createdb("Tractor");
                await ReadFile("ms-appx:///Assets/Tractors.txt", "Tractor");

  }

This is my App.xaml.cs i am adding the tables in the onlaunched method. The table is populated using the text files present in the local folder. But it is taking too much time to insert. Each insertion has around 300 rows and 9 entries but it is taking time. Is there a way to speed up? and am I doing correct or is there any other method to play with the database.I am using SQLITE PCL and this is for Windows 8.1 RT

EDIT

public static async Task ReadFile(String file1, String table1)
        {
            using (var connection = new SQLiteConnection("Storage.db"))
            {
                string a;
                var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(file1));
                var stream = await file.OpenStreamForReadAsync();
                using (StreamReader sr = new StreamReader(stream))
                {
                    while (sr.Peek() >= 0)
                    {
                        a = sr.ReadLine();
                        string[] l = a.Split(',');
                        try
                        {
                            using (var tr = connection.BeginTransaction())
                            {
                                using (SQLiteCommand cmd = connection.CreateCommand(@"DROP TABLE IF EXISTS " + table1 + ";"))
                                {
                                    cmd.ExecuteNonQuery();
                                }

                                if (table1 == "commercial")
                                    {        
                                        using (SQLiteCommand cmd1 = connection.CreateCommand(@"
                                               CREATE TABLE IF NOT EXISTS " + table1 + " (id INT NOT NULL PRIMARY KEY,"
                                                  + "TYPE VARCHAR(255),"
                                                  + "MAKE VARCHAR(255),"
                                                  + "MODEL VARCHAR(255),"
                                                  + "Fitment VARCHAR(255),"
                                                  + "Part VARCHAR(255),"
                                                  + "NRB_No VARCHAR(255),"
                                                  + "Dimension VARCHAR(255),"
                                                  + "No_Off INT,"
                                                  + "Company VARCHAR(255)"
                                                + ");"))
                                        {
                                            cmd1.ExecuteNonQuery();
                                        }

                                        using (SQLiteCommand cmd = connection.CreateCommand(@"INSERT INTO " + table1 + " VALUES (\'" + l[0] + "\',\'" + l[1] + "\',\'" + l[2] + "\',\"" + l[3] + "\",\"" + l[4] + "\",\'" + l[5] + "\',\'" + l[6] + "\',\'"
                                        + l[7] + "\',\'" + l[8] + "\',\'" + l[9] + "\',\'" + l[10] + "\'); ON DUPLICATE UPDATE ID=ID"))
                                        {
                                            cmd.ExecuteNonQuery();
                                        }
                                    }
                                    else
                                    {
                                        using (SQLiteCommand cmd1 = connection.CreateCommand(@"
                                               CREATE TABLE IF NOT EXISTS " + table1 + " (id INT NOT NULL PRIMARY KEY,"
                                                  + "TYPE VARCHAR(255),"
                                                  + "MAKE VARCHAR(255),"
                                                  + "MODEL VARCHAR(255),"
                                                  + "Fitment VARCHAR(255),"
                                                  + "Part VARCHAR(255),"
                                                  + "NRB_No VARCHAR(255),"
                                                  + "Dimension VARCHAR(255),"
                                                  + "No_Off INT,"
                                                  + "Company VARCHAR(255)"
                                                  + "Usage VARCHAR(255)"
                                                + ");"))
                                        {
                                            cmd1.ExecuteNonQuery();
                                        }
                                        using (SQLiteCommand cmd = connection.CreateCommand(@"INSERT INTO " + table1 + " VALUES (\'" + l[0] + "\',\'" + l[1] + "\',\'" + l[2] + "\',\"" + l[3] + "\",\"" + l[4] + "\",\'" + l[5] + "\',\'" + l[6] + "\',\'"
                                            + l[7] + "\',\'" + l[8] + "\',\'" + l[9] + "\'); ON DUPLICATE UPDATE ID=ID"))
                                        {
                                            // Inserts data.
                                            cmd.ExecuteNonQuery();
                                        }
                                    }
                                    tr.Commit();
                            }
                        }
                            catch (SQLiteException ex)
                            {
                               tr.Rollback();
                            }
                        }
                    }
                }
            }

最佳答案

确保将插入内容包装在显式打开并提交的 transaction 中.否则 SQLite 会将每个语句包装在它自己的事务中,这会降低性能。 HTH.

编辑: 另外,看看 this general question关于 SQLite 性能。

关于c# - SQLite PCL 数据库插入花费太多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24471692/

相关文章:

c# - 基于路由参数的子容器注册

python - 使用 SQLite 的 Django 中的日期时间差异

sqlite - 如何清理sqlite数据库?

javascript - 自定义绑定(bind)无法从 Knockout 2.3 更新到 3.3

windows-8.1 - 仅为提供程序中的某些特定 ETW 任务激活堆栈?

r - 在 Windows 上从源代码编译 RCurl

c# - "SystemInfo.deviceUniqueIdentifier"在 Android 构建中使用什么?

c# - 使用ajax在iframe中加载内容

c# - Parallel.ForEach() 更改模拟上下文

Android:为 MySQL 和 SQLite 数据库设计