Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions

Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions

Scheduled Pinned Locked Moved C#
databasequestionandroidmobilesqlite
52 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Exoskeletor

    also i tried in this code

    public static void AddTemplate(int category, List images)
    {
    var templateDB = new TemplateDB();
    var imageByteList = new List();

            foreach (int image in images)
            {
                //imageByteList.Add(templateDB.DrawableToByteArray(image));
            }
            var tmpl = new Template()
            {
                Category = category,
            };
            var img1 = new TemplateImage()
            {
                Category = category,
                Image = images\[0\],
                //ImagesHash = DatabaseHelper.GetMD5Hash(imageByteList\[0\]),
            };
            var img2 = new TemplateImage()
            {
                Category = category,
                Image = images\[1\],
                //ImagesHash = DatabaseHelper.GetMD5Hash(imageByteList\[1\]),
            };
            var img3 = new TemplateImage()
            {
                Category = category,
                Image = images\[2\],
                //ImagesHash = DatabaseHelper.GetMD5Hash(imageByteList\[2\]),
            };
            var img4 = new TemplateImage()
            {
                Category = category,
                Image = images\[3\],
                //ImagesHash = DatabaseHelper.GetMD5Hash(imageByteList\[3\]),
            };
            var img5 = new TemplateImage()
            {
                Category = category,
                Image = images\[4\],
                //ImagesHash = DatabaseHelper.GetMD5Hash(imageByteList\[4\]),
            };
            tmpl.TemplateImages = new List() { img1, img2, img3, img4, img5 };
            //tmpl.ImagesHash = DatabaseHelper.GetMD5Hash(ConcatByteList(imageByteList));
            var result = DatabaseHelper.db().Query("Select \* from Templates where ImagesHash=?", tmpl.ImagesHash);
            if (result.Count == 0)
            {
            DatabaseHelper.db().InsertAll(tmpl.TemplateImages);
            DatabaseHelper.db().Insert(tmpl);
            DatabaseHelper.db().UpdateWithChildren(tmpl);
            }
    

    to replace insert with insertorupdate and my template database was empty. id was always 0 in every attemp to insert something to db, do you know why? here is the database structure

    [Table("Templates")]
    public class Template
    {
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public int Category { get; set; }
    //[TextBlob("imagesBlobbed")

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #42

    Not quite sure what the code in the DatabaseHelper does, but it looks like you're executing a SELECT statement, not an INSERT.

    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

    E 1 Reply Last reply
    0
    • L Lost User

      Not quite sure what the code in the DatabaseHelper does, but it looks like you're executing a SELECT statement, not an INSERT.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

      E Offline
      E Offline
      Exoskeletor
      wrote on last edited by
      #43

      With insert the database works but on every app run it reenters the data. with insertorupdate nothing is insert in the database, i read it 2-3 times the page but i cant understand what i'm doing wrong

      L 1 Reply Last reply
      0
      • L Lost User

        That's reading all the images and making one big array of bytes of it; may be a bit much for the memorymanager.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

        E Offline
        E Offline
        Exoskeletor
        wrote on last edited by
        #44

        insert or update and insert or replace doesnt work with autoincrement fields [^] so i guess again i'm stuck in generating my own guid and use them, or i should run insertorupdate if the database already exist while manually giving the id's starting from Id 1. guid sounds better idea

        L 1 Reply Last reply
        0
        • E Exoskeletor

          With insert the database works but on every app run it reenters the data. with insertorupdate nothing is insert in the database, i read it 2-3 times the page but i cant understand what i'm doing wrong

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #45

          You're not recreating the database/overwriting it each time it runs, I hope?

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

          E 1 Reply Last reply
          0
          • E Exoskeletor

            insert or update and insert or replace doesnt work with autoincrement fields [^] so i guess again i'm stuck in generating my own guid and use them, or i should run insertorupdate if the database already exist while manually giving the id's starting from Id 1. guid sounds better idea

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #46

            Nothing against Guids; and the order in which the inserts are done is usually not guaranteed, so an autoincrement-assignment may not always be in the exact same order.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

            E 1 Reply Last reply
            0
            • L Lost User

              You're not recreating the database/overwriting it each time it runs, I hope?

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

              E Offline
              E Offline
              Exoskeletor
              wrote on last edited by
              #47

              thats why im trying to check if the entry exist, in order to not recreate it. this is the whole point of my question. how to create data once and only check if it exist in the next run. i just show that guid's doesnt change forever, on the same project, so i will use this, i will mark it as unique in the db structure, and everything will work amazingly good i hope :) Is there any other way of checking if data already exist? i dont like the idea of checking if the table exist, what if only the half data have been created and after that the app crash? what is the proper way to verify and create data in a database?

              L 1 Reply Last reply
              0
              • L Lost User

                Nothing against Guids; and the order in which the inserts are done is usually not guaranteed, so an autoincrement-assignment may not always be in the exact same order.

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                E Offline
                E Offline
                Exoskeletor
                wrote on last edited by
                #48

                what is usually the correct way to check first and then only create data if it is necessary? i have read somewhere that you can see if the table exist, or the table count, but this approach seems so poor to me, what do you say , what is the best way?

                L 1 Reply Last reply
                0
                • E Exoskeletor

                  what is usually the correct way to check first and then only create data if it is necessary? i have read somewhere that you can see if the table exist, or the table count, but this approach seems so poor to me, what do you say , what is the best way?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #49

                  I usually perform a select (on the pk) to verify if it exists, and if it doesn't, execute an insert; whether that's the "best" idea is debatable. I'm trying to stay away from parts of SQL that aren't part of the SQL92 spec, making it easier to move queries between different types of databases. Another little benefit is that I can use most databases to test any queries, since most support the SQL92 standard. If your code is designed specifically for SQLite, then it makes little sense to limit yourself and not use the options that the database gives you. There's an "upsert" command for example (SQLite Query Language: upsert[^]), combining insert and update (in a somwhat similar way to "insert or replace").

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                  E 1 Reply Last reply
                  0
                  • E Exoskeletor

                    thats why im trying to check if the entry exist, in order to not recreate it. this is the whole point of my question. how to create data once and only check if it exist in the next run. i just show that guid's doesnt change forever, on the same project, so i will use this, i will mark it as unique in the db structure, and everything will work amazingly good i hope :) Is there any other way of checking if data already exist? i dont like the idea of checking if the table exist, what if only the half data have been created and after that the app crash? what is the proper way to verify and create data in a database?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #50

                    Most of what I wrote was based on the (wrong) assumption that you were trying to compare images :D

                    Exoskeletor wrote:

                    i dont like the idea of checking if the table exist,

                    The table should always exist; don't create them on the fly, define them beforehand.

                    Exoskeletor wrote:

                    what if only the half data have been created and after that the app crash? what is the proper way to verify and create data in a database?

                    If you perform multiple inserts, you'll find that SQLite "feels" slow. That's where transactions come in - before the first insert, you start a transaction, and after the last one, you commit the transaction (or roll it back if there was an error). The transaction will be treated as a single statement, and only be written once committed. That way you can ensure all the stuff is there that you want.

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                    1 Reply Last reply
                    0
                    • L Lost User

                      I usually perform a select (on the pk) to verify if it exists, and if it doesn't, execute an insert; whether that's the "best" idea is debatable. I'm trying to stay away from parts of SQL that aren't part of the SQL92 spec, making it easier to move queries between different types of databases. Another little benefit is that I can use most databases to test any queries, since most support the SQL92 standard. If your code is designed specifically for SQLite, then it makes little sense to limit yourself and not use the options that the database gives you. There's an "upsert" command for example (SQLite Query Language: upsert[^]), combining insert and update (in a somwhat similar way to "insert or replace").

                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                      E Offline
                      E Offline
                      Exoskeletor
                      wrote on last edited by
                      #51

                      Great friend, with your help im executing a transaction and if it is completed i save this state somewhere. and to check if db exist and not to recreated i have made this code that works great,im also saving the current database version as int in order to easily do more updates in the future. Now all i have to do is call GetTemplateById or GetAllTemplates and if any creation is required will happen :) :)

                          public bool FirstRun { get; set; } = true;
                          public int DatabaseCreatedVersionOf { get; set; } = -1;
                      
                          public override void OnCreate()
                          {
                              base.OnCreate();
                          }
                      
                          public void UpdateDatabaseCreatedVersion()
                          {
                              DatabaseCreatedVersionOf = Preferences.Get(Settings.DatabaseCreatedVersionOfKey, 
                                 Settings.DatabaseCreatedVersionOfDefault);
                          }
                      
                          public void CreateTemplateDB()
                          {
                              UpdateDatabaseCreatedVersion();
                              if (DatabaseCreatedVersionOf == -1)
                                  TemplateDB.CreateDB();
                              FirstRun = false;
                          }
                          public Template GetTemplateById(int id)
                          {
                              if (FirstRun)
                              {
                                  CreateTemplateDB();
                                  FirstRun = false;
                              }
                      
                              return TemplateDB.GetTemplate(id);
                          }
                          public List
                      
                      L 1 Reply Last reply
                      0
                      • E Exoskeletor

                        Great friend, with your help im executing a transaction and if it is completed i save this state somewhere. and to check if db exist and not to recreated i have made this code that works great,im also saving the current database version as int in order to easily do more updates in the future. Now all i have to do is call GetTemplateById or GetAllTemplates and if any creation is required will happen :) :)

                            public bool FirstRun { get; set; } = true;
                            public int DatabaseCreatedVersionOf { get; set; } = -1;
                        
                            public override void OnCreate()
                            {
                                base.OnCreate();
                            }
                        
                            public void UpdateDatabaseCreatedVersion()
                            {
                                DatabaseCreatedVersionOf = Preferences.Get(Settings.DatabaseCreatedVersionOfKey, 
                                   Settings.DatabaseCreatedVersionOfDefault);
                            }
                        
                            public void CreateTemplateDB()
                            {
                                UpdateDatabaseCreatedVersion();
                                if (DatabaseCreatedVersionOf == -1)
                                    TemplateDB.CreateDB();
                                FirstRun = false;
                            }
                            public Template GetTemplateById(int id)
                            {
                                if (FirstRun)
                                {
                                    CreateTemplateDB();
                                    FirstRun = false;
                                }
                        
                                return TemplateDB.GetTemplate(id);
                            }
                            public List
                        
                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #52

                        Good to hear it works; you're welcome, and I'll be more careful in the future to make sure I understand the question :D

                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups