/// <summary>
        /// Insert an item into the table and return the primary key value created for this new item.
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public Int64 InsertItem(TItemType item)
        {
            Int64 result = -1;

            lock (Lock)
            {
                using (var statement = sqlConnection.Prepare(GetInsertItemSql()))
                {
                    this.FillInsertItemStatement(statement, item);
                    SQLiteResult output = statement.Step();

                    if (output != SQLiteResult.DONE)
                    {
                        string foo = sqlConnection.ErrorMessage();
                        throw new Exception(output.ToString());
                    }

                    result = (Int64)sqlConnection.LastInsertRowId();
                }
            }

            return(result);
        }
        public long Insert(string table, Dictionary <string, object> contentValues)
        {
            if (String.IsNullOrWhiteSpace(table) || contentValues == null || contentValues.Keys.Count == 0)
            {
                throw new InvalidOperationException("Must specify a table and provide content to insert");
            }
            string columns            = String.Join(", ", contentValues.Keys);
            var    valueBindingString = new StringBuilder();

            for (int i = 0, max = contentValues.Count; i < max; i++)
            {
                valueBindingString.Append("?");
                if ((i + 1) < max)
                {
                    valueBindingString.Append(", ");
                }
            }
            string sql = String.Format(InsertStatement,
                                       table,
                                       columns,
                                       valueBindingString);

            using (ISQLiteStatement stmt = _sqlConnection.Prepare(sql))
            {
                int count = 1;
                foreach (string key in contentValues.Keys)
                {
                    stmt.Bind(count, contentValues[key]);
                    count++;
                }
                var result = stmt.Step();
            }
            long insertId = _sqlConnection.LastInsertRowId();

            return(insertId);
        }
Esempio n. 3
0
 public long LastInsertRowId()
 {
     return(_connection.LastInsertRowId());
 }