I currently have a method to see what type of object an input is and create a SQL input based on it, Like so:
private static string PropertyToDBString(object o)
{
Debug.Log(o.GetType());
if (o == typeof(System.String) || o == typeof(string))
return "'" + o.ToString() + "'";
else if (o == typeof(System.Boolean) || o == typeof(bool))
return ((System.Boolean)o) ? "1" : "0";
return "'" + o.ToString() + "'";
}
But this does not seem to work, Everything returns as .toString() based, Boolean return as True/False, but the log is picking up the type as system.boolean. Like so:
I'm using SQLite and wondering if I should bother using proper data types, As limits don't exist and even the fact the boolean column is INT(1) on the DB but still stores True/False. Should I just use TEXT for everything.
I currently have a method to see what type of object an input is and create a SQL input based on it
Don't do that. Use parameterized SQL instead. You should not be trying to format your values for use in SQL. This is almost always going to be a route to SQL injection attacks.
Now, as to the problem itself... you're checking whether o
itself is typeof(bool)
- whereas I suspect you want to check whether o.GetType()
is typeof(bool)
.
Better yet, use the is
operator:
if (o is string)
{
return "'" + o + "'";
}
if (o is bool)
{
bool value = (bool) o;
return value ? "1" : "0";
}
// Are you sure you want this? I would throw an exception if you
// don't know what to do with the value...
return "'" + o + "'";
See more on this question at Stackoverflow