How to insert integer value into SQLite table at Delphi -


i trying insert integer value in sqlite table @ delphi.
in table emp usergroup_id integer , label, description string data type.
code follows:

var   gid: integer;   sdescription,ldescription: string; begin   sdescription := rzedit1.text;   ldescription := rzmemo1.text;   gid := integer(rzcombobox1.items.objects[rzcombobox1.items.indexof(gname)]);    try     sqlconnection1.connected := true;     sqlmonitor1.active := true;     ssql := 'insert emp(usergroup_id, label, description) values (gid,''' + sdescription + ''',''' + ldescription + ''' )';     sqlconnection1.executedirect(ssql);    except     on e: edatabaseerror       showmessage('exception raised message' + e.message);   end; end; 

it giving me error unknown column gid.
when tried fixed integer value instead of variable works:

ssql := 'insert emp(usergroup_id, label, description) values (1,''' + sdescription + ''',''' + ldescription + ''' )'; 

it inserts values table.
how insert integer value of gid database above query. proper format?

your gid becomes part of sql statement (hence error: unknown column gid).
need use delphi gid variable construct sql statement (just did sdescription , ldescription):

ssql := 'insert emp(usergroup_id, label, description) values (' + inttostr(gid) + ', ''' + sdescription + ''',''' + ldescription + ''' )'; 

if have used parameters wouldn't have such messy query/code (which subject sql injection, etc..) e.g.:

qry := tsqlquery.create(nil); // or ever tquery component use in framework try   qry.sqlconnection := sqlconnection1;   qry.sql.text := 'insert emp(usergroup_id, label, description) values (:usergroup_id, :label, :description)';   qry.params.parambyname('usergroup_id').value := gid;   qry.params.parambyname('label').value := sdescription;   qry.params.parambyname('description').value := ldescription;   qry.execsql;   qry.free; end; 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -