ios - 数据库在使用 xamarin ios 的构建之间未保留

标签 ios sqlite xamarin.ios xamarin

我正在 mac 上使用 xamarin studio 创建一个简单的 sqlite 驱动的 ios 应用程序。

sqlite 文件是在“个人”文件夹中创建的,并在构建之间保留,但是当我运行该应用程序时,我在之前的调试 session 中创建的表消失了吗?

在我的代码中,检查文件是否存在后,我使用 sqliteconnection 进行连接并创建一个表并使用来自命令对象的 executenonquery 方法插入一行。在相同的上下文中,我可以使用第二个命令对象查询表,但是如果我停止调试器并重新启动表,我会消失吗?

我是否应该将文件放在不同的文件夹中,是 xamarin 还是 ios 中的设置来保留表格?我是无意中在 sqlite 中使用了临时表还是出了什么问题?

注意:到目前为止,我只使用 xamarin 的入门版并在 iphone 模拟器上进行调试。

public class BaseHandler
{
     private static bool DbIsUpToDate { get; set; }

     const int DB_VERSION = 1;    //Created DB

     const string DB_NAME = "mydb.db3";
     protected const string CNN_STRING = "Data Source=" + DB_NAME + ";Version=3";

     public BaseHandler ()
     {
         //No need to validate database more than once on each restart.
         if (DbIsUpToDate)
             return;

         CheckAndCreateDatabase(DB_NAME);

         int userVersion = GetUserVersion();
         UpdateDBToVersion(userVersion);

         DbIsUpToDate = true;
    }

    int GetUserVersion()
    {
         int version = 0;

         using (var cnn = new SqliteConnection(CNN_STRING)) 
         {
             cnn.Open();

             using (var cmd = cnn.CreateCommand())
             {
                 cmd.CommandText = "CREATE TABLE UVERSION (VERSION INTEGER);" +
                                   "INSERT INTO UVERSION (VERSION) VALUES(1);";
                 cmd.ExecuteNonQuery();
             }

             using (var cmd = cnn.CreateCommand())
             {
                 cmd.CommandText = "SELECT VERSION FROM UVERSION;";
                 var pragma = cmd.ExecuteScalar();
                 version = Convert.ToInt32((long)pragma);
             }
        }

        return version;
    }


    void UpdateDBToVersion(int userVersion)
    {
         //Prepare the sql statements depending on the users current verion
        var sqls = new List<string> ();

         if (userVersion < 1) 
        {
             sqls.Add("CREATE TABLE IF NOT EXISTS MYTABLE ("
                     + " ID INTEGER PRIMARY KEY, "
                     + " NAME TEXT, "
                     + " DESC TEXT "
                     + ");");
        }

         //Execute the update statements
        using (var cnn = new SqliteConnection(CNN_STRING)) 
        {
             cnn.Open();

             using (var trans = cnn.BeginTransaction(System.Data.IsolationLevel.ReadCommitted))
             {
                foreach(string sql in sqls) 
                {
                    using (var cmd = cnn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        cmd.ExecuteNonQuery();
                    }
                }

                 trans.Commit();

                 //SetUserVersion(DB_VERSION);
            }

         }
     }

    protected string GetDBPath (string dbName)
    {
         // get a reference to the documents folder
        var documents = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

         // create the db path
        string db = Path.Combine (documents, dbName);

         return db;
    }

    protected void CheckAndCreateDatabase (string dbName)
    {
         var dbPath = GetDBPath(dbName);

         // determine whether or not the database exists
         bool dbExists = File.Exists(dbPath);

         if (!dbExists) 
            SqliteConnection.CreateFile(dbPath);
    }
 }

同样,我的问题是,每次我运行调试器时,它都会运行 GetUserVersion,但 UVERSION 表不会在 session 之间保留。 “File.Exists(dbPath)”返回 true,因此不会运行 CreateFile。为什么数据库是空的?

最佳答案

这是我用来在 iOS 模拟器中保存我的数据库的代码片段,数据似乎在应用程序编译之间保持良好:

string documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal); 
string libraryPath = Path.Combine (documentsPath, "../Library/");
var path = Path.Combine (libraryPath, "MyDatabase.db3");

您可能还想从 Github 查看 Xamarin 的 SQLite 类: https://github.com/praeclarum/sqlite-net/tree/master/src

这是一个关于如何使用所述类的教程: http://docs.xamarin.com/recipes/ios/data/sqlite/create_a_database_with_sqlitenet

关于ios - 数据库在使用 xamarin ios 的构建之间未保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16531550/

相关文章:

ios - 我如何知道用户何时从字母更改为数字 iOS

ios - Flutter - FileSystemException : Cannot open file, path = '/path'(操作系统错误 : Operation not permitted, errno = 1)在 ios 上

ios - 是否可以自动化涉及 iOS 应用程序双因素身份验证的测试?

python - 警告 : IPython History requires SQLite, 您的历史将不会被保存

java - 在 SQLite 数据库上使用 Java 的 JDBCPreparedStatement 查询 NOT NULL

c# - 从 Xamarin 读取多部分数据

ios - QuickBlox : How to delete user from application?

PHP sqlite 命令行访问

iphone - Monotouch 对话框无法退出 ILIST,因为后退按钮丢失,

c# - 如何在 Monotouch 中创建多级表?