【问题标题】:Xamarin and SQLite: fix a corrupt database and shrinkXamarin 和 SQLite:修复损坏的数据库并缩小
【发布时间】:2017-08-29 09:28:02
【问题描述】:

有没有办法从代码中缩小数据库?我在我的数据库中发现一些表已损坏:如何从代码中检测到并修复它?

当我尝试使用Execute 更新特定表中的记录时,我总是收到Corrupt 错误。

表的定义是

[Table("tbl_Colour")]
public class Colour : BaseTableListing {
}

public class BaseTableListing : ITableEntity
{
    [PrimaryKey, AutoIncrement]
    [Indexed]
    public int Id { get; set; } = 0;

    public string CreatedBy { get; set; } = "";

    public DateTime CreatedDate { get; set; } = DateTime.Now;

    // more fields
    // ...
}

查询是

UPDATE tbl_Colour SET IsUpdated = 0, 
SynchronizeDate=CAST((((JulianDay('2017-04-04 09:22:12', 'localtime') 
    - 2440587.5)*86400.0) + 62135596800) * 10000000 AS BIGINT) 
WHERE ID = 57;

提前谢谢你。

【问题讨论】:

    标签: ios sqlite xamarin.forms


    【解决方案1】:

    收缩sqlite文件的命令是VACUUMhttps://sqlite.org/lang_vacuum.html

    它会重建文件,所以也许它可以解决你的问题。

    【讨论】:

    • 感谢您的留言
    • 我找到了另一个命令integrity_check,但我不知道如何从CreateCommand 调用它
    【解决方案2】:

    如果您的数据库在您的解决方案或项目中,我的解决方案是 database

    /// <summary>
    /// Compacts the database.
    /// </summary>
    public void CompactDatabase()
    {
        try
        {
            SQLiteCommand cmd = database.CreateCommand("vacuum");
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Debug.WriteLine("CompactDatabase has an error: " + ex.Message);
    
            // if you use MobileCenter
            Analytics.TrackEvent("CompactDb Error", new Dictionary<string, string> {
                { "Message", ex.Message },
                { "Source", ex.Source },
                { "StackTrace", ex.StackTrace.ToString() }
            });
        }
    }
    

    【讨论】:

    • 没错,vacuum 是一个普通的命令,你可以像每个命令一样执行它。我认为它可以解决您的问题。
    猜你喜欢
    • 2016-08-24
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多