C# VS Xamarin Android SQLite net在Android v7.x中使应用程序崩溃
我正在尝试使用Visual Studio 2015、Xamarin和SQLite制作一个跨平台的应用程序。在Android v6.0之前,一切都很正常。但是,当我在安卓7.x(7.0和7.1.1)模拟器上测试我的应用程序时,我的应用程序甚至没有启动就崩溃了。我在xamarin论坛上通过这些链接读到了关于这次崩溃的消息,并且知道由于7.0原生sqlite不能使用,但是那里的答案提到了这一点- 首先,我从“Manage NuGet Packages for”中删除了库 解决方案…'-库“sqlite net pcl”和连接的库 “SQLitePCL.raw”。然后我通过 PackageManager控制台,在我的例子中,命令是“安装软件包” sqlite净pcl’。现在的版本是“sqlite net pcl”-->1.2.0| “SQLitePCL.raw”->1.0.1。它还下载了其他依赖项,如 SQLitePCLRaw.bundle_绿色 但是,我的项目已经有了sqlite net pcl和相关软件包的更新版本C# VS Xamarin Android SQLite net在Android v7.x中使应用程序崩溃,c#,android,sqlite,xamarin,xamarin.android,C#,Android,Sqlite,Xamarin,Xamarin.android,我正在尝试使用Visual Studio 2015、Xamarin和SQLite制作一个跨平台的应用程序。在Android v6.0之前,一切都很正常。但是,当我在安卓7.x(7.0和7.1.1)模拟器上测试我的应用程序时,我的应用程序甚至没有启动就崩溃了。我在xamarin论坛上通过这些链接读到了关于这次崩溃的消息,并且知道由于7.0原生sqlite不能使用,但是那里的答案提到了这一点- 首先,我从“Manage NuGet Packages for”中删除了库 解决方案…'-库“sqlite
sqlite-net-pcl => 1.2.0
SQLitePCL.bundle_green => 0.9.3
SQLitePCL.raw => 0.9.3
SQLitePCLRaw.bundle_green => 1.1.0
SQLitePCLRaw.core => 1.1.0
我被困在这里,他们都更新到最新的我应该改变什么
相关代码-
SqliteService.cs
using System;
using Medical_Study;
using Xamarin.Forms;
using Medical_Study.Droid;
using System.IO;
[assembly: Dependency(typeof(SqliteService))]
namespace Medical_Study.Droid
{
public class SqliteService : ISQLite
{
public SqliteService()
{
}
#region ISQLite implementation
public SQLite.SQLiteConnection GetConnection()
{
var sqliteFilename = "QBank.db";
string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); // Documents folder
var path = Path.Combine(documentsPath, sqliteFilename);
// This is where we copy in the prepopulated database
Console.WriteLine(path);
if (!File.Exists(path))
{
var s = Forms.Context.Resources.OpenRawResource(Resource.Raw.QBank); // RESOURCE NAME ###
// create a write stream
FileStream writeStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
// write to the stream
ReadWriteStream(s, writeStream);
}
var conn = new SQLite.SQLiteConnection(path);
// Return the database connection
return conn;
}
#endregion
/// <summary>
/// helper method to get the database out of /raw/ and into the user filesystem
/// </summary>
void ReadWriteStream(Stream readStream, Stream writeStream)
{
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0, Length);
// write the required bytes
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = readStream.Read(buffer, 0, Length);
}
readStream.Close();
writeStream.Close();
}
}
}
using System;
using SQLite;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
using System.Text;
using System.Threading.Tasks;
using Medical_Study.Models;
namespace Medical_Study
{
public class DataAccess
{
static object locker = new object();
SQLiteConnection database;
public DataAccess()
{
database = DependencyService.Get<ISQLite>().GetConnection();
// create the tables
database.CreateTable<QBank>();
}
public IEnumerable<QBank> GetAllQuestions()
{
lock (locker)
{
return (from i in database.Table<QBank>() select i).ToList();
}
//return database.Query<QBank>("Select * From [QBank] where [qid]");
}
public IEnumerable<QBank> GetSpecialQuestions()
{
lock (locker)
{
return database.Query<QBank>("SELECT * FROM [QBank] WHERE [question_text] = 0");
}
}
public QBank GetQuestion(int id)
{
lock (locker)
{
return database.Table<QBank>().FirstOrDefault(x => x.qid == id);
}
}
public int SaveQuestion(QBank question)
{
lock (locker)
{
if (question.qid != 0)
{
database.Update(question);
return question.qid;
}
else
{
return database.Insert(question);
}
}
}
public int DeleteQuestion(int id)
{
lock (locker)
{
return database.Delete<QBank>(id);
}
}
}
}
使用系统;
运用医学研究;
使用Xamarin.Forms;
使用医学研究机器人;
使用System.IO;
[程序集:依赖项(typeof(SqliteService))]
名称空间医学研究机器人
{
公共类SqliteService:ISQLite
{
公共SqliteService()
{
}
#区域ISQLite实现
public SQLite.SQLiteConnection GetConnection()
{
var sqliteFilename=“QBank.db”;
string documentsPath=System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);//文档文件夹
var path=path.Combine(documentsPath,sqliteFilename);
//这就是我们复制预填充数据库的地方
控制台写入线(路径);
如果(!File.Exists(path))
{
var s=Forms.Context.Resources.OpenRawResource(Resource.Raw.QBank);//资源名称###
//创建写入流
FileStream writeStream=newfilestream(路径,FileMode.OpenOrCreate,FileAccess.Write);
//写入流
ReadWriteStream(s,writeStream);
}
var conn=new SQLite.SQLiteConnection(路径);
//返回数据库连接
返回连接;
}
#端区
///
///将数据库从/raw/中取出并放入用户文件系统的helper方法
///
void ReadWriteStream(流readStream,流writeStream)
{
整数长度=256;
字节[]缓冲区=新字节[长度];
int bytesRead=readStream.Read(缓冲区,0,长度);
//写入所需的字节
而(字节读取>0)
{
writeStream.Write(缓冲区,0,字节读取);
bytesRead=readStream.Read(缓冲区,0,长度);
}
readStream.Close();
writeStream.Close();
}
}
}
DataAccess.cs
using System;
using Medical_Study;
using Xamarin.Forms;
using Medical_Study.Droid;
using System.IO;
[assembly: Dependency(typeof(SqliteService))]
namespace Medical_Study.Droid
{
public class SqliteService : ISQLite
{
public SqliteService()
{
}
#region ISQLite implementation
public SQLite.SQLiteConnection GetConnection()
{
var sqliteFilename = "QBank.db";
string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); // Documents folder
var path = Path.Combine(documentsPath, sqliteFilename);
// This is where we copy in the prepopulated database
Console.WriteLine(path);
if (!File.Exists(path))
{
var s = Forms.Context.Resources.OpenRawResource(Resource.Raw.QBank); // RESOURCE NAME ###
// create a write stream
FileStream writeStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
// write to the stream
ReadWriteStream(s, writeStream);
}
var conn = new SQLite.SQLiteConnection(path);
// Return the database connection
return conn;
}
#endregion
/// <summary>
/// helper method to get the database out of /raw/ and into the user filesystem
/// </summary>
void ReadWriteStream(Stream readStream, Stream writeStream)
{
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0, Length);
// write the required bytes
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = readStream.Read(buffer, 0, Length);
}
readStream.Close();
writeStream.Close();
}
}
}
using System;
using SQLite;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
using System.Text;
using System.Threading.Tasks;
using Medical_Study.Models;
namespace Medical_Study
{
public class DataAccess
{
static object locker = new object();
SQLiteConnection database;
public DataAccess()
{
database = DependencyService.Get<ISQLite>().GetConnection();
// create the tables
database.CreateTable<QBank>();
}
public IEnumerable<QBank> GetAllQuestions()
{
lock (locker)
{
return (from i in database.Table<QBank>() select i).ToList();
}
//return database.Query<QBank>("Select * From [QBank] where [qid]");
}
public IEnumerable<QBank> GetSpecialQuestions()
{
lock (locker)
{
return database.Query<QBank>("SELECT * FROM [QBank] WHERE [question_text] = 0");
}
}
public QBank GetQuestion(int id)
{
lock (locker)
{
return database.Table<QBank>().FirstOrDefault(x => x.qid == id);
}
}
public int SaveQuestion(QBank question)
{
lock (locker)
{
if (question.qid != 0)
{
database.Update(question);
return question.qid;
}
else
{
return database.Insert(question);
}
}
}
public int DeleteQuestion(int id)
{
lock (locker)
{
return database.Delete<QBank>(id);
}
}
}
}
使用系统;
使用SQLite;
使用System.Collections.Generic;
使用System.Linq;
使用Xamarin.Forms;
使用系统文本;
使用System.Threading.Tasks;
使用医学研究模型;
名称空间医学研究
{
公共类数据访问
{
静态对象锁定器=新对象();
SQLiteConnection数据库;
公共数据访问()
{
数据库=DependencyService.Get().GetConnection();
//创建表
CreateTable();
}
公共IEnumerable GetAllQuestions()
{
锁(储物柜)
{
返回(从database.Table()中的i选择i.ToList();
}
//返回database.Query(“从[QBank]where[qid]中选择*”;
}
公共IEnumerable GetSpecialQuestions()
{
锁(储物柜)
{
返回database.Query(“从[QBank]中选择*,其中[question_text]=0”);
}
}
公共QBank GetQuestion(内部id)
{
锁(储物柜)
{
返回database.Table().FirstOrDefault(x=>x.qid==id);
}
}
公共问题(QBank问题)
{
锁(储物柜)
{
如果(question.qid!=0)
{
数据库更新(问题);
返回问题.qid;
}
其他的
{
返回数据库。插入(问题);
}
}
}
公共整数删除问题(整数id)
{
锁(储物柜)
{
返回数据库。删除(id);
}
}
}
}