Sch*_*Box 4 c# database sqlite portable-class-library windows-phone-8.1
我有一个Windows Phone 8.1类库,我想稍后添加它作为Windows Phone 8.1 App项目的参考.
该ClassLibrary应负责创建和管理自己的数据库.我尝试SQLiteConnection
在我的ClassLibrary中创建一个新的,但它抛出以下错误:A first chance exception of type 'System.InvalidOperationException' occurred in SQLitePCL.DLL
但是,如果我在我的MainApp中做同样的事情一切正常.
那么,是否可以在ClassLibrary中创建一个SQLite数据库,该数据库负责在没有MainApp支持的情况下创建和管理它.
小智 5
我有一个项目,其中SQLite库在类库中,然后我使用另一个类库进行我的应用程序和SQLite库之间的通信
添加此NuGet包后,您会看到您的类库有2个新类:SQLite.cs和SQLiteAsync.cs.
SQLite和线程也存在已知问题(页面加载时为NullReferenceException),您可以通过TableMapping GetMapping
在SQLite.cs 中的方法中添加锁来修复它:
public TableMapping GetMapping(Type type, CreateFlags createFlags = CreateFlags.None)
{
if (_mappings == null) {
_mappings = new Dictionary<string, TableMapping> ();
}
lock (_mappings)
{
TableMapping map;
if (!_mappings.TryGetValue(type.FullName, out map))
{
map = new TableMapping(type, createFlags);
_mappings[type.FullName] = map;
}
return map;
}
}
Run Code Online (Sandbox Code Playgroud)
设置引用后,您可以在此类库中使用SQLite库.
在我的项目中,我尝试分解我的代码,所以我开始创建一个名为DatabaseHelper.cs的类:
public class DatabaseHelper
{
private String DB_NAME = "DATABASENAME.db";
public SQLiteAsyncConnection Conn { get; set; }
public DatabaseHelper()
{
Conn = new SQLiteAsyncConnection(DB_NAME);
this.InitDb();
}
public async void InitDb()
{
// Create Db if not exist
bool dbExist = await CheckDbAsync();
if (!dbExist)
{
await CreateDatabaseAsync();
}
}
public async Task<bool> CheckDbAsync()
{
bool dbExist = true;
try
{
StorageFile sf = await ApplicationData.Current.LocalFolder.GetFileAsync(DB_NAME);
}
catch (Exception)
{
dbExist = false;
}
return dbExist;
}
private async Task CreateDatabaseAsync()
{
//add tables here
//example: await Conn.CreateTableAsync<DbComment>();
}
}
Run Code Online (Sandbox Code Playgroud)
在创建DatabaseHelper类之后,您可以首先为数据库中的每个表创建一个数据源类.在我的情况下,我有一个CommentDataSource.cs:
public class CommentDataSource
{
private DatabaseHelper db;
public CommentDataSource(DatabaseHelper databaseHelper)
{
this.db = databaseHelper;
}
public async Task<long> AddComment(String vat, String comment)
{
long id = 0;
DateTime date = DateTime.Now;
DbComment dbc = new DbComment(vat, comment, date);
await db.Conn.InsertAsync(dbc);
DbComment insertDbc = await db.Conn.Table<DbComment>().ElementAtAsync(await db.Conn.Table<DbComment>().CountAsync() - 1);
if (insertDbc != null)
{
id = insertDbc.Id;
}
return id;
}
public async void RemoveComment(long idComment)
{
DbComment comment = await db.Conn.Table<DbComment>().Where(c => c.Id == idComment).FirstOrDefaultAsync();
if (comment != null)
{
await db.Conn.DeleteAsync(comment);
}
}
public async Task<List<DbComment>> FetchAllComments(String vat)
{
return await db.Conn.Table<DbComment>().Where(x => x.VAT == vat).ToListAsync();
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,您将添加的所有数据源都将使用相同的databasehelper.
您仍然需要添加对sqlite lib的引用,否则您将收到错误.
现在您可以开始使用您的数据源类,就像您在此处看到的那样:
private DatabaseHelper db = new DatabaseHelper();
private CommentDataSource commentDataSource;
public MainPage()
{
this.InitializeComponent();
commentDataSource = new CommentDataSource(db);
}
Run Code Online (Sandbox Code Playgroud)
现在,您的应用中可以使用CommentsDataSource的每个方法.
希望这对你有所帮助!