标签: sqlite-net

使用SQLiteAsyncConnection在SQLite-net C#中创建通用查询

我正在使用SQLite-net(https://github.com/praeclarum/sqlite-net)在Android上使用Mono实现数据库,并具有以下条件:

public class DatabaseTestASync
{
    private SQLiteAsyncConnection m_db;

    public delegate void StocksFound(List<Stock> list);
    public event StocksFound StocksFoundListener;

    // Uninteresting parts remove, e.g. constructor

    public void AddStock(Stock stock)
    {
        m_db.InsertAsync(stock).ContinueWith(t => { Debug.WriteLine(stock.Symbol + " added"); });
    }

    public void GetAllStocks()
    {
        AsyncTableQuery<Stock> query = m_db.Table<Stock>().Where(stock => true);
        query.ToListAsync().ContinueWith(QueryReturns);
    }

    private void QueryReturns(Task<List<Stock>> t)
    {
        if (StocksFoundListener != null)
            StocksFoundListener(t.Result);
    }
Run Code Online (Sandbox Code Playgroud)

这非常适合给我一个股票列表,但我设想在我的项目中有一个表集合,并且不想为每个表创建单独的AddX,GetAllX,QueryReturns(X)等.我正在执行这些数据库操作的通用方法并尝试以下方法:

public class DBQuery<T>
{
    private SQLiteAsyncConnection m_db;
    public delegate void OnRecordsFound(List<T> list);
    public event OnRecordsFound RecordsFoundListener;

    public …
Run Code Online (Sandbox Code Playgroud)

c# generics asynchronous sqlite-net

5
推荐指数
1
解决办法
3847
查看次数

Sqlite-net guid select返回空guid

我正在开发一个使用sqlite作为数据库的Windows应用商店应用.我正在使用SQLite for Windows Runtime和sqlite-net.试图从表中获取Guid列表,但只在列表中获取空guid({00000000-0000-0000-0000-000000000000}).但是,当仅查询一个guid时,它可以正常工作.

我的代码如下:

async void SyncSurveys()
    {
        SQLiteConnection _db = new SQLiteConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, "SurveyDB"));

        var localSurveys = (from s in _db.Table<Survey>()
            select s.SurveyGuid).ToList();

        ...
    }
Run Code Online (Sandbox Code Playgroud)

我也尝试过以下格式的查询,但它既不起作用:

var localSurveys = _db.Table<Survey>().Select(s => s.SurveyGuid).ToList();
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用以下查询,只为了调试目的只获得一个guid它运行良好:

var localSurvey = db.Table<Survey>().FirstOrDefault().SurveyGuid;
Run Code Online (Sandbox Code Playgroud)

在不工作的情况下,列表的计数与表的rowcount匹配.有没有人知道为什么这不适用于列表版本?

c# sqlite windows-store-apps sqlite-net

5
推荐指数
1
解决办法
1050
查看次数

具有多个WHERE条件的SQLite数据库查询

试图开发Windows 8.1商店应用程序.除了其他困难之外,我还需要在where子句中使用两个参数从sqlite数据库中检索记录.我可以使用where子句中的一个参数成功查询,但是当我尝试使用两个参数时它会崩溃.这是我的代码:

    public string SaveMaint(MaintViewModel Maintenance)
    {
        string result = string.Empty;
        using (var db = new SQLite.SQLiteConnection(App.DBPath))
        {
            string change = string.Empty;
            try
            {
                var existingmaintenance = db.Query<maintenance> ("select * from maintenance where VehID = ? AND MaintID = ?", new String[] {Maintenance.Maintid, Maintenance.Vehicleid});
               // var existingmaintenance = (db.Table<maintenance>().Where 
               //     (c => c.MaintID == Maintenance.Maintid).SingleOrDefault());

                if (existingmaintenance != null)
                {
                    existingmaintenance.VehID = Maintenance.Vehicleid;
                    existingmaintenance.MaintID = Maintenance.Maintid; 
                    existingmaintenance.ServiceDate = Maintenance.Servicedate;
                    existingmaintenance.ServiceCost = Maintenance.Servicecost;
                    existingmaintenance.ServiceLocation = Maintenance.Servicelocation;
                    existingmaintenance.ServiceNote = Maintenance.Servicenote;
                    existingmaintenance.ServiceOdom = …
Run Code Online (Sandbox Code Playgroud)

c# database sqlite sqlite-net

5
推荐指数
1
解决办法
8954
查看次数

即使在卸载后,Universal App的手机版似乎也在缓存数据库

我正在使用SQLite开发Universal App,我一直在使用本教程和SQLite for Windows Runtime(Windows Phone 8.1)和sqLite-net开始使用.

我用它来检查数据库是否存在:

bool exists = true;

try {
    var file = await ApplicationData.Current.LocalFolder.GetFileAsync( "database.db" );
} catch {
    exists = false;
}
Run Code Online (Sandbox Code Playgroud)

如果没有运行一些插入查询来填充默认数据:

if( !exists ) {
    var conn = new SQLiteConnection( "database.db );
    conn.CreateTable<MyClass>();
    MyClass.Insert( "Default data value" );
}
Run Code Online (Sandbox Code Playgroud)

MyClass的地方

[Table( "MyClass" )]
public class MyClass {
    public MyClass() {
        this.ID = -1;
        this.Name = string.Empty;
    }

    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }

    [NotNull]
    public string Name …
Run Code Online (Sandbox Code Playgroud)

c# sqlite-net windows-phone-8.1 win-universal-app

5
推荐指数
1
解决办法
265
查看次数

SQLite.net中的可序列化数据类型

简介:在基于SQLite.net的SQLite数据库中(在WP8.1 SL上,但这无关紧要),我正在基于给定对象添加数据。该对象包含一个名为的自定义类型Date。到目前为止,我不将该属性存储在数据库中,而是使用另一个属性作为解决方法。

[Ignore]
public Date Date { get; set; }

[PrimaryKey]
public DateTime DateInternal
{
    get { return Date.ToDateTime(); }
    set { Date = new Date(value); }
}
Run Code Online (Sandbox Code Playgroud)

虽然效果很好,但我觉得这不是最好的方法。

实际问题:我该如何改善。即,我如何Date直接存储序列化版本。它应该以某种方式Date可以用作主键。对于我而言,将所有属性都放在Date表的单个列中并不重要。我只想将Date自己存储在一列中。

目前的研究:在尝试向Google寻求答案的过程中,我偶然发现了SQLite.net的ISerializable界面,但由于该serialize方法只有一种方法而没有deserialize方法,因此我不确定如何使用它。

namespace SQLite.Net
{
    public interface ISerializable<T>
    {
        [PublicAPI]
        T Serialize();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# sqlite serialization sqlite-net

5
推荐指数
1
解决办法
1761
查看次数

无法打开SQLite数据库

使用此代码:

public void InsertPlatypiRequestedRecord(string PlatypusId, string PlatypusName, DateTime invitationSentLocal)
{
    var db = new SQLiteConnection(SQLitePath);
    {
        db.CreateTable<PlatypiRequested>();
        db.RunInTransaction(() =>
            {
                db.Insert(new PlatypiRequested
                              {
                                  PlatypusId = PlatypusId,
                                  PlatypusName = PlatypusName,
                                  InvitationSentLocal = invitationSentLocal
                              });
                db.Dispose();
            });
    }
}
Run Code Online (Sandbox Code Playgroud)

...我得到," SQLite.SQLiteException未被用户代码处理HResult = -2146233088 Message =无法从未打开的数据库创建命令 "

...但是尝试添加"db.Open()"不起作用,因为显然没有这样的方法.

c# sqlite windows-8 windows-store-apps sqlite-net

4
推荐指数
1
解决办法
7767
查看次数

使用 SQLite.Net.Async 扩展 PCL 1.3.0 在同一对象上使用多个 @OneToOne?

是否可以使用 SQLite.Net.Async Extensions PCL 1.3.0 在一个实体中拥有多个 OneToOne 关系?

例子:

[Table("body")]
public class Body
{
    [OneToOne(CascadeOperations = CascadeOperation.All)]
    [Column ("left")]
    public Hand Left { get; set; }

    [OneToOne(CascadeOperations = CascadeOperation.All)]
    [Column ("right")]
    public Hand Right { get; set; }
}

[Table("hand")]
public class Hand
{
    // In this I do not need a reference back to Body.
}
Run Code Online (Sandbox Code Playgroud)

我一直在尝试使用以下答案:

SQLite-Net 扩展两个实体之间的一对一和一对多关系

并以这些网站为灵感:

https://bitbucket.org/twincoders/sqlite-net-extensions/overview

https://bitbucket.org/twincoders/sqlite-net-extensions/src/65a1f8519347c40c948855cfc1a1d4d8bbcc8748/Tests/ReflectionExtensionsTests.cs?at=master&fileviewer=file-view-default

不幸的是,到目前为止还没有运气。甚至有可能吗?

sqlite xamarin sqlite-net sqlite-net-extensions

4
推荐指数
1
解决办法
941
查看次数

池化只读 SQLite 连接用于读写访问

使用的 nuget 包:System.Data.SQLite.Core, 1.0.98.1

问题:在我的程序中,我在某些情况下使用启用了池和只读访问的 SQLite。通常它工作正常,但是如果对数据库有很多混合的只读/读写密集型请求,则程序会失败并出现以下异常:

Unhandled Exception: System.Data.SQLite.SQLiteException: attempt to write a readonly database
attempt to write a readonly database
at System.Data.SQLite.SQLite3.Reset(SQLiteStatement stmt)
at System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt)
at System.Data.SQLite.SQLiteDataReader.NextResult()
at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
Run Code Online (Sandbox Code Playgroud)

如果我禁用池化,则程序运行良好。我假设以某种方式汇集只读连接用于读写连接。我错过了什么 - 即它是否是预期的行为?

要重现的最少代码(在插入或删除时失败)。例如Thread.Sleep(10000),如果我引入延迟,它就可以正常工作。如果我删除循环,它也可以正常工作。

const string DbFilePath = "test.sqlite";
string readOnlyConnectionString = new SQLiteConnectionStringBuilder
    {
        DataSource = DbFilePath,
        Pooling = true,
        ReadOnly = true
    }.ConnectionString; // data source=test.sqlite;pooling=True;read only=True
string readWriteConnectionString = new SQLiteConnectionStringBuilder
    {
        DataSource …
Run Code Online (Sandbox Code Playgroud)

.net c# sqlite sqlite-net

4
推荐指数
1
解决办法
2607
查看次数

SQLite Net Extensions OneToMany 关系不插入/保存/更新子级

我正在尝试使用 SQLite Net Extensions 为游戏做一个笔记应用程序,它使用 3 层模型, Game [1 has many *] Character [1 has many *] Note [1 applies to *] Character

我在 Visual Studio Community 2015 中使用 Xamarin,并使用 NuGet 包管理器安装了 SQLiteNetExtensions。

我还没有通过游戏和角色之间的第一级关系,插入数据库(无论是通过初始插入然后更新,还是递归使用 InsertWithChildren)都不会更新游戏对象中的角色。它只会为List<CharacterModel>内部 GameModel 生成一个空对象。然而,游戏和角色都在数据库中。

抽象基础模型

public abstract class IdentifiableModel
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

游戏模型

[Table("Game")]
public class GameModel : IdentifiableModel
{
    [MaxLength(64)]
    public string Name { get; set; }

    [OneToMany]
    public List<CharacterModel> Characters { get; set; }
} …
Run Code Online (Sandbox Code Playgroud)

c# xamarin sqlite-net sqlite-net-extensions

4
推荐指数
2
解决办法
4530
查看次数

哪些 SQLite.Net.Attributes 会忽略对象字段作为列?

我在通用 Windows 中使用 Sqlite 和SQLite.Net-PCL,并为以后的表创建编写了一个类:

[Table("my_tab")]
    public class MyObj
    {
        [PrimaryKey, Column("column1")]
        public string myObjField1 { get; set; }

        [Column("column2")]
        public string myObjField2 { get; set; }

        //???
        public string myObjField3 { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是如何忽略myObjField3作为表列?

c# sqlite-net win-universal-app

4
推荐指数
1
解决办法
2186
查看次数