【原】WIn8 store 开发中使用SqlLite3数据库问题集锦
一、无法读取内置数据库数据
Q:当使用内置sqllite数据库文件时,提示找不到文件。
A:1、将数据库文件包含在项目内 ,然后在数据库文件属性上 复制到输出目录=使用复制 生成操作=内容。
2、通过类似资源的方式访问
var uri = new System.Uri("ms-appx:///Data/Database.wdb");
file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(file.Path);
Q:但使用程序动态生成的数据库文件时。
A:创建数据库文件:
1 var root = Windows.Storage.ApplicationData.Current.LocalFolder; 2 var path = await root.CreateFolderAsync("sql", CreationCollisionOption.OpenIfExists); 3 StorageFile filename = null; 4 try 5 { 6 filename = await path.CreateFileAsync("Database.wdb", CreationCollisionOption.FailIfExists); 7 LocalFile = filename.Path; 8 //第一次创建数据库时 新建表 9 //SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(file.Path); 10 //... 11 } 12 catch (Exception e) 13 { } 14 15 if (LocalFile.Length == 0) 16 {//创建数据库后 17 filename = await path.CreateFileAsync("Database.wdb", CreationCollisionOption.OpenIfExists); 18 LocalFile = filename.Path; 19 }
二、插入数据 ReadOnly
但是用内置数据库插入数据时,将会出现readonly ,这可能是因为内置资源时,默认为只读状态,所以只能使用程序动态生成应用数据跟路径下的动态数据库。
三、查询中文
1 SQLite.SQLiteConnection conn = Database.getDatabaseCon(); 2 Byte[] utf8 = System.Text.Encoding.UTF8.GetBytes("企业家"); 3 List<Person_table> pl_data = conn.Query<Person_table>("select * from person_table where per_type_info=?;",System.Text.Encoding.UTF8.GetString(utf8,0,utf8.Length)); 4 foreach (var item in pl_data) 5 { 6 Debug.WriteLine("select : " + item.per_name_info); 7 } 8 conn.Close();