|
but how can sqlite know to replace a record? both my tables have a primary unique key
[Table("Templates")]
public class Template
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public int Category { get; set; }
[OneToMany, Unique]
public List<TemplateImage> TemplateImages { get; set; }
public string ImagesHash { get; set; }
}
[Table("TemplateImages")]
public class TemplateImage
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public int Category { get; set; }
public string ImagesHash { get; set; }
public int Image { get; set; }
[ForeignKey(typeof(Template))]
public int TemplateId { get; set; }
}
doesnt that make them unique.
Also, this is replacing items according to the primary key it says? so if i have a template with id 5, and i delete it one day, the template that belong to id 6 wll go to the position of id 5? and so on?
|
|
|
|
|
also i tried in this code
public static void AddTemplate(int category, List<int> images)
{
var templateDB = new TemplateDB();
var imageByteList = new List<byte[]>();
foreach (int image in images)
{
}
var tmpl = new Template()
{
Category = category,
};
var img1 = new TemplateImage()
{
Category = category,
Image = images[0],
};
var img2 = new TemplateImage()
{
Category = category,
Image = images[1],
};
var img3 = new TemplateImage()
{
Category = category,
Image = images[2],
};
var img4 = new TemplateImage()
{
Category = category,
Image = images[3],
};
var img5 = new TemplateImage()
{
Category = category,
Image = images[4],
};
tmpl.TemplateImages = new List<TemplateImage>() { img1, img2, img3, img4, img5 };
var result = DatabaseHelper.db().Query<TemplateImage>("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; }
[OneToMany, Unique]
public List<TemplateImage> TemplateImages { get; set; }
public string ImagesHash { get; set; }
}
[Table("TemplateImages")]
public class TemplateImage
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public int Category { get; set; }
public string ImagesHash { get; set; }
public int Image { get; set; }
[ForeignKey(typeof(Template))]
public int TemplateId { get; set; }
}
|
|
|
|
|
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
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
|
|
|
|
|
You're not recreating the database/overwriting it each time it runs, I hope?
Bastard Programmer from Hell
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
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
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.
|
|
|
|
|
The page that I linked to lets you try the SQL statements online
Hadn't seen that it is a real "replace", not an insert "or" update; but it explains that too. It does look like it honors the unique constraint, so should work with a primary key too.
Exoskeletor wrote: so if i have a template with id 5, and i delete it one day, the template that belong to id 6 wll go to the position of id 5? and so on? Id's that have been used already do not get re-used; so if you delete 5 and 6 exists, another insert would give you 7, not 5, even if that position is empty.
Bastard Programmer from Hell
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.
|
|
|
|
|
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
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
|
|
|
|
|
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
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?
|
|
|
|
|
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
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<Template> GetAllTemplates()
{
if (FirstRun)
{
CreateTemplateDB();
FirstRun = false;
}
return TemplateDB.GetAllTemplates();
}
|
|
|
|
|
Good to hear it works; you're welcome, and I'll be more careful in the future to make sure I understand the question
Bastard Programmer from Hell
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.
|
|
|
|
|
Exoskeletor wrote: Im starting thinking that this is a very silly idea because i guess the Resource id is generated every time i compile a new version of my app, so im trying to create a unique hash of something that is not unique You don't want to hash it's id, but the data that represents the image You want to compare those to each other, not the Id's that identify the image.
Bastard Programmer from Hell
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 think i have an idea of how im going to do it, i can do something like this:
public static string GetMD5Hash(string text)
{
using ( var md5 = MD5.Create() )
{
byte[] computedHash = md5.ComputeHash( Encoding.UTF8.GetBytes(text) );
return new System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary(computedHash).ToString();
}
}
var nums = new List<int> {1, 2, 3};
var result = string.Join(", ", nums);
var hash = GetMD5Hash(result);
|
|
|
|
|
There is this code that have a name in the table data and I have been asked to make that data red. Right now it is bold (strong). I need to add code to also make it appear read. How do I do this?
Here is the code in C#
<ItemTemplate>
<%#
Eval("AttorneyPartyID").ToString() != Eval("PartyID").ToString() ?
"<table><tr><td>" + Eval("AttorneyFullName") + "</td></tr>"
:
"Pro Se"
%>
</ItemTemplate>
|
|
|
|
|
Why are you inserting a single-cell table? Just wrap the value in a <span> or a <div> , and set a suitable CSS class on it.
You'll also need to HTML encode the value from the database, just in case.
<ItemTemplate>
<%#
Eval("AttorneyPartyID", "{0}") != Eval("PartyID", "{0}")
? "<div class=\"attorney-full-name\">" + HttpUtility.HtmlEncode(Eval("AttorneyFullName", "{0}")) + "</div>"
: "Pro Se"
%>
</ItemTemplate>
.attorney-full-name {
color: #a00;
font-weight: bold;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
It is not a single-cell. I did not paste the whole table (ItemTemplate). The table has 5 rows and each row has a single (cell)
Here is the whole table
<asp:TemplateField HeaderText="Attorney" HeaderStyle-HorizontalAlign="Justify" ItemStyle-Width="25%" >
<ItemTemplate>
<%#
Eval("AttorneyPartyID").ToString() != Eval("PartyID").ToString() ?
"<table><tr><td>" + Eval("AttorneyFullName") + "</td></tr>"
:
"Pro Se"
%>
<%#
Eval("AttorneyPartyID").ToString() != Eval("PartyID").ToString() && Eval("AttorneyPhoneNumber") != "" && Eval("AttorneyPhoneNumber") != null ?
"<tr><td>Phone:" + ' ' + Eval("AttorneyPhoneNumber") + "</td></tr>"
:
string.Empty
%>
<%#
Eval("AttorneyPartyID").ToString() != Eval("PartyID").ToString() && Eval("AttorneyEmail") != "" && Eval("AttorneyEmail") != null ?
"<tr><td>Email:" + Eval("AttorneyEmail") + "</td></tr>"
:
string.Empty
%>
<%#
Eval("AttorneyPartyID").ToString() != Eval("PartyID").ToString() ?
"</table>"
:
string.Empty
%>
</ItemTemplate>
</asp:TemplateField>
|
|
|
|
|
You still need to HTML-encode the database values. And the solution to your original question is still the same: add a CSS class to the parent element of the text you want to style.
I also think you can tidy that code up somewhat, and avoid having to build the HTML as a string:
<asp:TemplateField HeaderText="Attorney" HeaderStyle-HorizontalAlign="Justify" ItemStyle-Width="25%">
<ItemTemplate>
<asp:MultiView runat="server" ActiveViewIndex='<%# Eval("AttorneyPartyID", "{0}") != Eval("PartyID", "{0}") ? 0 : 1 %>'>
<asp:View runat="server">
<table>
<tr>
<td class="attorney-full-name"><%#: Eval("AttorneyFullName") %></td>
</tr>
<asp:Placeholder runat="server" visible='<%# !string.IsNullOrEmpty(Eval("AttorneyPhoneNumber", "{0}")) %>'>
<tr>
<td class="attorney-phone-number">Phone: <%#: Eval("AttorneyPhoneNumber") %></td>
</tr>
</asp:Placeholder>
<asp:Placeholder runat="server" visible='<%# !string.IsNullOrEmpty(Eval("AttorneyEmail", "{0}")) %>'>
<tr>
<td class="attorney-email">Email: <%#: Eval("AttorneyEmail") %></td>
</tr>
</asp:Placeholder>
</table>
</asp:View>
<asp:View runat="server">
Pro Se
</asp:View>
</asp:MultiView>
</ItemTemplate>
</asp:TemplateField> NB: Using <%#: ... %> instead of <%# ... %> - note the extra : at the start - will automatically HTML-encode the output for you.
MultiView Class (System.Web.UI.WebControls) | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank for your help on trying to figure this out. I did add css class to existing css file.
.inactiveAttorney
{
color: #AD0000;
font-weight:bold
}
The product owner have asked me to only make the attorney name red when fActive = 0 (false). The code behind have this code
private bool _fActive;
public bool fActive
{
get { return this._fActive; }
set { this._fActive = value; }
}
this._fActive = parsefActive == true ? true : false;
I have successfully been able to use css class to make attorney name red. However, I need help to add a condition so that the name is red only when fActive =false (0).
Here is my code which is working and needs adding condition
"<table><tr><td class='inactiveAttorney'>" + Eval("AttorneyFullName") + "</td></tr></table>"
The fActive is a Boolean and else where in the code I see something done when a Boolean is true. I need to do something similar but I do not know how.
Here is that example.
<tr><td><%# (bool)Eval("PartyConfidentialHomePhone") == true ? "<img src=\"../../Images/YellowLock_16x16.png\" alt=\"Home Phone Secured\" />":"" %><%#Eval("PartyHomePhone").ToString() != "" && Eval("PartyHomePhone") != null ? "" + Eval("PartyHomePhone") + "":string.Empty%></td></tr>
modified 9-Mar-20 13:29pm.
|
|
|
|
|
Try:
"<table><tr><td class='" + (fActive ? "activeAttorney" : "inactiveAttorney") + "'>" + Eval("AttorneyFullName") + "</td></tr></table>"
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Homer,
Thanks for your reply. When I tried the code you gave me, I am getting an error The name "fActive" does not exist in the current context
|
|
|
|
|
Then the code-behind of the page or user control where the data-binding is taking place doesn't have the property you showed in your previous message.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|