Получить данные из sqlite net с параметрами и BLOB-объектами - PullRequest
2 голосов
/ 07 марта 2020

Я использую расширения sqlite- net и sqlite- net для большого двоичного объекта в проекте Xamarin. Android. У меня есть эта схема:

    public enum TemplateCategories
    {
        Emojis,
        Stars,
        Hearts,
        Characters,
        Emotions,
    }

public class Templates
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public int Category { get; set; }
    [TextBlob("imagesBlobbed")]
    public List<int> Images { get; set; }
    public string imagesBlobbed { get; set; }
}

, и я пытаюсь добавить объект шаблонов с этим:

    public static void CreateDB(SQLiteConnection db)
    {
        db.CreateTable<Templates>();
        AddTemplate(
            db,
            (int)TemplateCategories.Emojis,
            new List<int>
            {
            Resource.Drawable.ic_angry,
            Resource.Drawable.ic_sad,
            Resource.Drawable.ic_neutral,
            Resource.Drawable.ic_happy,
            Resource.Drawable.ic_smiling,
            }
        );

    public static void AddTemplate(SQLiteConnection db, int category, List<int> images)
    {
        var alreadyExist = db.Table<Templates>().Where(record => record.Images == images);

        if (alreadyExist?.Count() == 0)
        {
            var template = new Templates()
            {
                Category = category,
                Images = images,
            };
            db.InsertWithChildren(template, recursive: true);
        }
    }

, но на AddTemplate я получаю исключение нулевой ссылки в строке var уже Exist . Я посмотрел документацию, и я не уверен, как я могу найти определенный c элемент ( Список BLOB в моем случае) и получить его.

Я просто хочу быть Сначала убедитесь, что данные, которые я вставляю, еще не находятся в БД.

Мой подход неверен? Поскольку я хочу иметь возможность запрашивать список изображений, должен ли я создать связь OneToMany с другой таблицей, которая будет содержать изображения вместо BLOB-объекта?

Исключение:

**System.NullReferenceException:** 'Object reference not set to an instance of an object.'

03-08 00:38:28.681 D/Mono    (20388): DllImport attempting to load: '/system/lib/liblog.so'.
03-08 00:38:28.682 D/Mono    (20388): DllImport loaded library '/system/lib/liblog.so'.
03-08 00:38:28.682 D/Mono    (20388): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so').
03-08 00:38:28.682 D/Mono    (20388): Searching for '__android_log_print'.
03-08 00:38:28.682 D/Mono    (20388): Probing '__android_log_print'.
03-08 00:38:28.682 D/Mono    (20388): Found as '__android_log_print'.
03-08 00:38:28.686 I/MonoDroid(20388): UNHANDLED EXCEPTION:
03-08 00:38:28.690 I/MonoDroid(20388): System.NullReferenceException: Object reference not set to an instance of an object.
03-08 00:38:28.690 I/MonoDroid(20388):   at SQLite.TableQuery`1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List`1[T] queryArgs) [0x008b5] in <84b9c9e630fa45bd8ac799333976ebbf>:0 
03-08 00:38:28.690 I/MonoDroid(20388):   at SQLite.TableQuery`1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List`1[T] queryArgs) [0x0009d] in <84b9c9e630fa45bd8ac799333976ebbf>:0 
03-08 00:38:28.690 I/MonoDroid(20388):   at SQLite.TableQuery`1[T].GenerateCommand (System.String selectionList) [0x0005f] in <84b9c9e630fa45bd8ac799333976ebbf>:0 
03-08 00:38:28.690 I/MonoDroid(20388):   at SQLite.TableQuery`1[T].Count () [0x00000] in <84b9c9e630fa45bd8ac799333976ebbf>:0 
03-08 00:38:28.690 I/MonoDroid(20388):   at RateMe.Helpers.TemplateDB.AddTemplate (SQLite.SQLiteConnection db, System.Int32 category, System.Collections.Generic.List`1[T] images) [0x00077] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\Helpers\TemplateDB.cs:25 
03-08 00:38:28.690 I/MonoDroid(20388):   at RateMe.Helpers.TemplateDB.CreateDB (SQLite.SQLiteConnection db) [0x00009] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\Helpers\TemplateDB.cs:68 
03-08 00:38:28.690 I/MonoDroid(20388):   at RateMe.MainApplication.GetTemplateById (System.Int32 id) [0x00025] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\MainApplication.cs:43 
03-08 00:38:28.690 I/MonoDroid(20388):   at RateMe.MainActivity.SetButtonImages () [0x0008c] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\MainActivity.cs:554 
03-08 00:38:28.690 I/MonoDroid(20388):   at RateMe.MainActivity.OnResume () [0x0000f] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\MainActivity.cs:507 
03-08 00:38:28.690 I/MonoDroid(20388):   at Android.App.Activity.n_OnResume (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <985f5179c1dd4b3b98fb1b8dcc1ee9be>:0 
03-08 00:38:28.690 I/MonoDroid(20388):   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.13(intptr,intptr)
03-08 00:38:28.707 E/AppCenterCrashes(20388): Unhandled Exception from source=AndroidEnvironment
03-08 00:38:28.707 E/AppCenterCrashes(20388): System.NullReferenceException: Object reference not set to an instance of an object.
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at SQLite.TableQuery`1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List`1[T] queryArgs) [0x008b5] in <84b9c9e630fa45bd8ac799333976ebbf>:0 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at SQLite.TableQuery`1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List`1[T] queryArgs) [0x0009d] in <84b9c9e630fa45bd8ac799333976ebbf>:0 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at SQLite.TableQuery`1[T].GenerateCommand (System.String selectionList) [0x0005f] in <84b9c9e630fa45bd8ac799333976ebbf>:0 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at SQLite.TableQuery`1[T].Count () [0x00000] in <84b9c9e630fa45bd8ac799333976ebbf>:0 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at RateMe.Helpers.TemplateDB.AddTemplate (SQLite.SQLiteConnection db, System.Int32 category, System.Collections.Generic.List`1[T] images) [0x00077] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\Helpers\TemplateDB.cs:25 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at RateMe.Helpers.TemplateDB.CreateDB (SQLite.SQLiteConnection db) [0x00009] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\Helpers\TemplateDB.cs:68 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at RateMe.MainApplication.GetTemplateById (System.Int32 id) [0x00025] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\MainApplication.cs:43 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at RateMe.MainActivity.SetButtonImages () [0x0008c] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\MainActivity.cs:554 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at RateMe.MainActivity.OnResume () [0x0000f] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\MainActivity.cs:507 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at Android.App.Activity.n_OnResume (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <985f5179c1dd4b3b98fb1b8dcc1ee9be>:0 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.13(intptr,intptr)
03-08 00:38:28.714 D/Mono    (20388): Loading reference 5 of /storage/emulated/0/Android/data/com.cdrosos.simplecustomerfeedback/files/.__override__/Microsoft.AppCenter.Crashes.dll asmctx DEFAULT, looking for System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
03-08 00:38:28.714 D/Mono    (20388): Assembly Ref addref Microsoft.AppCenter.Crashes[0x9d908ac0] -> System.Core[0x872b3280]: 8
03-08 00:38:28.744 D/Mono    (20388): DllImport searching in: '__Internal' ('(null)').
03-08 00:38:28.744 D/Mono    (20388): Searching for 'java_interop_jnienv_new_local_ref'.
03-08 00:38:28.744 D/Mono    (20388): Probing 'java_interop_jnienv_new_local_ref'.
03-08 00:38:28.744 D/Mono    (20388): Found as 'java_interop_jnienv_new_local_ref'.
03-08 00:38:28.782 W/zygote  (20388): JNI RegisterNativeMethods: attempt to register 0 native methods for android.runtime.JavaProxyThrowable
03-08 00:38:28.786 D/Mono    (20388): DllImport searching in: '__Internal' ('(null)').
03-08 00:38:28.786 D/Mono    (20388): Searching for 'java_interop_jnienv_throw'.
03-08 00:38:28.786 D/Mono    (20388): Probing 'java_interop_jnienv_throw'.
03-08 00:38:28.786 D/Mono    (20388): Found as 'java_interop_jnienv_throw'.
**System.NullReferenceException:** 'Object reference not set to an instance of an object.'

03-08 00:38:30.941 E/mono-rt (20388): [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object.
03-08 00:38:30.941 E/mono-rt (20388):   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.13(intptr,intptr)
03-08 00:38:30.941 E/mono-rt (20388):   at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.13(intptr,intptr)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...