大佬教程收集整理的这篇文章主要介绍了使用xamarin ios的构建之间不存在数据库,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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," + " @R_272_4687@T," + " Desc text " + ");"); } //Execute the update statements using (var cnn = new sqliteConnection(CNN_StriNG)) { cnn.open(); using (var trans = cnn.begintransaction(System.Data.isolationLevel.ReadCoR_967_11845@mitted)) { 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是空的?
String documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal); String libraryPath = Path.Combine (documentsPath,"../Library/"); var path = Path.Combine (libraryPath,"MyDatabase.db3");
您可能还想查看Github的Xamarin的sqlite类:
@L_801_36@
这是一个关于如何使用所述类的教程:
http://docs.xamarin.com/recipes/ios/data/sqlite/create_a_database_with_sqlitenet
以上是大佬教程为你收集整理的使用xamarin ios的构建之间不存在数据库全部内容,希望文章能够帮你解决使用xamarin ios的构建之间不存在数据库所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。