
sqlite文件是在“个人”文件夹中创建的,并且在构建之间保留,但是当我运行应用程序时,我在上一个调试会话中创建的表格消失了吗?
在我的代码中,在检查文件是否存在之后,我使用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不会持久存在. “file.Exists(dbPath)”返回true,因此不运行Createfile.为什么db是空的?
解决方法 这是我用来在iOS模拟器中保存我的数据库的代码片段,数据似乎在app编译之间保持不变: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
以上是内存溢出为你收集整理的使用xamarin ios的构建之间不存在数据库全部内容,希望文章能够帮你解决使用xamarin ios的构建之间不存在数据库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)