// update a row void UpdateRow(string id, string name) { // list of data to be updated List <SQLiteDB.DB_DataPair> dataList = new List <SQLiteDB.DB_DataPair> (); // data to be updated SQLiteDB.DB_DataPair data = new SQLiteDB.DB_DataPair(); data.fieldName = "Name"; data.value = name; dataList.Add(data); // row to be updated SQLiteDB.DB_ConditionPair condition = new SQLiteDB.DB_ConditionPair(); condition.fieldName = "Id"; condition.value = id; condition.condition = SQLiteDB.DB_Condition.EQUAL_TO; int i = db.Update("Users", dataList, condition); if (i > 0) { Debug.Log(i + " Record Updated!"); _name = ""; _id = ""; Refresh(); } }
// add a single entry in database void AddRow(string id, string name) { List <SQLiteDB.DB_DataPair> dataPairList = new List <SQLiteDB.DB_DataPair>(); SQLiteDB.DB_DataPair data = new SQLiteDB.DB_DataPair(); // Insert first row // first field data.fieldName = "Id"; data.value = id; dataPairList.Add(data); // second field data.fieldName = "Name"; data.value = name; dataPairList.Add(data); // insert into Users - first row int i = db.Insert("Users", dataPairList); if (i > 0) { Debug.Log("Record Inserted!"); _name = ""; _id = ""; Refresh(); } }
//public IEnumerable<T> Query(Expression<Func<T, bool>> queryPredicate) //{ //} public bool Insert(T item) { bool successful = false; try { List <SQLiteDB.DB_DataPair> dataPairList = new List <SQLiteDB.DB_DataPair>(); SQLiteDB.DB_DataPair data = new SQLiteDB.DB_DataPair(); LogHelper.Log(LoggingLevel.TRACE, _config, "Creating new " + typeof(T).Name); foreach (var property in _itemProperties) { if (property.GetCustomAttributes(typeof(ColumnIgnoreAttribute), false).Length == 0) { // If the property is marked as required and is null, throw an exception if (property.GetCustomAttributes(typeof(RequiredAttribute), false).Length != 0 && //!property.PropertyType.IsValueType && property.GetValue(item) == null) { throw new RequiredFieldException(property.Name); } string fieldName = property.Name; var fieldNameAttribute = property.GetCustomAttributes(typeof(FieldNameAttribute), false).FirstOrDefault() as FieldNameAttribute; if (fieldNameAttribute != null) { // cleanse by replacing all spaces with underscores // TODO rip out all symbols and other invalid characters fieldName = fieldNameAttribute.FieldName.Replace(" ", "_"); } data.fieldName = fieldName; data.value = ConvertValueToString(item, property); dataPairList.Add(data); LogHelper.Log(LoggingLevel.TRACE, _config, "Added {" + data.fieldName + ", " + data.value + "} to object."); } } int changeCount = _db.Insert(_tableIdentifier, dataPairList); LogHelper.Log(LoggingLevel.LOG, _config, changeCount + " change(s) on insert to " + _tableIdentifier); if (changeCount > 0) { successful = true; } } catch (Exception ex) { LogHelper.Log(LoggingLevel.ERROR, _config, ex.ToString()); } return(successful); }
public bool Update(T item) { bool successful = false; List <SQLiteDB.DB_DataPair> dataPairList = new List <SQLiteDB.DB_DataPair>(); SQLiteDB.DB_DataPair data = new SQLiteDB.DB_DataPair(); SQLiteDB.DB_ConditionPair condition = new SQLiteDB.DB_ConditionPair(); foreach (var property in _itemProperties) { // if the property isn't being ignored if (property.GetCustomAttributes(typeof(ColumnIgnoreAttribute), false).Length == 0) { // if isn't primary key if (property.GetCustomAttributes(typeof(PrimaryKeyAttribute), false).Length == 0) { data.fieldName = property.Name; data.value = ConvertValueToString(item, property); dataPairList.Add(data); } // else is, it can't be updated but instead used for finding record to update else { condition.fieldName = property.Name; condition.value = ConvertValueToString(item, property); condition.condition = SQLiteDB.DB_Condition.EQUAL_TO; } } } if (_db.Update(_tableIdentifier, dataPairList, condition) > 0) { successful = true; } return(successful); }