/// <summary> /// Constructs a new SQLiteConnection and opens a SQLite database specified by databasePath. /// </summary> /// <param name="databasePath"> /// Specifies the path to the database file. /// </param> /// <param name="storeDateTimeAsTicks"> /// Specifies whether to store DateTime properties as ticks (true) or strings (false). You /// absolutely do want to store them as Ticks in all new projects. The default of false is /// only here for backwards compatibility. There is a *significant* speed advantage, with no /// down sides, when setting storeDateTimeAsTicks = true. /// </param> public SQLiteConnection(string databasePath, SQLiteOpenFlags openFlags, bool storeDateTimeAsTicks = false) { DatabasePath = databasePath; #if NETFX_CORE SQLite3.SetDirectory(/*temp directory type*/ 2, Windows.Storage.ApplicationData.Current.TemporaryFolder.Path); #endif IntPtr handle; #if SILVERLIGHT || USE_CSHARP_SQLITE var r = SQLite3.Open(databasePath, out handle, (int)openFlags, IntPtr.Zero); #else // open using the byte[] // in the case where the path may include Unicode // force open to using UTF-8 using sqlite3_open_v2 var databasePathAsBytes = GetNullTerminatedUtf8(DatabasePath); var r = SQLite3.Open(databasePathAsBytes, out handle, (int)openFlags, IntPtr.Zero); #endif Handle = handle; if (r != SQLite3.Result.OK) { throw SQLiteException.New(r, String.Format("Could not open database file: {0} ({1})", DatabasePath, r)); } _open = true; StoreDateTimeAsTicks = storeDateTimeAsTicks; BusyTimeout = TimeSpan.FromSeconds(0.1); }
public void Close() { if (_open && Handle != NullHandle) { try { if (_mappings != null) { foreach (var sqlInsertCommand in _mappings.Values) { sqlInsertCommand.Dispose(); } } var r = SQLite3.Close(Handle); if (r != SQLite3.Result.OK) { string msg = SQLite3.GetErrmsg(Handle); throw SQLiteException.New(r, msg); } } finally { Handle = NullHandle; _open = false; } } }
public int ExecuteNonQuery() { if (_conn.Trace) { Debug.WriteLine("Executing: " + this); } var r = SQLite3.Result.OK; var stmt = Prepare(); r = SQLite3.Step(stmt); Finalize(stmt); if (r == SQLite3.Result.Done) { int rowsAffected = SQLite3.Changes(_conn.Handle); return(rowsAffected); } else if (r == SQLite3.Result.Error) { string msg = SQLite3.GetErrmsg(_conn.Handle); throw SQLiteException.New(r, msg); } else { return(0); //throw SQLiteException.New (r, r.ToString ()); } }
public static Sqlite3Statement Prepare2(Sqlite3DatabaseHandle db, string query) { Sqlite3Statement stmt = default(Sqlite3Statement); #if USE_WP8_NATIVE_SQLITE var r = Sqlite3.sqlite3_prepare_v2(db, query, out stmt); #else stmt = new Sqlite3Statement(); var r = Sqlite3.sqlite3_prepare_v2(db, query, -1, ref stmt, 0); #endif if (r != 0) { throw SQLiteException.New((Result)r, GetErrmsg(db)); } return(stmt); }
/// <summary> /// Creates a new SQLiteCommand given the command text with arguments. Place a '?' /// in the command text for each of the arguments. /// </summary> /// <param name="cmdText"> /// The fully escaped SQL. /// </param> /// <param name="args"> /// Arguments to substitute for the occurences of '?' in the command text. /// </param> /// <returns> /// A <see cref="SQLiteCommand"/> /// </returns> public SQLiteCommand CreateCommand(string cmdText, params object[] ps) { if (!_open) { throw SQLiteException.New(SQLite3.Result.Error, "Cannot create commands from unopened database"); } else { var cmd = NewCommand(); cmd.CommandText = cmdText; foreach (var o in ps) { cmd.Bind(o); } return(cmd); } }
public int ExecuteNonQuery(object[] source) { if (Connection.Trace) { Debug.WriteLine("Executing: " + CommandText); } var r = SQLite3.Result.OK; if (!Initialized) { Statement = Prepare(); Initialized = true; } //bind the values. if (source != null) { for (int i = 0; i < source.Length; i++) { SQLiteCommand.BindParameter(Statement, i + 1, source [i], Connection.StoreDateTimeAsTicks); } } r = SQLite3.Step(Statement); if (r == SQLite3.Result.Done) { int rowsAffected = SQLite3.Changes(Connection.Handle); SQLite3.Reset(Statement); return(rowsAffected); } else if (r == SQLite3.Result.Error) { string msg = SQLite3.GetErrmsg(Connection.Handle); SQLite3.Reset(Statement); throw SQLiteException.New(r, msg); } else { SQLite3.Reset(Statement); throw SQLiteException.New(r, r.ToString()); } }