Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions
-
Not quite sure what the code in the
DatabaseHelper
does, but it looks like you're executing aSELECT
statement, not anINSERT
.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.
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
-
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.
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
-
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
-
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
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.
-
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?
-
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.
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?
-
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?
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.
-
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?
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.
-
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.
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
-
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
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.