public int ExecuteNonQuery() { int result = 0; for (int i = 0; i < _Statements.Count; ++i) { SQLiteStatement statement = _Statements[i]; statement.Compile(); _Connection.NativeMethods.busy_timeout(_Timeout); SQLiteCode res = statement.Step(); if (res == SQLiteCode.Done || res == SQLiteCode.RowReady) { statement.Reset(); result += _Connection.LastChangesCount; } else if (res == SQLiteCode.Error) { statement.Dispose(); // UnCompile will throw exception with appropriate msg. } else { throw new SQLiteException("Unknown SQLite error"); } } return(result); }
public void Open() { if (_State != ConnectionState.Closed) { throw new SQLiteException("The connection is not closed."); } if (File.Exists(_ConnectionStringBuilder.DataSource)) { if (_ConnectionStringBuilder.NewDatabase) { // Try to delete existing file try { File.Delete(_ConnectionStringBuilder.DataSource); File.Delete(String.Concat(_ConnectionStringBuilder.DataSource, "-journal")); } catch (IOException) { throw new SQLiteException("Cannot create new file, the existing file is in use."); } } } else { if (!_ConnectionStringBuilder.NewDatabase) { throw new SQLiteException(String.Format("File '{0}' does not exist. Use ConnectionString parameter NewDatabase=True to create new file or enter the full path name.", _ConnectionStringBuilder.DataSource)); } } _NativeMethods = new WindowsMethods(_ConnectionStringBuilder.Encoding); SQLiteCode res = _NativeMethods.Open(_ConnectionStringBuilder.DataSource); if (res != SQLiteCode.Ok) { try { if (_NativeMethods.ErrorCode() != SQLiteCode.Ok) { throw new SQLiteException(string.Format("Error opening database {0}.\r\n {1}", _ConnectionStringBuilder.DataSource, _NativeMethods.ErrorMessage())); } } finally { _NativeMethods.Dispose(); _NativeMethods = null; } } _Version = _NativeMethods.SQliteVersion(); _State = ConnectionState.Open; SQLiteCommand cmd = CreateCommand(string.Format("pragma synchronous = {0}", _ConnectionStringBuilder.SynchronousMode)); cmd.ExecuteNonQuery(); cmd = CreateCommand(string.Format("pragma cache_size = {0}", _ConnectionStringBuilder.CacheSize)); cmd.ExecuteNonQuery(); }
public void Dispose() { if (_StatementHandle != IntPtr.Zero) { SQLiteCode res = _NativeMethods.finalize(_StatementHandle); _StatementHandle = IntPtr.Zero; if (res != SQLiteCode.Ok && res != SQLiteCode.CallbackAbort) { throw new SQLiteException(string.Format("Error while finalize statement {0}.\r\n {1}", _CommandText, _NativeMethods.ErrorMessage())); } _Command.Connection.LastChangesCount = _NativeMethods.total_changes() - _PreviousTotalChangesCount; } }
public SQLiteCode Step() { if (_FirstStep) { _FirstStep = false; _PreviousTotalChangesCount = _NativeMethods.total_changes(); } SQLiteCode res = _NativeMethods.step(_StatementHandle); if (res == SQLiteCode.LibraryUsedIncorrectly || res == SQLiteCode.Busy) { throw new SQLiteException(string.Format("Step failed for {0}, ErrorCode {1}. ", _CommandText, _NativeMethods.ErrorCode())); } return(res); }
public bool Read() { if (_Command == null) { throw new NullReferenceException("The Command is null."); } EnsureInitialization(); if (_Done) { return(false); } if (_FirstRead) { _FirstRead = false; return(true); } SQLiteCode res = _Statement.Step(); if (res == SQLiteCode.RowReady) { return(true); } else if (res == SQLiteCode.Done) { _Done = true; } else if (res == SQLiteCode.Error) { _Statement.Dispose(); } else { throw new SQLiteException(string.Format("Unknown SQLite error code {0}.", res)); } return(false); }
private void ExecuteFirstStep() { _Done = true; _FirstRead = true; // Exec first step and get column info. _Statement = _Command.Statements[_StatementIndex]; _Statement.Compile(); _NativeMethods.busy_timeout(_Command.CommandTimeout); SQLiteCode res = _Statement.Step(); if (res == SQLiteCode.RowReady || res == SQLiteCode.Done) { _FieldNames = new String[_Statement.GetColumnCount()]; _FieldTypes = new Type[_Statement.GetColumnCount()]; _ColumnNameStartIndexes = new int[_Statement.GetColumnCount()]; // Get column info. for (int i = 0; i < _Statement.GetColumnCount(); i++) { _FieldNames[i] = _Statement.GetColumnName(i); _FieldTypes[i] = SQLType2Type(_Statement.GetColumnType(i)); _ColumnNameStartIndexes[i] = _FieldNames[i].IndexOf('.') + 1; } _Done = res == SQLiteCode.Done; } else if (res == SQLiteCode.Error) { _Statement.Dispose(); } else { throw new SQLiteException(string.Format("Unknown SQLite error code {0}.", res)); } }