今天我面临这个问题:我有一个例外:
SQLite.SQLiteException:文件已加密或不是数据库
我正在做有关本指南的项目:https://developer.xamarin.com/guides/xamarin-forms/working-with/databases/。 这是我的数据库类,我初始化连接和表创建:
public class ThingDatabase
{
SQLiteConnection database;
public ThingDatabase()
{
database = DependencyService.Get<ISQLite>().GetConnection();
database.CreateTable<ThingObj>();
}
}
这是我的对象类:
public class ThingObj
{
public ThingObj()
{
}
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string City { get; set; }
public string ThingNumber { get; set; }
public bool IsStarted { get; set; }
}
这是我创建的SQLite_Android
课程,例如:
[assembly: Dependency(typeof(SQLite_Android))]
...
{
public class SQLite_Android : ISQLite
{
public SQLite_Android() { }
public SQLite.SQLiteConnection GetConnection() {
var sqliteFilename = "ThingSQLite.db3";
string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(documentsPath, sqliteFilename);
Console.WriteLine(path);
if (!File.Exists(path))
{
var s = Forms.Context.Resources.OpenRawResource(Resource.Raw.Database);
FileStream writeStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
ReadWriteStream(s, writeStream);
}
var conn = new SQLite.SQLiteConnection(path);
return conn;
}
void ReadWriteStream(Stream readStream, Stream writeStream)
{
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = readStream.Read(buffer, 0, Length);
}
readStream.Close();
writeStream.Close();
}
}
当我调试时,在database.CreateTable<ThingObj>();
断点之后,我收到该异常并重定向到SQLite.cs
类最后一行。
编辑:关于AkashAmin建议我将sqliteFilename更改为另一个字符串名称并解决了之前的问题。现在我想在路径中创建数据库文件,但遗憾的是Resource.Designer.cs
文件不会在Resource.designer.cs
中自动生成Raw部分类。我收到了这个错误:
&#39;资源&#39;不包含&#39; Raw&#39;
的定义