Sqlite数据库没有保存插入的数据 [英] Sqlite database not saving data inserted

查看:119
本文介绍了Sqlite数据库没有保存插入的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一段代码,我希望用远程MySQL数据库中的一些条目更新应用程序SQLite数据库。代码执行时没有错误,但仍然没有反映在数据库中。我需要帮助搞清楚我错过了什么。

以下是代码段:



I'm having trouble with a segment of code where i wish to update the application SQLite database with some entries from a remote MySQL database. The code executes without errors but still does not reflect in the database. Please i need help figuring out what I'm missing out.
Below is the code segment:

#region Offline Upload

            if (items_going_offline > 0)
            {
                update_progress.SafeInvoke(d => d.Visible = true);

                DialogResult result = MessageBox.Show("Host " + items_going_offline + " document(s) to OFFLINE", "Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (result == DialogResult.Yes)
                {
                    #region Incoming
                    if (MySqlincoming_refNos.Count > 0)
                    {
                        foreach (string re in MySqlincoming_refNos)
                        {
                            path_offline.Clear();
                            path_online.Clear();

                            select_query_online = "select * from " + store.database() + "incoming where ref_no = @ref";

                            MySql_cmd = new MySqlCommand(select_query_online, online_con1);

                            MySql_cmd.Parameters.AddWithValue("@ref", (string)re);

                            Retry.retry.Do(() => store.openConnection("mysql", online_con1, con1), TimeSpan.FromSeconds(10), 5);

                            DataSet dsI = new DataSet();
                            MySqlDataAdapter daI = new MySqlDataAdapter(MySql_cmd);
                            daI.Fill(dsI);

                            if (dsI.Tables[0].Rows.Count > 0)
                            {
                                refNo = (string)dsI.Tables[0].Rows[0][1];
                                subject = (string)dsI.Tables[0].Rows[0][2];
                                date = (DateTime)dsI.Tables[0].Rows[0][3];
                                senders = (string)dsI.Tables[0].Rows[0][4]; ;
                                receiver = (string)dsI.Tables[0].Rows[0][5];

                                try
                                {
                                    for (int a = 1; a < 21; a++)
                                    {
                                        if (dsI.Tables[0].Rows[0][5 + a] != DBNull.Value)
                                        {
                                            string f = (string)dsI.Tables[0].Rows[0][5 + a];
                                            path_online.Add(f.Remove(0, 39));
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show("Unable to read data\n" + ex.Message);
                                }

                                
                                List<byte[]> downloadedFiles = new List<byte[]>();
                                //download the files
                                store.downloadFile(store.FTP(), path_online, ref downloadedFiles);

                                Retry.retry.Do(() => store.openConnection("sqlite", online_con1, con1), TimeSpan.FromSeconds(10), 5);

                                using (SQLiteTransaction Sqlite_transaction = con1.BeginTransaction())
                                {
                                    try
                                    {
                                        insert_query_online = "insert into incoming (ref_no, subject, date, sender, receiver, page_1, page_2, page_3, page_4, page_5, page_6, page_7, page_8, page_9, page_10, page_11, page_12, page_13, page_14, page_15, page_16, page_17, page_18, page_19, page_20) values(@ref_no, @subject, @date, @sender, @receiver, @page_1, @page_2, @page_3, @page_4, @page_5, @page_6, @page_7, @page_8, @page_9, @page_10, @page_11, @page_12, @page_13, @page_14, @page_15, @page_16, @page_17, @page_18, @page_19, @page_20)";

                                        Sqlite_cmd = new SQLiteCommand(insert_query_offline, con1);

                                        Sqlite_cmd.Parameters.AddWithValue("@ref_no", (string)refNo);
                                        Sqlite_cmd.Parameters.AddWithValue("@subject", (string)subject);
                                        Sqlite_cmd.Parameters.AddWithValue("@date", (DateTime)date);
                                        Sqlite_cmd.Parameters.AddWithValue("@sender", (string)senders);
                                        Sqlite_cmd.Parameters.AddWithValue("@receiver", (string)receiver);

                                        //make some pages = null
                                        for (int a = 0; a < 20; a++)
                                        {
                                            if(a < downloadedFiles.Count)
                                                Sqlite_cmd.Parameters.AddWithValue("@page_" + (a + 1) + "", (object)downloadedFiles[a]);
                                            else
                                                Sqlite_cmd.Parameters.AddWithValue("@page_" + (a + 1) + "", null);
                                        }

                                        //Retry.retry.Do(() => Sqlite_cmd.ExecuteNonQuery(), TimeSpan.FromSeconds(10), 5);
                                        Sqlite_cmd.ExecuteNonQuery();
                                        Sqlite_transaction.Commit();

                                        update_progress.SafeInvoke(d => d.Increment(1));

                                        offlineCountdown--;
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show("Unable to UPDATE an Offline incoming item");
                                        Sqlite_transaction.Rollback();
                                    }
                                }
                                store.closeConnection("sqlite", online_con1, con1);
                            } 
                            store.closeConnection("mysql", online_con1, con1);
                        }
                    }
                    #endregion





我尝试了什么:



[删除重复代码]



What I have tried:

[remove duplicate code]

推荐答案

你说你的代码没有错误地执行但数据没有插入到数据库中。



你需要习惯使用调试工具在视觉工作室。



如何在视觉工作室中使用断点 - Google搜索 [ ^ ]



使用断点 [ ^ ]



在Visual Studio中调试 [ ^ ]



你提供了一个示例代码,这很棒,但是为了使这个正在运行的应用程序,缺少很多代码。如果你从mysql中获取数据并将其移动到sql lite,你应该查看.net中的sql lite和mysql教程



与sql lite c#连接 - Google搜索 [ ^ ]



连接到mysql c# - Google搜索 [ ^ ]



这里的想法是把你的问题分解成更小的部分。因此,在这种精神下,您将首先编写代码来连接到mysql。连接工作后,编写代码以对mysql执行查询。然后看到一旦查询运行就会得到期望结果。如果查询没有运行...繁荣...有你的问题。但是如果查询运行,请编写代码以连接到sql lite并测试以查看它是否正常工作。如果该代码不起作用,那么您知道您的问题是使用sql lite。如果连接有效,请编写一个示例插入,如果插入有效,您知道您已经有正确的代码插入到sql lite中。继续这样做,你就会明白你已经修复了自己的代码而无需等待互联网上的陌生人为你编写应用程序。



因此,不要猜测代码在做什么,而是使用上面的链接和建议来使用断点逐步执行代码,并查看代码在什么时候出现问题。我们无法访问您的计算机,硬盘驱动器,数据库,也无法阅读思想。除了代码(示例:架构/示例数据)之外,这里有很多内容,任何人都可以使用给定的代码复制您的问题,以便能够为您提供帮助。因此,帮助您的最佳方法是鼓励您在visual studio中使用调试器来帮助自己。
You say your code executes with no errors but the data does not insert into the database.

You need to get comfortable with using debugging tools in visual studio.

how to use breakpoints in visual studio - Google Search[^]

Using Breakpoints[^]

Debugging in Visual Studio[^]

You've provided a sample code, which is great, but there is a lot of code missing in order to make this a running application. If you are taking data from mysql and moving it to sql lite, you should look at sql lite and mysql tutorials in .net

connect to sql lite c# - Google Search[^]

Connect to mysql c# - Google Search[^]

The idea here being take your issue and break it down into smaller pieces. So in that spirit, you would start by writing code to connect to mysql. Once the connection works, write the code to execute a query against mysql. Then see that you get the expect results once the query runs. If the query doesn't run...boom...there is your problem. But should the query run, write the code to connect to sql lite and test to see that is working. If that code doesn't work, then you know your issue is with sql lite. If the connection works, write a sample insert, if the insert works you know you've got the correct code to insert into sql lite. Keep going with this and you'll get to the point that you've fixed your own code without having to wait around for strangers on the internet to write your app for you.

So instead of guessing what your code is doing, use the above links and suggestions to step through your code with break points and see at what point your code is having issues. We don't have access to your computer, hard drive, databases, nor can we read minds. There is a lot missing here other than code (example: schema/sample data) for anyone to replicate your issue with given code to be able to help you. So the best way to help you is to encourage you to help yourself by using the debugger in visual studio.


这篇关于Sqlite数据库没有保存插入的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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