Forum > Databases
Replace substring in a field and update DB
WJSwanepoel:
Thanks, I shall try and battle thru it on my own.
Thaddy:
You can battle, but that will probably not work soon.
As promised:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Program sqlite_test_tdk;{$mode objfpc}{$H+}{$ifdef mswindows}{$apptype console}{$endif} Uses SysUtils, sqldb, sqlite3conn; Const db = 'test.db'; Var Conn: TSQLite3Connection; Query: TSQLQuery; Txn: TSQLTransaction; Id: integer = 2; TextField: string;BeginTry Conn := TSQLite3Connection.Create(Nil); Try Conn.DatabaseName := db; Try Txn := TSQLTransaction.Create(Nil); Try Conn.Transaction := Txn; If Not FileExists(db) Then Try Conn.Open; Conn.ExecuteDirect('CREATE TABLE "content" ( "topic_id" Integer NOT NULL PRIMARY KEY AUTOINCREMENT,"data" VARCHAR)'); Conn.ExecuteDirect('INSERT INTO "content" VALUES (1, "DATA 1")'); Conn.ExecuteDirect('INSERT INTO "content" VALUES (2, "DATA 2")'); Conn.ExecuteDirect('INSERT INTO "content" VALUES (3, "DATA 3")'); Txn.Commit; Conn.Close; Except On E: Exception Do writeln('Could not create database: ',db); End; Try Query := TSQLQuery.Create(Nil); Try Query.Database := Conn; Query.SQL.Text := 'SELECT topic_id, data FROM content WHERE topic_id = :id'; Query.ParamByName('Id').AsInteger := Id; Query.Open; TextField := Query.FieldByName('data').AsString; Writeln('The value of TextField is: ', TextField); // Change the value of TextField Query.Edit; Query.FieldByName('data').AsString := 'Changed Topic '+DateTimeToStr(Now); TextField := Query.FieldByName('data').AsString; Writeln('The value of TextField is now: ', TextField); // Commit the change Try Query.Post; Query.ApplyUpdates; Txn.Commit; Except // Usually EUpdateError On E: Exception Do writeln(E.Message) End; Finally Query.Free; End; Except On E: Exception Do writeln('Could not Create Query:', E.Message); End; Finally Txn.Free; End; Except On E: Exception Do writeln('Could not create transaction: ',E.Message); End; Finally Conn.Free; End;Except On E: Exception Do writeln('Could not create connection: ', E.Message);End; writeln('Press any key'); readln;End.Done. Maybe only specialize the exception objects a bit. (EDatabaseError or derived from EDatabase error, like EUpdateError)
WJSwanepoel:
Thank you for the help. I will try to apply myself and get it working.
Thaddy:
I editted the code somewhat because as an oversight I handled the try finally's not correctly. Brought the creation of the class instances outside of their subsequent try finally block. As it should be.
WJSwanepoel:
Thank you, much appreciated.
Navigation
[0] Message Index
[#] Next page
[*] Previous page